forked from vuejs/vue-web-component-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
145 lines (119 loc) · 4.15 KB
/
test.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
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
/* global test expect el els */
const launchPage = require('./setup')
test('properties', async () => {
const { page } = await launchPage(`properties`)
// props proxying: get
const foo = await page.evaluate(() => el.foo)
expect(foo).toBe(123)
// get camelCase
const someProp = await page.evaluate(() => el.someProp)
expect(someProp).toBe('bar')
// props proxying: set
await page.evaluate(() => {
el.foo = 234
el.someProp = 'lol'
})
const newFoo = await page.evaluate(() => el.vueComponent.foo)
expect(newFoo).toBe(234)
const newBar = await page.evaluate(() => el.vueComponent.someProp)
expect(newBar).toBe('lol')
})
test('attributes', async () => {
const { page } = await launchPage(`attributes`)
// boolean
const foo = await page.evaluate(() => el.foo)
expect(foo).toBe(true)
// boolean="true"
const bar = await page.evaluate(() => el.bar)
expect(bar).toBe(true)
// absence of boolean with default: true
const baz = await page.evaluate(() => el.baz)
expect(baz).toBe(true)
// boolean="false" with default: true
const qux = await page.evaluate(() => el.qux)
expect(qux).toBe(false)
// some-number="123"
const someNumber = await page.evaluate(() => el.someNumber)
expect(someNumber).toBe(123)
// set via attribute
await page.evaluate(() => {
el.setAttribute('foo', 'foo')
el.setAttribute('bar', 'false')
el.setAttribute('baz', 'false')
el.setAttribute('qux', '')
el.setAttribute('some-number', '234')
})
// boolean="boolean"
expect(await page.evaluate(() => el.foo)).toBe(true)
expect(await page.evaluate(() => el.bar)).toBe(false)
expect(await page.evaluate(() => el.baz)).toBe(false)
expect(await page.evaluate(() => el.qux)).toBe(true)
expect(await page.evaluate(() => el.someNumber)).toBe(234)
})
test('events', async () => {
const { page } = await launchPage(`events`)
await page.evaluate(() => {
el.shadowRoot.querySelector('button').click()
})
expect(await page.evaluate(() => window.emitted)).toBe(true)
expect(await page.evaluate(() => window.emittedDetail)).toEqual([123])
})
test('slots', async () => {
const { page } = await launchPage(`slots`)
const content = await page.evaluate(() => {
return el.shadowRoot.querySelector('div').innerHTML
})
expect(content).toMatch(`<div>default</div><div>foo</div>`)
// update slots
await page.evaluate(() => {
el.innerHTML = `<div>default2</div><div slot="foo">foo2</div>`
})
const newContent = await page.evaluate(() => {
return el.shadowRoot.querySelector('div').innerHTML
})
expect(newContent).toMatch(`<div>default2</div><div>foo2</div>`)
})
test('lifecycle', async () => {
const { page, logs } = await launchPage(`lifecycle`)
expect(logs).toContain('created')
expect(logs).toContain('mounted')
await page.evaluate(() => {
el.parentNode.removeChild(el)
})
expect(logs).toContain('deactivated')
await page.evaluate(() => {
document.body.appendChild(el)
})
expect(logs).toContain('activated')
})
test('async', async () => {
const { page } = await launchPage(`async`)
// should not be ready yet
expect(await page.evaluate(() => els[0].shadowRoot.querySelector('div'))).toBe(null)
expect(await page.evaluate(() => els[1].shadowRoot.querySelector('div'))).toBe(null)
// wait until component is resolved
await new Promise(resolve => {
page.on('console', msg => {
if (msg.text() === 'resolved') {
resolve()
}
})
})
// both instances should be initialized
expect(await page.evaluate(() => els[0].shadowRoot.textContent)).toMatch(`123 bar`)
expect(await page.evaluate(() => els[1].shadowRoot.textContent)).toMatch(`234 baz`)
// attribute sync should work
await page.evaluate(() => {
els[0].setAttribute('foo', '345')
})
expect(await page.evaluate(() => els[0].shadowRoot.textContent)).toMatch(`345 bar`)
// new instance should work
await page.evaluate(() => {
const newEl = document.createElement('my-element')
newEl.setAttribute('foo', '456')
document.body.appendChild(newEl)
})
expect(await page.evaluate(() => {
return document.querySelectorAll('my-element')[2].shadowRoot.textContent
})).toMatch(`456 bar`)
})