Skip to content

Commit f7bce9c

Browse files
add unit tests for Contentstack caching behavior
1 parent 139f5fe commit f7bce9c

File tree

2 files changed

+68
-9
lines changed

2 files changed

+68
-9
lines changed

test/unit/contentstack-debug-integration.spec.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('Contentstack Debug Logging Integration', () => {
165165
mockClient.restore();
166166
});
167167

168-
it('should execute cache adapter when cacheOptions is provided', async () => {
168+
it('should set cache adapter when cacheOptions is provided', () => {
169169
const config: StackConfig = {
170170
apiKey: "apiKey",
171171
deliveryToken: "delivery",
@@ -178,18 +178,49 @@ describe('Contentstack Debug Logging Integration', () => {
178178

179179
const stack = Contentstack.stack(config);
180180
const client = stack.getClient();
181-
const mockClient = new MockAdapter(client);
182181

183-
mockClient.onGet('/content_types/test').reply(200, {
184-
content_types: []
185-
});
182+
// Verify the custom adapter was set
183+
const customAdapter = client.defaults.adapter;
184+
expect(customAdapter).toBeDefined();
185+
expect(typeof customAdapter).toBe('function');
186+
});
187+
188+
it('should set cache adapter with NETWORK_ELSE_CACHE policy', () => {
189+
const config: StackConfig = {
190+
apiKey: "apiKey",
191+
deliveryToken: "delivery",
192+
environment: "env",
193+
cacheOptions: {
194+
policy: Policy.NETWORK_ELSE_CACHE,
195+
maxAge: 3600,
196+
},
197+
};
198+
199+
const stack = Contentstack.stack(config);
200+
const client = stack.getClient();
186201

187-
// Make request to trigger cache adapter
188-
await client.get('/content_types/test', { contentTypeUid: 'test' });
202+
const customAdapter = client.defaults.adapter;
203+
expect(customAdapter).toBeDefined();
204+
expect(typeof customAdapter).toBe('function');
205+
});
189206

190-
expect(client.defaults.adapter).toBeDefined();
207+
it('should set cache adapter with CACHE_ELSE_NETWORK policy', () => {
208+
const config: StackConfig = {
209+
apiKey: "apiKey",
210+
deliveryToken: "delivery",
211+
environment: "env",
212+
cacheOptions: {
213+
policy: Policy.CACHE_ELSE_NETWORK,
214+
maxAge: 3600,
215+
},
216+
};
191217

192-
mockClient.restore();
218+
const stack = Contentstack.stack(config);
219+
const client = stack.getClient();
220+
221+
const customAdapter = client.defaults.adapter;
222+
expect(customAdapter).toBeDefined();
223+
expect(typeof customAdapter).toBe('function');
193224
});
194225
});
195226

test/unit/contentstack.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,34 @@ describe("Contentstack", () => {
470470
isBrowserSpy.mockRestore();
471471
});
472472

473+
it('should use fallback value when live_preview param is empty (line 74 || branch)', () => {
474+
const isBrowserSpy = jest.spyOn(utils, 'isBrowser').mockReturnValue(true);
475+
476+
// Mock document.location with empty live_preview param
477+
(global as any).document = {
478+
location: {
479+
toString: () => 'http://localhost?live_preview='
480+
}
481+
};
482+
483+
const config = {
484+
apiKey: "apiKey",
485+
deliveryToken: "delivery",
486+
environment: "env",
487+
live_preview: {
488+
enable: true,
489+
live_preview: 'fallback_hash'
490+
},
491+
};
492+
493+
const stackInstance = createStackInstance(config);
494+
495+
// Should use the fallback value when params.get returns empty string
496+
expect(stackInstance.config.live_preview?.live_preview).toBe('fallback_hash');
497+
498+
isBrowserSpy.mockRestore();
499+
});
500+
473501
it('should not extract params when not in browser environment', () => {
474502
const isBrowserSpy = jest.spyOn(utils, 'isBrowser').mockReturnValue(false);
475503

0 commit comments

Comments
 (0)