-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlivePreviewEventManager.test.ts
More file actions
252 lines (199 loc) · 8.62 KB
/
livePreviewEventManager.test.ts
File metadata and controls
252 lines (199 loc) · 8.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/**
* @vitest-environment jsdom
*/
import { vi } from "vitest";
import { EventManager } from "@contentstack/advanced-post-message";
import { LIVE_PREVIEW_CHANNEL_ID } from "../livePreviewEventManager.constant";
// Mock dependencies
vi.mock("@contentstack/advanced-post-message", () => ({
EventManager: vi.fn(),
}));
vi.mock("../../../common/inIframe", () => ({
inNewTab: vi.fn(),
}));
// Import after mocking
import { inNewTab } from "../../../common/inIframe";
describe("livePreviewEventManager", () => {
let mockEventManager: any;
let originalWindow: any;
beforeEach(() => {
// Reset all mocks
vi.clearAllMocks();
// Create mock EventManager
mockEventManager = {
on: vi.fn(),
send: vi.fn(),
};
(EventManager as any).mockImplementation(() => mockEventManager);
// Store original window
originalWindow = global.window;
// Reset inNewTab mock
(inNewTab as any).mockReturnValue(false);
});
afterEach(() => {
// Restore original window
global.window = originalWindow;
// Clear module cache to reset the module state
vi.resetModules();
});
describe("when window is undefined", () => {
beforeEach(() => {
// Mock window as undefined
Object.defineProperty(global, "window", {
value: undefined,
writable: true,
});
});
it("should not initialize EventManager when window is undefined", async () => {
// Re-import the module to trigger initialization
const module = await import("../livePreviewEventManager");
expect(EventManager).not.toHaveBeenCalled();
expect(module.default).toBeUndefined();
});
});
describe("when window is defined", () => {
let mockWindow: any;
beforeEach(() => {
// Create mock window object
mockWindow = {
parent: { postMessage: vi.fn() },
opener: { postMessage: vi.fn() },
};
Object.defineProperty(global, "window", {
value: mockWindow,
writable: true,
});
});
it("should initialize EventManager with window.parent as target when not in new tab", async () => {
(inNewTab as any).mockReturnValue(false);
// Re-import the module to trigger initialization
const module = await import("../livePreviewEventManager");
expect(EventManager).toHaveBeenCalledWith(LIVE_PREVIEW_CHANNEL_ID, {
target: mockWindow.parent,
debug: false,
suppressErrors: true,
});
expect(module.default).toBe(mockEventManager);
});
it("should initialize EventManager with window.opener as target when in new tab", async () => {
(inNewTab as any).mockReturnValue(true);
// Re-import the module to trigger initialization
const module = await import("../livePreviewEventManager");
expect(EventManager).toHaveBeenCalledWith(LIVE_PREVIEW_CHANNEL_ID, {
target: mockWindow.opener,
debug: false,
suppressErrors: true,
});
expect(module.default).toBe(mockEventManager);
});
it("should call inNewTab to determine the target", async () => {
// Re-import the module to trigger initialization
await import("../livePreviewEventManager");
expect(inNewTab).toHaveBeenCalled();
});
it("should use correct channel ID", async () => {
// Re-import the module to trigger initialization
await import("../livePreviewEventManager");
expect(EventManager).toHaveBeenCalledWith(
LIVE_PREVIEW_CHANNEL_ID,
expect.any(Object)
);
});
it("should set correct default event options", async () => {
(inNewTab as any).mockReturnValue(false);
// Re-import the module to trigger initialization
await import("../livePreviewEventManager");
expect(EventManager).toHaveBeenCalledWith(
expect.any(String),
expect.objectContaining({
debug: false,
suppressErrors: true,
})
);
});
describe("target selection logic", () => {
it("should prioritize window.opener when inNewTab returns true", async () => {
(inNewTab as any).mockReturnValue(true);
// Re-import the module to trigger initialization
await import("../livePreviewEventManager");
const callArgs = (EventManager as any).mock.calls[0];
expect(callArgs[1].target).toBe(mockWindow.opener);
expect(callArgs[1].target).not.toBe(mockWindow.parent);
});
it("should use window.parent when inNewTab returns false", async () => {
(inNewTab as any).mockReturnValue(false);
// Re-import the module to trigger initialization
await import("../livePreviewEventManager");
const callArgs = (EventManager as any).mock.calls[0];
expect(callArgs[1].target).toBe(mockWindow.parent);
expect(callArgs[1].target).not.toBe(mockWindow.opener);
});
it("should throw error when inNewTab throws an error", async () => {
(inNewTab as any).mockImplementation(() => {
throw new Error("inNewTab error");
});
// Should throw because inNewTab error is not caught in the implementation
await expect(async () => {
await import("../livePreviewEventManager");
}).rejects.toThrow("inNewTab error");
});
});
describe("edge cases", () => {
it("should handle missing window.parent gracefully", async () => {
mockWindow.parent = undefined;
(inNewTab as any).mockReturnValue(false);
// Re-import the module to trigger initialization
const module = await import("../livePreviewEventManager");
expect(EventManager).toHaveBeenCalledWith(LIVE_PREVIEW_CHANNEL_ID, {
target: undefined,
debug: false,
suppressErrors: true,
});
expect(module.default).toBe(mockEventManager);
});
it("should handle missing window.opener gracefully", async () => {
mockWindow.opener = undefined;
(inNewTab as any).mockReturnValue(true);
// Re-import the module to trigger initialization
const module = await import("../livePreviewEventManager");
expect(EventManager).toHaveBeenCalledWith(LIVE_PREVIEW_CHANNEL_ID, {
target: undefined,
debug: false,
suppressErrors: true,
});
expect(module.default).toBe(mockEventManager);
});
it("should handle when EventManager constructor throws", async () => {
(EventManager as any).mockImplementation(() => {
throw new Error("EventManager constructor error");
});
// Should not crash the module initialization
expect(async () => {
await import("../livePreviewEventManager");
}).not.toThrow();
});
});
});
describe("module export", () => {
it("should export the EventManager instance when window is available", async () => {
const mockWindow = {
parent: { postMessage: vi.fn() },
opener: { postMessage: vi.fn() },
};
Object.defineProperty(global, "window", {
value: mockWindow,
writable: true,
});
const module = await import("../livePreviewEventManager");
expect(module.default).toBe(mockEventManager);
});
it("should export undefined when window is not available", async () => {
Object.defineProperty(global, "window", {
value: undefined,
writable: true,
});
const module = await import("../livePreviewEventManager");
expect(module.default).toBeUndefined();
});
});
});