Skip to content

fix: reset livePreviewConfig properties if not received in params #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions src/lib/stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 3 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ export interface FindResponse<T> {
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
Expand Down
85 changes: 84 additions & 1 deletion test/unit/stack.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 = <jest.Mock<typeof synchronization>>(<unknown>synchronization);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
});
});

Loading