forked from alpinejs/alpine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbind.spec.js
141 lines (110 loc) · 4.26 KB
/
bind.spec.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
import Alpine from 'alpinejs'
global.MutationObserver = class {
observe() {}
}
test('attribute bindings are set on initialize', async () => {
document.body.innerHTML = `
<div x-data="{ foo: 'bar' }">
<span x-bind:foo="foo"></span>
</div>
`
Alpine.start()
expect(document.querySelector('span').getAttribute('foo')).toEqual('bar')
})
test('class attribute bindings are removed by object syntax', async () => {
document.body.innerHTML = `
<div x-data="{ isOn: false }">
<span class="foo" x-bind:class="{ 'foo': isOn }"></span>
</div>
`
Alpine.start()
expect(document.querySelector('span').classList.contains('foo')).toBeFalsy()
})
test('class attribute bindings are added by object syntax', async () => {
document.body.innerHTML = `
<div x-data="{ isOn: true }">
<span x-bind:class="{ 'foo': isOn }"></span>
</div>
`
Alpine.start()
expect(document.querySelector('span').classList.contains('foo')).toBeTruthy()
})
test('multiple classes are added by object syntax', async () => {
document.body.innerHTML = `
<div x-data="{ isOn: false }">
<span class="foo bar" x-bind:class="{ 'foo bar': isOn }"></span>
</div>
`
Alpine.start()
expect(document.querySelector('span').classList.contains('foo')).toBeFalsy()
expect(document.querySelector('span').classList.contains('bar')).toBeFalsy()
})
test('multiple classes are removed by object syntax', async () => {
document.body.innerHTML = `
<div x-data="{ isOn: true }">
<span x-bind:class="{ 'foo bar': isOn }"></span>
</div>
`
Alpine.start()
expect(document.querySelector('span').classList.contains('foo')).toBeTruthy()
expect(document.querySelector('span').classList.contains('bar')).toBeTruthy()
})
test('class attribute bindings are added by nested object syntax', async () => {
document.body.innerHTML = `
<div x-data="{ nested: { isOn: true } }">
<span x-bind:class="{ 'foo': nested.isOn }"></span>
</div>
`
Alpine.start()
expect(document.querySelector('span').classList.contains('foo')).toBeTruthy()
})
test('class attribute bindings are removed by array syntax', async () => {
document.body.innerHTML = `
<div x-data="{}">
<span class="foo" x-bind:class="[]"></span>
</div>
`
Alpine.start()
expect(document.querySelector('span').classList.contains('foo')).toBeFalsy()
})
test('class attribute bindings are added by array syntax', async () => {
document.body.innerHTML = `
<div x-data="{}">
<span class="" x-bind:class="['foo']"></span>
</div>
`
Alpine.start()
expect(document.querySelector('span').classList.contains('foo')).toBeTruthy
})
test('boolean attributes set to false are removed from element', async () => {
document.body.innerHTML = `
<div x-data="{ isSet: false }">
<input x-bind:disabled="isSet"></input>
<input x-bind:checked="isSet"></input>
<input x-bind:required="isSet"></input>
<input x-bind:readonly="isSet"></input>
<input x-bind:hidden="isSet"></input>
</div>
`
Alpine.start()
expect(document.querySelectorAll('input')[0].disabled).toBeFalsy()
expect(document.querySelectorAll('input')[1].checked).toBeFalsy()
expect(document.querySelectorAll('input')[2].required).toBeFalsy()
expect(document.querySelectorAll('input')[3].readOnly).toBeFalsy()
expect(document.querySelectorAll('input')[4].hidden).toBeFalsy()
})
test('boolean attributes set to true are added to element', async () => {
document.body.innerHTML = `
<div x-data="{ isSet: true }">
<input x-bind:disabled="isSet"></input>
<input x-bind:checked="isSet"></input>
<input x-bind:required="isSet"></input>
<input x-bind:readonly="isSet"></input>
</div>
`
Alpine.start()
expect(document.querySelectorAll('input')[0].disabled).toBeTruthy()
expect(document.querySelectorAll('input')[1].checked).toBeTruthy()
expect(document.querySelectorAll('input')[2].required).toBeTruthy()
expect(document.querySelectorAll('input')[3].readOnly).toBeTruthy()
})