@apolitical/content 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,130 +0,0 @@
1
- import { createClient } from 'contentful';
2
- import { setContext, getEntry, getEntries } from '../../lib';
3
- import { getContext } from '../../lib/helper/context';
4
-
5
- import { entryMock } from './mocks/microcourse.mock';
6
- import { entriesMock } from './mocks/microcourses.mock';
7
-
8
- // const mockCreateClient = jest.fn();
9
-
10
- jest.mock('contentful', () => {
11
- const actual = jest.requireActual('contentful');
12
- return {
13
- ...actual,
14
- createClient: jest.fn(),
15
- };
16
- });
17
-
18
- jest.mock('contentful');
19
-
20
- describe('Apolitical Content', () => {
21
- const getEntriesStub = jest.fn();
22
- const context = {
23
- space: 'some-space',
24
- accessToken: 'c29tZS1hY2Nlc3MtdG9rZW4=',
25
- previewToken: 'c29tZS1wcmV2aWV3LXRva2Vu',
26
- environment: 'some-environment',
27
- origin: 'https://beta.apolitical.co',
28
- };
29
-
30
- describe('Global context', () => {
31
- beforeEach(() => {
32
- setContext(Object.assign({}, context, { isPreview: false }));
33
- createClient.mockResolvedValue({ getEntries: getEntriesStub });
34
- });
35
-
36
- describe('Context', () => {
37
- test('should return cdn as default host', async () => {
38
- const testContext = getContext();
39
- expect(testContext.host).toBe('cdn.contentful.com');
40
- });
41
- test('should return access token', async () => {
42
- const testContext = getContext();
43
- expect(testContext.accessToken).toBe('some-access-token');
44
- });
45
-
46
- describe('Preview', () => {
47
- beforeEach(() => {
48
- setContext(Object.assign({}, context, { isPreview: true }));
49
- });
50
-
51
- test('should return preview host', async () => {
52
- const testContext2 = getContext();
53
- expect(testContext2.host).toBe('preview.contentful.com');
54
- });
55
-
56
- test('should return preview token', async () => {
57
- const testContext2 = getContext();
58
- expect(testContext2.accessToken).toBe('some-preview-token');
59
- });
60
- });
61
- });
62
-
63
- describe('Get Entry', () => {
64
- const query = {
65
- content_type: 'microcourse',
66
- 'fields.slug[in]': 'some-slug',
67
- include: 2,
68
- };
69
-
70
- test('should use default context', async () => {
71
- getEntriesStub.mockReturnValueOnce(entryMock);
72
- await getEntry(query);
73
- expect(createClient).toHaveBeenCalledTimes(1);
74
- expect(createClient.mock.calls[0][0]).toMatchSnapshot();
75
- });
76
-
77
- test('should use preview context', async () => {
78
- getEntriesStub.mockReturnValueOnce(entryMock);
79
- await getEntry(query, {}, true);
80
- expect(createClient).toHaveBeenCalledTimes(1);
81
- expect(createClient.mock.calls[0][0]).toMatchSnapshot();
82
- });
83
-
84
- test('should read entry', async () => {
85
- getEntriesStub.mockReturnValueOnce(entryMock);
86
- const entry = await getEntry(query);
87
- expect(entry).toMatchSnapshot();
88
- });
89
-
90
- test('should read raw entry', async () => {
91
- getEntriesStub.mockReturnValueOnce(entryMock);
92
- const entry = await getEntry(query, { flatten: false });
93
- expect(entry).toEqual(entryMock.items[0]);
94
- });
95
- });
96
-
97
- describe('Read Entries', () => {
98
- const query = {
99
- content_type: 'microcourse',
100
- limit: 2,
101
- };
102
-
103
- test('should use default context', async () => {
104
- getEntriesStub.mockReturnValueOnce(entriesMock);
105
- await getEntries(query);
106
- expect(createClient).toHaveBeenCalledTimes(1);
107
- expect(createClient.mock.calls[0][0]).toMatchSnapshot();
108
- });
109
-
110
- test('should use preview context', async () => {
111
- getEntriesStub.mockReturnValueOnce(entriesMock);
112
- await getEntries(query, {}, true);
113
- expect(createClient).toHaveBeenCalledTimes(1);
114
- expect(createClient.mock.calls[0][0]).toMatchSnapshot();
115
- });
116
-
117
- test('should get entries', async () => {
118
- getEntriesStub.mockReturnValueOnce(entriesMock);
119
- const entries = await getEntries(query);
120
- expect(entries).toMatchSnapshot();
121
- });
122
-
123
- test('should read raw entries', async () => {
124
- getEntriesStub.mockReturnValueOnce(entriesMock);
125
- const entries = await getEntries(query, { flatten: false });
126
- expect(entries).toEqual(entriesMock.items);
127
- });
128
- });
129
- });
130
- });