-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstack.spec.ts
More file actions
148 lines (120 loc) · 4.75 KB
/
stack.spec.ts
File metadata and controls
148 lines (120 loc) · 4.75 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
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 { 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';
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 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();
});
});