Skip to content

Commit c7cd386

Browse files
authored
test(e2e): enhance and fix test of svg example (vuejs#560)
1 parent c463a71 commit c7cd386

File tree

3 files changed

+103
-18
lines changed

3 files changed

+103
-18
lines changed

packages/vue/examples/__tests__/e2eUtils.ts

+12
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ export function setupPuppeteer() {
7676
}
7777

7878
async function setValue(selector: string, value: string) {
79+
await page.$eval(
80+
selector,
81+
(node, value) => {
82+
(node as HTMLInputElement).value = value
83+
node.dispatchEvent(new Event('input'))
84+
},
85+
value
86+
)
87+
}
88+
89+
async function typeValue(selector: string, value: string) {
7990
const el = (await page.$(selector))!
8091
await el.evaluate(node => ((node as HTMLInputElement).value = ''))
8192
await el.type(value)
@@ -108,6 +119,7 @@ export function setupPuppeteer() {
108119
isChecked,
109120
isFocused,
110121
setValue,
122+
typeValue,
111123
enterValue,
112124
clearValue
113125
}

packages/vue/examples/__tests__/grid.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface TableData {
77
}
88

99
describe('e2e: grid', () => {
10-
const { page, click, text, count, setValue, clearValue } = setupPuppeteer()
10+
const { page, click, text, count, typeValue, clearValue } = setupPuppeteer()
1111
const columns = ['name', 'power'] as const
1212

1313
async function assertTable(data: TableData[]) {
@@ -88,18 +88,18 @@ describe('e2e: grid', () => {
8888
{ name: 'Jet Li', power: 8000 }
8989
])
9090

91-
await setValue('input[name="query"]', 'j')
91+
await typeValue('input[name="query"]', 'j')
9292
await assertTable([
9393
{ name: 'Jackie Chan', power: 7000 },
9494
{ name: 'Jet Li', power: 8000 }
9595
])
9696

97-
await setValue('input[name="query"]', 'infinity')
97+
await typeValue('input[name="query"]', 'infinity')
9898
await assertTable([{ name: 'Chuck Norris', power: Infinity }])
9999

100100
await clearValue('input[name="query"]')
101101
expect(await count('p')).toBe(0)
102-
await setValue('input[name="query"]', 'stringthatdoesnotexistanywhere')
102+
await typeValue('input[name="query"]', 'stringthatdoesnotexistanywhere')
103103
expect(await count('p')).toBe(1)
104104
}
105105

packages/vue/examples/__tests__/svg.spec.ts

+87-14
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,58 @@ declare function valueToPoint(
1616
}
1717

1818
describe('e2e: svg', () => {
19-
const { page, click, count, setValue } = setupPuppeteer()
19+
const { page, click, count, setValue, typeValue } = setupPuppeteer()
2020

21-
async function assertStats(total: number) {
22-
await page().evaluate(
21+
// assert the shape of the polygon is correct
22+
async function assertPolygon(total: number) {
23+
expect(
24+
await page().evaluate(
25+
total => {
26+
const points = globalStats
27+
.map((stat, i) => {
28+
const point = valueToPoint(stat.value, i, total)
29+
return point.x + ',' + point.y
30+
})
31+
.join(' ')
32+
return (
33+
document.querySelector('polygon')!.attributes[0].value === points
34+
)
35+
},
36+
[total]
37+
)
38+
).toBe(true)
39+
}
40+
41+
// assert the position of each label is correct
42+
async function assertLabels(total: number) {
43+
const positions = await page().evaluate(
2344
total => {
24-
const points = globalStats
25-
.map((stat, i) => {
26-
const point = valueToPoint(stat.value, i, total)
27-
return point.x + ',' + point.y
28-
})
29-
.join(' ')
30-
return document.querySelector('polygon')!.attributes[0].value === points
45+
return globalStats.map((stat, i) => {
46+
const point = valueToPoint(+stat.value + 10, i, total)
47+
return [point.x, point.y]
48+
})
3149
},
3250
[total]
3351
)
52+
for (let i = 0; i < total; i++) {
53+
const textPosition = await page().$eval(
54+
`text:nth-child(${i + 3})`,
55+
node => [+node.attributes[0].value, +node.attributes[1].value]
56+
)
57+
expect(textPosition).toEqual(positions[i])
58+
}
59+
}
60+
61+
// assert each value of stats is correct
62+
async function assertStats(expected: number[]) {
63+
const statsValue = await page().evaluate(() => {
64+
return globalStats.map(stat => +stat.value)
65+
})
66+
expect(statsValue).toEqual(expected)
67+
}
68+
69+
function nthRange(n: number) {
70+
return `#demo div:nth-child(${n + 1}) input[type="range"]`
3471
}
3572

3673
async function testSvg(apiType: 'classic' | 'composition') {
@@ -48,22 +85,58 @@ describe('e2e: svg', () => {
4885
expect(await count('label')).toBe(6)
4986
expect(await count('button')).toBe(7)
5087
expect(await count('input[type="range"]')).toBe(6)
51-
await assertStats(6)
88+
await assertPolygon(6)
89+
await assertLabels(6)
90+
await assertStats([100, 100, 100, 100, 100, 100])
91+
92+
await setValue(nthRange(1), '10')
93+
await assertPolygon(6)
94+
await assertLabels(6)
95+
await assertStats([10, 100, 100, 100, 100, 100])
5296

5397
await click('button.remove')
5498
expect(await count('text')).toBe(5)
5599
expect(await count('label')).toBe(5)
56100
expect(await count('button')).toBe(6)
57101
expect(await count('input[type="range"]')).toBe(5)
58-
await assertStats(5)
102+
await assertPolygon(5)
103+
await assertLabels(5)
104+
await assertStats([100, 100, 100, 100, 100])
59105

60-
await setValue('input[name="newlabel"]', 'foo')
106+
await typeValue('input[name="newlabel"]', 'foo')
61107
await click('#add > button')
62108
expect(await count('text')).toBe(6)
63109
expect(await count('label')).toBe(6)
64110
expect(await count('button')).toBe(7)
65111
expect(await count('input[type="range"]')).toBe(6)
66-
await assertStats(6)
112+
await assertPolygon(6)
113+
await assertLabels(6)
114+
await assertStats([100, 100, 100, 100, 100, 100])
115+
116+
await setValue(nthRange(1), '10')
117+
await assertPolygon(6)
118+
await assertLabels(6)
119+
await assertStats([10, 100, 100, 100, 100, 100])
120+
121+
await setValue(nthRange(2), '20')
122+
await assertPolygon(6)
123+
await assertLabels(6)
124+
await assertStats([10, 20, 100, 100, 100, 100])
125+
126+
await setValue(nthRange(6), '60')
127+
await assertPolygon(6)
128+
await assertLabels(6)
129+
await assertStats([10, 20, 100, 100, 100, 60])
130+
131+
await click('button.remove')
132+
await assertPolygon(5)
133+
await assertLabels(5)
134+
await assertStats([20, 100, 100, 100, 60])
135+
136+
await setValue(nthRange(1), '10')
137+
await assertPolygon(5)
138+
await assertLabels(5)
139+
await assertStats([10, 100, 100, 100, 60])
67140
}
68141

69142
test(

0 commit comments

Comments
 (0)