-
Notifications
You must be signed in to change notification settings - Fork 447
Expand file tree
/
Copy pathindex.spec.ts
More file actions
152 lines (134 loc) · 4.1 KB
/
Copy pathindex.spec.ts
File metadata and controls
152 lines (134 loc) · 4.1 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
import { afterEach, describe, expect, it, vi } from 'vitest';
import type { ProgressTracker } from '@php-wasm/progress';
import type { BlueprintBundle } from '@wp-playground/blueprints';
const mocks = vi.hoisted(() => {
vi.stubGlobal('location', {
href: 'http://localhost/',
origin: 'http://localhost',
});
return {
bootPlaygroundV1: vi.fn(),
bootPlaygroundV2: vi.fn(),
BlueprintsV1Handler: vi.fn(),
BlueprintsV2Handler: vi.fn(),
createBlueprintReflection: vi.fn(),
};
});
vi.mock('@wp-playground/blueprints', () => ({
BlueprintReflection: {
create: mocks.createBlueprintReflection,
},
isBlueprintBundle: (blueprint: any) => Boolean(blueprint?.read),
}));
vi.mock('./blueprints-v1-handler', () => ({
BlueprintsV1Handler: mocks.BlueprintsV1Handler,
}));
vi.mock('./blueprints-v2-handler', () => ({
BlueprintsV2Handler: mocks.BlueprintsV2Handler,
}));
import { startPlaygroundAPI, startPlaygroundWeb } from './index';
describe('startPlaygroundWeb', () => {
afterEach(() => {
vi.clearAllMocks();
});
it('routes Blueprint v1 declarations through the v1 handler', async () => {
const playground = { connected: true };
mocks.BlueprintsV1Handler.mockImplementation(() => ({
bootPlayground: mocks.bootPlaygroundV1,
}));
mocks.bootPlaygroundV1.mockResolvedValue(playground);
const iframe = createIframe();
await expect(
startPlaygroundWeb({
iframe,
remoteUrl:
'http://localhost/remote.html?blueprints-runner=v2&existing=1',
progressTracker: createProgressTracker(),
blueprint: {
steps: [],
},
})
).resolves.toBe(playground);
expect(mocks.BlueprintsV1Handler).toHaveBeenCalledTimes(1);
expect(mocks.BlueprintsV2Handler).not.toHaveBeenCalled();
expect(iframe.src).not.toContain('blueprints-runner');
expect(iframe.src).toContain('existing=1');
});
it('routes Blueprint v2 declarations through the v2 handler', async () => {
const playground = { connected: true };
mocks.BlueprintsV2Handler.mockImplementation(() => ({
bootPlayground: mocks.bootPlaygroundV2,
}));
mocks.bootPlaygroundV2.mockResolvedValue(playground);
const iframe = createIframe();
await expect(
startPlaygroundWeb({
iframe,
remoteUrl: 'http://localhost/remote.html',
progressTracker: createProgressTracker(),
blueprint: {
version: 2,
siteOptions: {
blogname: 'V2 site',
},
},
})
).resolves.toBe(playground);
expect(mocks.BlueprintsV2Handler).toHaveBeenCalledTimes(1);
expect(mocks.BlueprintsV1Handler).not.toHaveBeenCalled();
expect(iframe.src).not.toContain('blueprints-runner');
});
it('routes Blueprint v2 bundles through the v2 handler', async () => {
const playground = { connected: true };
mocks.BlueprintsV2Handler.mockImplementation(() => ({
bootPlayground: mocks.bootPlaygroundV2,
}));
mocks.bootPlaygroundV2.mockResolvedValue(playground);
mocks.createBlueprintReflection.mockResolvedValueOnce({
getVersion: () => 2,
});
const iframe = createIframe();
const bundle = {
read: vi.fn(),
} satisfies BlueprintBundle;
await expect(
startPlaygroundWeb({
iframe,
remoteUrl: 'http://localhost/remote.html',
progressTracker: createProgressTracker(),
blueprint: bundle,
})
).resolves.toBe(playground);
expect(mocks.createBlueprintReflection).toHaveBeenCalledWith(bundle);
expect(mocks.BlueprintsV2Handler).toHaveBeenCalledTimes(1);
expect(mocks.BlueprintsV1Handler).not.toHaveBeenCalled();
expect(iframe.src).not.toContain('blueprints-runner');
});
});
describe('startPlaygroundAPI', () => {
it.each([
['a different endpoint', 'http://localhost/remote.html'],
['an untrusted origin', 'https://example.com/api.html'],
])('rejects %s', async (_description, apiUrl) => {
await expect(
startPlaygroundAPI({
iframe: createIframe(),
apiUrl,
})
).rejects.toThrow('Invalid API URL');
});
});
function createIframe() {
return {
src: '',
addEventListener: vi.fn((_event, callback: () => void) => {
callback();
}),
} as unknown as HTMLIFrameElement;
}
function createProgressTracker() {
return {
setCaption: vi.fn(),
finish: vi.fn(),
} as unknown as ProgressTracker;
}