-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathenrichedTextPress.spec.ts
More file actions
353 lines (291 loc) · 10.2 KB
/
Copy pathenrichedTextPress.spec.ts
File metadata and controls
353 lines (291 loc) · 10.2 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import { test, expect, type Page } from '@playwright/test';
test.setTimeout(90_000);
const IMAGE_ROUTE = '**/pw-e2e-ok.png';
const IMAGE_SRC = '/pw-e2e-ok.png';
const BROKEN_IMAGE_ROUTE = '**/pw-e2e-broken.png';
const BROKEN_IMAGE_SRC = '/pw-e2e-broken.png';
const PNG_BODY = Buffer.from(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==',
'base64'
);
const sel = {
root: '[data-testid="test-enriched-text-root"]',
htmlInput: '[data-testid="test-enriched-text-html-input"]',
setValueButton: '[data-testid="test-enriched-text-set-value-button"]',
valueOutput: '[data-testid="test-enriched-text-value-output"]',
display: '[data-testid="test-enriched-text-display"]',
displayInner: '[data-testid="test-enriched-text-display"] .et-view',
linkPressOutput: '[data-testid="test-enriched-text-link-press-output"]',
mentionPressOutput: '[data-testid="test-enriched-text-mention-press-output"]',
imagePressOutput: '[data-testid="test-enriched-text-image-press-output"]',
} as const;
const VISIBILITY_TIMEOUT_MS = 1_000;
test.beforeEach(async ({ page }) => {
await page.route(IMAGE_ROUTE, async (route) => {
await route.fulfill({
status: 200,
contentType: 'image/png',
body: PNG_BODY,
});
});
await page.route(BROKEN_IMAGE_ROUTE, (route) => route.abort());
});
async function gotoTestEnrichedText(page: Page): Promise<void> {
await page.goto('/test-enriched-text');
await page.waitForSelector(sel.displayInner);
}
async function setEnrichedTextValue(page: Page, html: string): Promise<void> {
await page.fill(sel.htmlInput, html);
await page.click(sel.setValueButton);
await expect
.poll(async () => (await page.locator(sel.valueOutput).textContent()) ?? '')
.toBe(html);
}
function linkPress(page: Page) {
return page.locator(sel.linkPressOutput);
}
function mentionPress(page: Page) {
return page.locator(sel.mentionPressOutput);
}
function imagePress(page: Page) {
return page.locator(sel.imagePressOutput);
}
test.describe('EnrichedText link press', () => {
test('pressing a link emits onLinkPress with the url', async ({ page }) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
'<html><p>Visit <a href="https://example.com">our site</a> now.</p></html>'
);
await expect(linkPress(page)).toHaveText('null');
await page.locator(`${sel.displayInner} a`).click();
await expect
.poll(async () =>
JSON.parse((await linkPress(page).textContent()) || 'null')
)
.toEqual({ url: 'https://example.com' });
});
test('pressing text styled inside a link still emits onLinkPress', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
'<html><p>A bold <a href="https://example.com/path">l<b>in</b>k</a> here.</p></html>'
);
await page.locator(`${sel.displayInner} a b`).click();
await expect
.poll(async () =>
JSON.parse((await linkPress(page).textContent()) || 'null')
)
.toEqual({ url: 'https://example.com/path' });
});
test('pressing non-link text does not emit onLinkPress', async ({ page }) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
'<html><p>Plain text with a <a href="https://example.com">link</a>.</p></html>'
);
await page
.locator(`${sel.displayInner} p`)
.click({ position: { x: 2, y: 2 } });
await expect(linkPress(page)).toHaveText('null');
});
});
test.describe('EnrichedText mention press', () => {
test('pressing a mention emits onMentionPress with text and indicator', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
'<html><p>Hello <mention indicator="@" text="@John Doe">@John Doe</mention>!</p></html>'
);
await expect(mentionPress(page)).toHaveText('null');
await page.locator(`${sel.displayInner} mention`).click();
await expect
.poll(async () =>
JSON.parse((await mentionPress(page).textContent()) || 'null')
)
.toEqual({ text: '@John Doe', indicator: '@', attributes: {} });
});
test('pressing text styled inside a mention still emits onMentionPress', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
'<html><p>Hi <mention indicator="@" text="@Jane">@Ja<s>ne</s></mention>.</p></html>'
);
await page.locator(`${sel.displayInner} mention s`).click();
await expect
.poll(async () =>
JSON.parse((await mentionPress(page).textContent()) || 'null')
)
.toEqual({ text: '@Jane', indicator: '@', attributes: {} });
});
test('pressing non-mention text does not emit onMentionPress', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
'<html><p>Some text <mention indicator="@" text="@Jane">@Jane</mention>.</p></html>'
);
await page
.locator(`${sel.displayInner} p`)
.click({ position: { x: 2, y: 2 } });
await expect(mentionPress(page)).toHaveText('null');
});
});
test.describe('EnrichedText image press', () => {
test('pressing an image emits onImagePress with the image', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
`<html><p>Look <img src="${IMAGE_SRC}" width="32" height="32" /> now.</p></html>`
);
await expect(imagePress(page)).toHaveText('null');
await page.locator(`${sel.displayInner} img`).click();
await expect
.poll(async () =>
JSON.parse((await imagePress(page).textContent()) || 'null')
)
.toEqual({ image: { uri: IMAGE_SRC, width: 32, height: 32 } });
});
test('pressing an image surrounded by styled text still emits onImagePress', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
`<html><p>A <b>bold</b> <img src="${IMAGE_SRC}" width="120" height="60" /> here.</p></html>`
);
await page.locator(`${sel.displayInner} img`).click();
await expect
.poll(async () =>
JSON.parse((await imagePress(page).textContent()) || 'null')
)
.toEqual({ image: { uri: IMAGE_SRC, width: 120, height: 60 } });
});
test('pressing non-image text does not emit onImagePress', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
`<html><p>Plain text with an <img src="${IMAGE_SRC}" width="32" height="32" />.</p></html>`
);
await page
.locator(`${sel.displayInner} p`)
.click({ position: { x: 2, y: 2 } });
await expect(imagePress(page)).toHaveText('null');
});
test('pressing an empty-src placeholder does not emit onImagePress', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
'<html><p>Before <img src="" width="40" height="40" /> after.</p></html>'
);
await expect(page.locator(`${sel.displayInner} img`)).toBeVisible({
timeout: VISIBILITY_TIMEOUT_MS,
});
await page.locator(`${sel.displayInner} img`).click();
await expect(imagePress(page)).toHaveText('null');
});
test('pressing a broken-URL placeholder does emit onImagePress', async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
`<html><p>Before <img src="${BROKEN_IMAGE_SRC}" width="40" height="40" /> after.</p></html>`
);
await expect(page.locator(`${sel.displayInner} img`)).toBeVisible({
timeout: VISIBILITY_TIMEOUT_MS,
});
await page.locator(`${sel.displayInner} img`).click();
await expect
.poll(async () =>
JSON.parse((await imagePress(page).textContent()) || 'null')
)
.toEqual({ image: { uri: BROKEN_IMAGE_SRC, width: 40, height: 40 } });
});
});
test.describe('EnrichedText press inside lists', () => {
const listCases = [
{
name: 'unordered list',
open: '<ul>',
close: '</ul>',
},
{
name: 'ordered list',
open: '<ol>',
close: '</ol>',
},
{
name: 'checkbox list',
open: '<ul data-type="checkbox">',
close: '</ul>',
},
];
for (const c of listCases) {
test(`pressing a link inside a ${c.name} emits onLinkPress`, async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
`<html>${c.open}<li>Visit <a href="https://example.com/list">our site</a></li><li>Other item</li>${c.close}</html>`
);
await expect(linkPress(page)).toHaveText('null');
await page.locator(`${sel.displayInner} li a`).click();
await expect
.poll(async () =>
JSON.parse((await linkPress(page).textContent()) || 'null')
)
.toEqual({ url: 'https://example.com/list' });
});
test(`pressing a mention inside a ${c.name} emits onMentionPress`, async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
`<html>${c.open}<li>Hello <mention indicator="@" text="@John Doe" id="1">@John Doe</mention></li><li>Other item</li>${c.close}</html>`
);
await expect(mentionPress(page)).toHaveText('null');
await page.locator(`${sel.displayInner} li mention`).click();
await expect
.poll(async () =>
JSON.parse((await mentionPress(page).textContent()) || 'null')
)
.toEqual({
text: '@John Doe',
indicator: '@',
attributes: { id: '1' },
});
});
test(`pressing an image inside a ${c.name} emits onImagePress`, async ({
page,
}) => {
await gotoTestEnrichedText(page);
await setEnrichedTextValue(
page,
`<html>${c.open}<li><p>See <img src="${IMAGE_SRC}" width="28" height="28" /> tiny</p></li><li><p>Other item</p></li>${c.close}</html>`
);
await expect(imagePress(page)).toHaveText('null');
await page.locator(`${sel.displayInner} li img`).click();
await expect
.poll(async () =>
JSON.parse((await imagePress(page).textContent()) || 'null')
)
.toEqual({ image: { uri: IMAGE_SRC, width: 28, height: 28 } });
});
}
});