-
Notifications
You must be signed in to change notification settings - Fork 26
/
localized-rich-text-input.visualspec.js
113 lines (85 loc) Β· 3.35 KB
/
localized-rich-text-input.visualspec.js
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
import { percySnapshot } from '@percy/puppeteer';
import { getDocument, queries, wait } from 'pptr-testing-library';
const { getByLabelText, getByTestId, getAllByLabelText, getByText } = queries;
describe('LocalizedRichTextInput', () => {
const blur = async element => {
// eslint-disable-next-line no-shadow
await page.evaluate(element => {
element.blur();
}, element);
};
const selectAllText = async input => {
// eslint-disable-next-line no-shadow
await page.evaluate(input => {
const range = document.createRange();
range.selectNodeContents(input);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
}, input);
};
const getNumberOfTags = async tagName => {
// eslint-disable-next-line no-shadow
const numberOfTags = await page.evaluate(tagName => {
return document.querySelectorAll(tagName).length;
}, tagName);
return numberOfTags;
};
it('Default', async () => {
await page.goto(`${HOST}/localized-rich-text-input`);
await expect(page).toMatch('minimal');
await percySnapshot(page, 'LocalizedRichTextInput');
});
it('Interactive', async () => {
await page.goto(`${HOST}/localized-rich-text-input/interactive`);
const doc = await getDocument(page);
let input = await getByTestId(doc, 'rich-text-data-test-en');
// make the text bold
let boldButton = await getByLabelText(doc, 'Bold');
await boldButton.click();
await input.type('Hello world');
await wait(() => getByText(doc, 'Hello world'));
// check that there is now a strong tag in the document.
let numOfTags = await getNumberOfTags('strong');
expect(numOfTags).toEqual(1);
// select the text
await selectAllText(input);
// click the bold button again
await boldButton.click();
// check there are no strong tags in the document.
numOfTags = await getNumberOfTags('strong');
expect(numOfTags).toEqual(0);
await input.press('Backspace');
const expandButton = await getByLabelText(doc, 'Show all languages (2)');
await expandButton.click();
const boldButtons = await getAllByLabelText(doc, 'Bold');
// there should be three bold buttons (3 inputs open)
expect(boldButtons.length).toBe(3);
// switch to german input
input = await getByTestId(doc, 'rich-text-data-test-de');
boldButton = boldButtons[1];
await boldButton.click();
await input.type('Hello world');
// check that there is now a strong tag in the document.
numOfTags = await getNumberOfTags('strong');
// now back to English
input = await getByTestId(doc, 'rich-text-data-test-en');
// start by removing all the text
await selectAllText(input);
await input.press('Backspace');
// next, open the Style menu
// blur input first to test that editor focus works correctly
await blur(input);
const styleMenuButtons = await getAllByLabelText(doc, 'Style');
const styleMenuButton = styleMenuButtons[0];
await styleMenuButton.click();
await wait(() => getByText(doc, 'Headline H1'));
await percySnapshot(page, 'LocalizedRichTextInput - style menu open');
// then click on the H1 button
const h1Button = await getByText(doc, 'Headline H1');
await h1Button.click();
// now type into the input
const h1Text = 'Hello World';
await input.type(h1Text);
});
});