Skip to content

Commit 539bac3

Browse files
authored
Merge pull request #319 from contentstack/VE-4802
chore: remove snapshot testing
2 parents f97db98 + a2d4397 commit 539bac3

69 files changed

Lines changed: 1251 additions & 9877 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/configManager/__test__/handleUserConfig.test.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -195,24 +195,6 @@ describe("handleInitData()", () => {
195195
expect(config.stackDetails.apiKey).toBe("bltuserapikey");
196196
});
197197

198-
test.skip("should set api key from headers if available", () => {
199-
// we removed it so it is good that it is skipped
200-
const initData: Partial<IInitData> = {
201-
enable: true,
202-
203-
stackSdk: {
204-
live_preview: {},
205-
headers: {
206-
api_key: "bltheaderapikey",
207-
},
208-
environment: "dev",
209-
},
210-
};
211-
212-
handleInitData(initData);
213-
expect(config.stackDetails.apiKey).toBe("bltheaderapikey");
214-
});
215-
216198
test("should reset api key if it is not passed", () => {
217199
const initData: Partial<IInitData> = {
218200
enable: true,

src/livePreview/__test__/live-preview.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getDefaultConfig } from "../../configManager/config.default";
1010
import Config from "../../configManager/configManager";
1111
import { PublicLogger } from "../../logger/logger";
1212
import { ILivePreviewWindowType } from "../../types/types";
13-
import * as utils from "../../utils";
13+
import { addLivePreviewQueryTags } from '../../utils/addLivePreviewQueryTags';
1414
import livePreviewPostMessage from "../eventManager/livePreviewEventManager";
1515
import { LIVE_PREVIEW_POST_MESSAGE_EVENTS } from "../eventManager/livePreviewEventManager.constant";
1616
import {
@@ -20,6 +20,9 @@ import {
2020
import LivePreview from "../live-preview";
2121
import { mockLivePreviewInitEventListener } from "./mock";
2222

23+
vi.mock("../../utils/addLivePreviewQueryTags", () => ({
24+
addLivePreviewQueryTags: vi.fn(),
25+
}));
2326
vi.mock("../../visualBuilder/utils/visualBuilderPostMessage", async () => {
2427
const { getAllContentTypes } = await vi.importActual<
2528
typeof import("../../__test__/data/contentType")
@@ -429,15 +432,15 @@ describe("incoming postMessage", () => {
429432

430433
describe("testing window event listeners", () => {
431434
let addEventListenerMock: any;
432-
let sendInitEvent: any;
435+
let sendInitEvent = vi.fn().mockImplementation(mockLivePreviewInitEventListener);
433436
let livePreviewInstance: LivePreview;
434437

435438
beforeEach(() => {
436439
Config.reset();
437440
livePreviewPostMessage?.destroy({ soft: true });
438441
livePreviewPostMessage?.on(
439442
LIVE_PREVIEW_POST_MESSAGE_EVENTS.INIT,
440-
mockLivePreviewInitEventListener
443+
sendInitEvent
441444
);
442445

443446
const titlePara = document.createElement("h3");
@@ -457,13 +460,10 @@ describe("testing window event listeners", () => {
457460
document.body.appendChild(descPara);
458461
document.body.appendChild(linkPara);
459462

460-
Config.set("windowType", ILivePreviewWindowType.PREVIEW);
461-
462463
addEventListenerMock = vi.spyOn(window, "addEventListener");
463464
});
464465

465466
afterEach(() => {
466-
vi.restoreAllMocks();
467467
document.getElementsByTagName("html")[0].innerHTML = "";
468468
});
469469

@@ -473,11 +473,7 @@ describe("testing window event listeners", () => {
473473
});
474474

475475
test("should attach a load event to call requestDataSync if document is not yet loaded", () => {
476-
Object.defineProperty(document, "readyState", {
477-
get() {
478-
return "loading";
479-
},
480-
});
476+
const readyState = vi.spyOn(document, 'readyState', 'get').mockReturnValue('loading');
481477

482478
Config.replace({
483479
enable: true,
@@ -489,28 +485,32 @@ describe("testing window event listeners", () => {
489485
"load",
490486
expect.any(Function)
491487
);
488+
readyState.mockRestore();
492489
});
493-
//TODO: fix this test
494-
test.skip("should handle link click event if ssr is set to true", async () => {
495-
vi.spyOn(utils, "addLivePreviewQueryTags");
490+
test("should handle link click event if ssr is set to true", async () => {
491+
496492
Config.replace({
497493
enable: true,
498494
ssr: true,
495+
debug: true,
499496
});
500497

501498
const targetElement = document.createElement("a");
502499
targetElement.href = "http://localhost:3000/";
503500

504501
document.body.appendChild(targetElement);
505502
await act(async () => {
506-
livePreviewInstance = new LivePreview();
503+
livePreviewInstance = new LivePreview();
507504
});
505+
await waitFor(() => {
506+
expect(sendInitEvent).toBeCalled();
507+
})
508508
await waitFor(() => {
509509
expect(Config.get().stackDetails.contentTypeUid).toBe('contentTypeUid');
510510
})
511511
await act(async () => {
512512
fireEvent.click(targetElement);
513513
});
514-
expect(utils.addLivePreviewQueryTags).toBeCalled();
514+
expect(addLivePreviewQueryTags).toBeCalled();
515515
});
516516
});

src/livePreview/editButton/__test__/editButtonAction.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import livePreviewPostMessage from "../../eventManager/livePreviewEventManager";
99
import { LIVE_PREVIEW_POST_MESSAGE_EVENTS } from "../../eventManager/livePreviewEventManager.constant";
1010
import { LivePreviewEditButton } from "../editButton";
1111
import LivePreview from "../../live-preview";
12-
import { fireEvent, prettyDOM, waitFor } from "@testing-library/preact";
12+
import { act } from "@testing-library/preact";
1313

1414
Object.defineProperty(globalThis, "crypto", {
1515
value: {
@@ -595,7 +595,7 @@ describe("cslp tooltip", () => {
595595
locationSpy.mockRestore();
596596
});
597597

598-
test.skip("should re-render the edit button tooltip if not already available even when edit button is enabled", async () => {
598+
test("should re-render the edit button tooltip if not already available even when edit button is enabled", async () => {
599599
Config.replace({
600600
enable: true,
601601
editButton: {
@@ -622,12 +622,14 @@ describe("cslp tooltip", () => {
622622
bubbles: true,
623623
});
624624

625-
titlePara?.dispatchEvent(hoverEvent);
625+
await act(async () => {
626+
titlePara?.dispatchEvent(hoverEvent);
627+
})
626628

627-
expect(tooltip?.getAttribute("current-data-cslp")).toBe(TITLE_CSLP_TAG);
629+
expect(document.getElementById('cslp-tooltip')).toHaveAttribute('current-data-cslp', TITLE_CSLP_TAG);
628630

629631
descPara?.dispatchEvent(hoverEvent);
630632

631-
expect(tooltip?.getAttribute("current-data-cslp")).toBe(DESC_CSLP_TAG);
633+
expect(document.getElementById('cslp-tooltip')).toHaveAttribute('current-data-cslp', DESC_CSLP_TAG);
632634
});
633635
});

src/livePreview/editButton/editButton.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ export class LivePreviewEditButton {
335335
}
336336

337337
private updateTooltipPosition() {
338+
if (!document.getElementById("cslp-tooltip")) {
339+
this.createCslpTooltip();
340+
}
338341
const editButton = Config.get().editButton;
339342
const elements = Config.get().elements;
340343

src/preview/__test__/contentstack-live-preview-HOC.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,8 @@ describe("Live Preview HOC config", () => {
202202
});
203203
});
204204

205-
test.skip("should save the config when window is not available", () => {
205+
test("should throw error when window is not available", () => {
206+
vi.spyOn(PublicLogger, "warn");
206207
const originalWindow = global.window;
207208

208209
// mock window deletion using define property
@@ -219,11 +220,10 @@ describe("Live Preview HOC config", () => {
219220
},
220221
};
221222

222-
// expect(ContentstackLivePreview.userConfig).toBeNull();
223-
224223
ContentstackLivePreview.init(userConfig);
225224

226-
// expect(ContentstackLivePreview.userConfig).toBe(userConfig);
225+
expect(PublicLogger.warn).toHaveBeenCalledTimes(1);
226+
expect(PublicLogger.warn).toHaveBeenCalledWith('The SDK is not initialized in the browser.');
227227

228228
// restoring the window
229229
Object.defineProperty(global, "window", {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { PublicLogger } from "../logger/logger";
2+
export function addLivePreviewQueryTags(link: string): string {
3+
try {
4+
const docUrl: URL = new URL(document.location.href);
5+
const newUrl: URL = new URL(link);
6+
const livePreviewHash: string | null =
7+
docUrl.searchParams.get("live_preview");
8+
const ctUid: string | null =
9+
docUrl.searchParams.get("content_type_uid");
10+
const entryUid: string | null = docUrl.searchParams.get("entry_uid");
11+
if (livePreviewHash && ctUid && entryUid) {
12+
newUrl.searchParams.set("live_preview", livePreviewHash);
13+
newUrl.searchParams.set("content_type_uid", ctUid);
14+
newUrl.searchParams.set("entry_uid", entryUid);
15+
}
16+
return newUrl.href;
17+
} catch (error) {
18+
PublicLogger.error("Error while adding live preview to URL");
19+
return link;
20+
}
21+
}

src/utils/index.ts

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,9 @@
11
import { PublicLogger } from "../logger/logger";
2-
2+
import { addLivePreviewQueryTags } from "./addLivePreviewQueryTags";
33
export function hasWindow(): boolean {
44
return typeof window !== "undefined";
55
}
6-
export function addLivePreviewQueryTags(link: string): string {
7-
try {
8-
const docUrl: URL = new URL(document.location.href);
9-
const newUrl: URL = new URL(link);
10-
const livePreviewHash: string | null =
11-
docUrl.searchParams.get("live_preview");
12-
const ctUid: string | null =
13-
docUrl.searchParams.get("content_type_uid");
14-
const entryUid: string | null = docUrl.searchParams.get("entry_uid");
15-
if (livePreviewHash && ctUid && entryUid) {
16-
newUrl.searchParams.set("live_preview", livePreviewHash);
17-
newUrl.searchParams.set("content_type_uid", ctUid);
18-
newUrl.searchParams.set("entry_uid", entryUid);
19-
}
20-
return newUrl.href;
21-
} catch (error) {
22-
PublicLogger.error("Error while adding live preview to URL");
23-
return link;
24-
}
25-
}
6+
export { addLivePreviewQueryTags };
267
export function addParamsToUrl() {
278
// Setting the query params to all the click events related to current domain
289
window.addEventListener("click", (event: any) => {

0 commit comments

Comments
 (0)