Skip to content

Commit f0b80ab

Browse files
committed
Fixed Unit test cases
1 parent c522698 commit f0b80ab

3 files changed

Lines changed: 392 additions & 203 deletions

File tree

test/unit/cache.spec.ts

Lines changed: 190 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
1-
import MockAdapter from 'axios-mock-adapter';
2-
import { httpClient, AxiosInstance } from '@contentstack/core';
3-
import { handleRequest } from '../../src/lib/cache';
4-
import { HOST_URL } from '../utils/constant';
5-
import { Policy } from '../../src/lib/types';
6-
import { PersistanceStore } from '../../src/persistance';
7-
8-
describe('Cache handleRequest function', () => {
1+
import MockAdapter from "axios-mock-adapter";
2+
import { httpClient, AxiosInstance } from "@contentstack/core";
3+
import { handleRequest } from "../../src/lib/cache";
4+
import { HOST_URL } from "../utils/constant";
5+
import { Policy } from "../../src/lib/types";
6+
import { PersistanceStore } from "../../src/persistance";
7+
import { iGlobal } from "../../src/persistance/helper/utils";
8+
9+
// Mock localStorage for Node.js environment
10+
const mockLocalStorage = (() => {
11+
let store: { [key: string]: string } = {};
12+
return {
13+
getItem: (key: string) => store[key] || null,
14+
setItem: (key: string, value: string) => {
15+
store[key] = value;
16+
},
17+
removeItem: (key: string) => {
18+
delete store[key];
19+
},
20+
clear: () => {
21+
store = {};
22+
},
23+
get length() {
24+
return Object.keys(store).length;
25+
},
26+
key: (index: number) => Object.keys(store)[index] || null,
27+
};
28+
})();
29+
30+
// Setup mock before tests
31+
beforeAll(() => {
32+
(iGlobal as any).localStorage = mockLocalStorage;
33+
});
34+
35+
describe("Cache handleRequest function", () => {
936
let client: AxiosInstance;
1037
let mockClient: MockAdapter;
1138
let apiKey: string;
@@ -19,164 +46,266 @@ describe('Cache handleRequest function', () => {
1946
});
2047

2148
beforeEach(() => {
22-
apiKey = 'testKey';
49+
apiKey = "testKey";
2350
resolve = jest.fn();
2451
reject = jest.fn();
25-
config = { contentTypeUid: 'testContentType', headers: {} };
52+
config = { contentTypeUid: "testContentType", headers: {} };
2653
});
2754

28-
describe('NETWORK_ELSE_CACHE policy', () => {
29-
it('should return network response when proper response is received', async () => {
55+
describe("NETWORK_ELSE_CACHE policy", () => {
56+
it("should return network response when proper response is received", async () => {
3057
const cacheOptions = { policy: Policy.NETWORK_ELSE_CACHE, maxAge: 3600 };
31-
const defaultAdapter = jest.fn((_config) => ({ data: JSON.stringify('foo') }));
58+
const defaultAdapter = jest.fn((_config) => ({
59+
data: JSON.stringify("foo"),
60+
}));
3261
const cacheStore = new PersistanceStore(cacheOptions);
3362

34-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
63+
await handleRequest(
64+
cacheOptions,
65+
apiKey,
66+
defaultAdapter,
67+
resolve,
68+
reject,
69+
config
70+
);
3571

3672
expect(defaultAdapter).toHaveBeenCalledWith(config);
37-
expect(resolve).toBeCalledWith({"data": "foo"});
73+
expect(resolve).toBeCalledWith({ data: "foo" });
3874
expect(reject).not.toBeCalled();
3975

4076
cacheStore.removeItem(apiKey, config.contentTypeUid);
4177
});
4278

43-
it('should return cache data when proper network response is not received', async () => {
79+
it("should return cache data when proper network response is not received", async () => {
4480
const cacheOptions = { policy: Policy.NETWORK_ELSE_CACHE, maxAge: 3600 };
4581
const defaultAdapter = jest.fn().mockReturnValue({
46-
foo: 'bar',
47-
baz: 'quux',
82+
foo: "bar",
83+
baz: "quux",
4884
});
4985
const cacheStore = new PersistanceStore(cacheOptions);
5086

51-
cacheStore.setItem(apiKey, 'cacheData', config.contentTypeUid, cacheOptions.maxAge);
52-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
87+
cacheStore.setItem(
88+
apiKey,
89+
"cacheData",
90+
config.contentTypeUid,
91+
cacheOptions.maxAge
92+
);
93+
await handleRequest(
94+
cacheOptions,
95+
apiKey,
96+
defaultAdapter,
97+
resolve,
98+
reject,
99+
config
100+
);
53101

54102
expect(defaultAdapter).toHaveBeenCalledWith(config);
55-
expect(resolve).toBeCalledWith({ config: {}, data: 'cacheData', headers: {}, status: 200, statusText: 'OK' });
103+
expect(resolve).toBeCalledWith({
104+
config: {},
105+
data: "cacheData",
106+
headers: {},
107+
status: 200,
108+
statusText: "OK",
109+
});
56110
expect(reject).not.toBeCalled();
57111

58112
cacheStore.removeItem(apiKey, config.contentTypeUid);
59113
});
60114

61-
it('should return error data when network response has error', async () => {
115+
it("should return error data when network response has error", async () => {
62116
const cacheOptions = { policy: Policy.NETWORK_ELSE_CACHE, maxAge: 3600 };
63117
const defaultAdapter = jest.fn().mockReturnValue({
64-
foo: 'bar',
65-
baz: 'quux',
118+
foo: "bar",
119+
baz: "quux",
66120
});
67121
const cacheStore = new PersistanceStore(cacheOptions);
68122

69-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
123+
await handleRequest(
124+
cacheOptions,
125+
apiKey,
126+
defaultAdapter,
127+
resolve,
128+
reject,
129+
config
130+
);
70131

71132
expect(defaultAdapter).toHaveBeenCalledWith(config);
72133
expect(resolve).not.toBeCalled();
73134
expect(reject).toBeCalledWith({
74-
foo: 'bar',
75-
baz: 'quux',
135+
foo: "bar",
136+
baz: "quux",
76137
});
77138

78139
cacheStore.removeItem(apiKey, config.contentTypeUid);
79140
});
80141
});
81142

82-
describe('CACHE_THEN_NETWORK policy', () => {
83-
it('should return cache response when proper cache is available then return network response', async () => {
143+
describe("CACHE_THEN_NETWORK policy", () => {
144+
it("should return cache response when proper cache is available then return network response", async () => {
84145
const cacheOptions = { policy: Policy.CACHE_THEN_NETWORK, maxAge: 3600 };
85-
const defaultAdapter = jest.fn((_config) => ({ data: 'foo' }));
146+
const defaultAdapter = jest.fn((_config) => ({ data: "foo" }));
86147

87148
const cacheStore = new PersistanceStore(cacheOptions);
88-
cacheStore.setItem(apiKey, 'cacheData', config.contentTypeUid, cacheOptions.maxAge);
89-
90-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
149+
cacheStore.setItem(
150+
apiKey,
151+
"cacheData",
152+
config.contentTypeUid,
153+
cacheOptions.maxAge
154+
);
155+
156+
await handleRequest(
157+
cacheOptions,
158+
apiKey,
159+
defaultAdapter,
160+
resolve,
161+
reject,
162+
config
163+
);
91164

92165
expect(defaultAdapter).not.toHaveBeenCalled();
93-
expect(resolve).toBeCalledWith({ config: {}, data: 'cacheData', headers: {}, status: 200, statusText: 'OK' });
166+
expect(resolve).toBeCalledWith({
167+
config: {},
168+
data: "cacheData",
169+
headers: {},
170+
status: 200,
171+
statusText: "OK",
172+
});
94173
expect(reject).not.toBeCalled();
95174

96175
cacheStore.removeItem(apiKey, config.contentTypeUid);
97176
});
98-
it('should return api response when proper cache is not available', async () => {
177+
it("should return api response when proper cache is not available", async () => {
99178
const cacheOptions = { policy: Policy.CACHE_THEN_NETWORK, maxAge: 3600 };
100-
const defaultAdapter = jest.fn((_config) => ({ data: JSON.stringify('foo') }));
179+
const defaultAdapter = jest.fn((_config) => ({
180+
data: JSON.stringify("foo"),
181+
}));
101182

102183
const cacheStore = new PersistanceStore(cacheOptions);
103184

104-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
185+
await handleRequest(
186+
cacheOptions,
187+
apiKey,
188+
defaultAdapter,
189+
resolve,
190+
reject,
191+
config
192+
);
105193

106194
expect(defaultAdapter).toHaveBeenCalled();
107-
expect(resolve).toBeCalledWith({"data": "foo"});
195+
expect(resolve).toBeCalledWith({ data: "foo" });
108196
expect(reject).not.toBeCalled();
109197

110198
cacheStore.removeItem(apiKey, config.contentTypeUid);
111199
});
112-
it('should return error api response when data is not available in network or cache', async () => {
200+
it("should return error api response when data is not available in network or cache", async () => {
113201
const cacheOptions = { policy: Policy.CACHE_THEN_NETWORK, maxAge: 3600 };
114202
const defaultAdapter = jest.fn().mockReturnValue({
115-
foo: 'bar',
116-
baz: 'quux',
203+
foo: "bar",
204+
baz: "quux",
117205
});
118206

119207
const cacheStore = new PersistanceStore(cacheOptions);
120208

121-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
209+
await handleRequest(
210+
cacheOptions,
211+
apiKey,
212+
defaultAdapter,
213+
resolve,
214+
reject,
215+
config
216+
);
122217

123218
expect(defaultAdapter).toHaveBeenCalled();
124219
expect(resolve).not.toBeCalled();
125220
expect(reject).toBeCalledWith({
126-
foo: 'bar',
127-
baz: 'quux',
221+
foo: "bar",
222+
baz: "quux",
128223
});
129224

130225
cacheStore.removeItem(apiKey, config.contentTypeUid);
131226
});
132227
});
133228

134-
describe('CACHE_ELSE_NETWORK policy', () => {
135-
it('should return cache response when proper cache is available', async () => {
229+
describe("CACHE_ELSE_NETWORK policy", () => {
230+
it("should return cache response when proper cache is available", async () => {
136231
const cacheOptions = { policy: Policy.CACHE_ELSE_NETWORK, maxAge: 3600 };
137-
const defaultAdapter = jest.fn((_config) => ({ data: 'foo' }));
232+
const defaultAdapter = jest.fn((_config) => ({ data: "foo" }));
138233

139234
const cacheStore = new PersistanceStore(cacheOptions);
140-
cacheStore.setItem(apiKey, 'cacheData', config.contentTypeUid, cacheOptions.maxAge);
141-
142-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
235+
cacheStore.setItem(
236+
apiKey,
237+
"cacheData",
238+
config.contentTypeUid,
239+
cacheOptions.maxAge
240+
);
241+
242+
await handleRequest(
243+
cacheOptions,
244+
apiKey,
245+
defaultAdapter,
246+
resolve,
247+
reject,
248+
config
249+
);
143250

144251
expect(defaultAdapter).not.toHaveBeenCalledWith(config);
145-
expect(resolve).toBeCalledWith({ config: {}, data: 'cacheData', headers: {}, status: 200, statusText: 'OK' });
252+
expect(resolve).toBeCalledWith({
253+
config: {},
254+
data: "cacheData",
255+
headers: {},
256+
status: 200,
257+
statusText: "OK",
258+
});
146259
expect(reject).not.toBeCalled();
147260

148261
cacheStore.removeItem(apiKey, config.contentTypeUid);
149262
});
150263

151-
it('should return network response data when cache is not available', async () => {
264+
it("should return network response data when cache is not available", async () => {
152265
const cacheOptions = { policy: Policy.CACHE_ELSE_NETWORK, maxAge: 3600 };
153-
const defaultAdapter = jest.fn((_config) => ({ data: JSON.stringify('foo') }));
266+
const defaultAdapter = jest.fn((_config) => ({
267+
data: JSON.stringify("foo"),
268+
}));
154269
const cacheStore = new PersistanceStore(cacheOptions);
155270

156-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
271+
await handleRequest(
272+
cacheOptions,
273+
apiKey,
274+
defaultAdapter,
275+
resolve,
276+
reject,
277+
config
278+
);
157279

158280
expect(defaultAdapter).toHaveBeenCalledWith(config);
159-
expect(resolve).toBeCalledWith({"data": "foo"});
281+
expect(resolve).toBeCalledWith({ data: "foo" });
160282
expect(reject).not.toBeCalled();
161283

162284
cacheStore.removeItem(apiKey, config.contentTypeUid);
163285
});
164286

165-
it('should return error data when network response has error', async () => {
287+
it("should return error data when network response has error", async () => {
166288
const cacheOptions = { policy: Policy.CACHE_ELSE_NETWORK, maxAge: 3600 };
167289
const defaultAdapter = jest.fn().mockReturnValue({
168-
foo: 'bar',
169-
baz: 'quux',
290+
foo: "bar",
291+
baz: "quux",
170292
});
171293
const cacheStore = new PersistanceStore(cacheOptions);
172294

173-
await handleRequest(cacheOptions, apiKey, defaultAdapter, resolve, reject, config);
295+
await handleRequest(
296+
cacheOptions,
297+
apiKey,
298+
defaultAdapter,
299+
resolve,
300+
reject,
301+
config
302+
);
174303

175304
expect(defaultAdapter).toHaveBeenCalled();
176305
expect(resolve).not.toBeCalled();
177306
expect(reject).toBeCalledWith({
178-
foo: 'bar',
179-
baz: 'quux',
307+
foo: "bar",
308+
baz: "quux",
180309
});
181310

182311
cacheStore.removeItem(apiKey, config.contentTypeUid);

0 commit comments

Comments
 (0)