-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstack.spec.ts
More file actions
240 lines (194 loc) · 7.67 KB
/
stack.spec.ts
File metadata and controls
240 lines (194 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import { httpClient, AxiosInstance } from '@contentstack/core';
import { jest } from '@jest/globals';
import MockAdapter from 'axios-mock-adapter';
import { Stack } from '../../src/lib/stack';
import { Asset } from '../../src/lib/asset';
import { ContentType } from '../../src/lib/content-type';
import { HOST_URL, LOCALE } from '../utils/constant';
import { contentTypeQueryFindResponseDataMock, syncResult } from '../utils/mocks';
import { synchronization } from '../../src/lib/synchronization';
import { ContentTypeQuery } from '../../src/lib/contenttype-query';
import { AssetQuery } from '../../src/lib/asset-query';
import { StackConfig } from '../../src/lib/types';
import * as utils from '../../src/lib/utils';
jest.mock('../../src/lib/synchronization');
const syncMock = <jest.Mock<typeof synchronization>>(<unknown>synchronization);
describe('Stack class tests', () => {
let stack: Stack;
let client: AxiosInstance;
let mockClient: MockAdapter;
beforeAll(() => {
client = httpClient({ defaultHostname: HOST_URL });
mockClient = new MockAdapter(client as any);
});
beforeEach(() => {
const config = jest.fn().mockReturnValue({
apiKey: '',
deliveryToken: '',
environment: '',
});
stack = new Stack(client, config() as StackConfig);
client.defaults.params = {};
});
it('should test import of class Stack', (done) => {
expect(stack).toBeInstanceOf(Stack);
done();
});
it('should return Asset instance when asset function is called with stack obj', (done) => {
expect(stack.asset('assetUid')).toBeInstanceOf(Asset);
expect(stack.asset()).toBeInstanceOf(AssetQuery);
done();
});
it('should return ContentType instance when contentType function is called with stack obj', (done) => {
expect(stack.contentType('contentTypeUid')).toBeInstanceOf(ContentType);
expect(stack.contentType()).toBeInstanceOf(ContentTypeQuery);
done();
});
it('should return TaxonomyQuery instance when taxonomy function is called', (done) => {
const taxonomyQuery = stack.taxonomy();
expect(taxonomyQuery).toBeDefined();
done();
});
it('should return GlobalField instance when globalField function is called with uid', (done) => {
const globalField = stack.globalField('globalFieldUid');
expect(globalField).toBeDefined();
done();
});
it('should return GlobalFieldQuery instance when globalField function is called without uid', (done) => {
const globalFieldQuery = stack.globalField();
expect(globalFieldQuery).toBeDefined();
done();
});
it('should set the correct locale when setLocale function is called with proper locale param', (done) => {
stack.setLocale(LOCALE);
expect(stack.config.locale).toEqual(LOCALE);
done();
});
it('should return the syncMock value when sync is called', async () => {
syncMock.mockReturnValue(syncResult);
const result = await stack.sync();
expect(result).toEqual(syncResult);
syncMock.mockReset();
});
it('should set live preview parameters correctly when live_preview is true', (done) => {
const query = {
live_preview: 'live_preview_hash',
contentTypeUid: 'contentTypeUid',
entryUid: 'entryUid',
preview_timestamp: 'timestamp',
include_applied_variants: true,
};
stack.config.live_preview = { enable: true, live_preview: 'true' };
stack.livePreviewQuery(query);
expect(stack.getClient().stackConfig.live_preview).toEqual({
live_preview: 'live_preview_hash',
contentTypeUid: 'contentTypeUid',
enable: true,
entryUid: 'entryUid',
preview_timestamp: 'timestamp',
include_applied_variants: true,
});
done();
});
it('should set live preview parameters to null when live_preview is false', () => {
const query = {
live_preview: '',
};
stack.config.live_preview = { enable: false, live_preview: '' };
stack.livePreviewQuery(query);
expect(stack.getClient().stackConfig.live_preview).toEqual({
live_preview: '',
contentTypeUid: '',
entryUid: '',
enable: false,
preview_timestamp: '',
include_applied_variants: false,
});
});
it('should set release_id header when release_id is present in query', () => {
const query = {
live_preview: 'live_preview_hash',
release_id: 'releaseId',
};
stack.livePreviewQuery(query);
expect(stack.getClient().defaults.headers['release_id']).toEqual('releaseId');
});
it('should delete release_id header when release_id is not present in query', () => {
stack.getClient().defaults.headers['release_id'] = 'releaseId';
const query = { live_preview: 'live_preview_hash'};
stack.livePreviewQuery(query);
expect(stack.getClient().defaults.headers['release_id']).toBeUndefined();
});
it('should set preview_timestamp header when preview_timestamp is present in query', () => {
const query = {
live_preview: 'live_preview_hash',
preview_timestamp: 'timestamp',
};
stack.livePreviewQuery(query);
expect(stack.getClient().defaults.headers['preview_timestamp']).toEqual('timestamp');
});
it('should delete preview_timestamp header when preview_timestamp is not present in query', () => {
stack.getClient().defaults.headers['preview_timestamp'] = 'timestamp';
const query = { live_preview: 'live_preview_hash' };
stack.livePreviewQuery(query);
expect(stack.getClient().defaults.headers['preview_timestamp']).toBeUndefined();
});
it('should handle livePreviewQuery when live_preview config is not set', () => {
delete stack.config.live_preview;
const query = {
live_preview: 'live_preview_hash',
release_id: 'releaseId',
};
stack.livePreviewQuery(query);
expect(stack.getClient().defaults.headers['release_id']).toEqual('releaseId');
});
it('should use content_type_uid and entry_uid fallback properties', () => {
const query = {
live_preview: 'live_preview_hash',
content_type_uid: 'contentTypeUid',
entry_uid: 'entryUid',
};
stack.config.live_preview = { enable: true, live_preview: 'true' };
stack.livePreviewQuery(query);
expect(stack.getClient().stackConfig.live_preview).toEqual({
live_preview: 'live_preview_hash',
contentTypeUid: 'contentTypeUid',
enable: true,
entryUid: 'entryUid',
preview_timestamp: '',
include_applied_variants: false,
});
});
it('should return last activities', async () => {
mockClient.onGet('/content_types').reply(200, contentTypeQueryFindResponseDataMock);
const response = await stack.getLastActivities();
expect(response).toEqual(contentTypeQueryFindResponseDataMock);
expect(response.content_types).toBeDefined();
expect(Array.isArray(response.content_types)).toBe(true);
});
it('should throw error when getLastActivities fails', async () => {
mockClient.onGet('/content_types').networkError();
await expect(stack.getLastActivities()).rejects.toThrow('Error fetching last activities');
});
it('should set port to 3000', () => {
stack.setPort(3000);
expect(stack.config.port).toEqual(3000);
});
it('should not set port when provided with non-number', () => {
stack.setPort('3000' as any);
expect(stack.config.port).not.toEqual('3000');
});
it('should set debug to true', () => {
stack.setDebug(true);
expect(stack.config.debug).toEqual(true);
});
it('should set debug to false', () => {
stack.setDebug(false);
expect(stack.config.debug).toEqual(false);
});
it('should not set debug when provided with non-boolean', () => {
stack.config.debug = false;
stack.setDebug('true' as any);
expect(stack.config.debug).toEqual(false);
});
});