From 441a85cb9aedb94d50b715d9f7a869a6baf2e238 Mon Sep 17 00:00:00 2001 From: Nadeem Patwekar Date: Fri, 3 Jan 2025 20:13:50 +0530 Subject: [PATCH] fix: reset livePreviewConfig properties if not received in params --- src/lib/stack.ts | 35 +++++++++++++---- src/lib/types.ts | 4 +- test/unit/stack.spec.ts | 85 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 115 insertions(+), 9 deletions(-) diff --git a/src/lib/stack.ts b/src/lib/stack.ts index 7b639cc..9a6ec82 100644 --- a/src/lib/stack.ts +++ b/src/lib/stack.ts @@ -145,22 +145,43 @@ export class Stack { livePreviewQuery(query: LivePreviewQuery) { if (this.config.live_preview) { - const livePreviewParams: any = { - ...this.config.live_preview, - live_preview: query.live_preview || 'init', - contentTypeUid: query.contentTypeUid, - entryUid: query.entryUid, - preview_timestamp: query.preview_timestamp || "", - include_applied_variants: query.include_applied_variants || false, + let livePreviewParams: any = { ...this.config.live_preview }; + + if (query.live_preview) { + livePreviewParams = { + ...livePreviewParams, + live_preview: query.live_preview, + contentTypeUid: query.contentTypeUid || query.content_type_uid, + entryUid: query.entryUid || query.entry_uid, + preview_timestamp: query.preview_timestamp || "", + include_applied_variants: query.include_applied_variants || false, + }; + } else { + livePreviewParams = { + live_preview: null, + contentTypeUid: null, + entryUid: null, + preview_timestamp: null, + include_applied_variants: false, + }; } this._client.stackConfig.live_preview = livePreviewParams; } if (query.hasOwnProperty('release_id')) { this._client.defaults.headers['release_id'] = query.release_id; + } else { + delete this._client.defaults.headers['release_id']; } + if (query.hasOwnProperty('preview_timestamp')) { this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp; + } else { + delete this._client.defaults.headers['preview_timestamp']; } } + + getClient(): any { + return this._client; + } } diff --git a/src/lib/types.ts b/src/lib/types.ts index 82f571d..2fcd75f 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -260,7 +260,9 @@ export interface FindResponse { export interface LivePreviewQuery { live_preview: string include_applied_variants?: boolean; - contentTypeUid: string + contentTypeUid?: string + content_type_uid?: string + entry_uid?: string entryUid?: any; preview_timestamp?: string release_id?: string diff --git a/test/unit/stack.spec.ts b/test/unit/stack.spec.ts index c61705f..35a3476 100644 --- a/test/unit/stack.spec.ts +++ b/test/unit/stack.spec.ts @@ -1,4 +1,5 @@ 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'; @@ -8,6 +9,7 @@ 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 = >(synchronization); @@ -29,7 +31,7 @@ describe('Stack class tests', () => { environment: '', }); - stack = new Stack(client, config()); + stack = new Stack(client, config() as StackConfig); }); it('should test import of class Stack', (done) => { expect(stack).toBeInstanceOf(Stack); @@ -60,4 +62,85 @@ describe('Stack class tests', () => { 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: null, + contentTypeUid: null, + entryUid: null, + preview_timestamp: null, + 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(); + }); }); +