-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
store.spec.js
91 lines (86 loc) · 2.18 KB
/
store.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
import { haveText, html, test } from '../utils'
test('can register and use a global store',
[html`
<div x-data>
<span x-text="$store.test.foo"></span>
<button @click="$store.test.foo = 'baz'">clickme</button>
</div>
`,
`
Alpine.store('test', { foo: 'bar' })
`],
({ get }) => {
get('span').should(haveText('bar'))
get('button').click()
get('span').should(haveText('baz'))
}
)
test('store init function is called',
[html`
<div x-data>
<span x-text="$store.test.foo"></span>
<button @click="$store.test.foo = 'baz'">clickme</button>
</div>
`,
`
Alpine.store('test', { foo: 'bar', init() { this.foo = 'baz'} })
`],
({ get }) => {
get('span').should(haveText('baz'))
}
)
test('can use primitives as store',
[html`
<div x-data>
<span x-text="$store.test"></span>
<button @click="$store.test = 'baz'">clickme</button>
</div>
`,
`
Alpine.store('test', 'bar')
`],
({ get }) => {
get('span').should(haveText('bar'))
get('button').click()
get('span').should(haveText('baz'))
}
)
test('can use number as store',
[html`
<div x-data>
<span x-text="$store.test"></span>
<button @click="$store.test++">clickme</button>
</div>
`,
`
Alpine.store('test', 0)
`],
({ get }) => {
get('span').should(haveText('0'))
get('button').click()
get('span').should(haveText('1'))
}
)
test('store\'s "this" context is reactive for init function',
[html`
<div x-data>
<span x-text="$store.test.count"></span>
<button id="button">increment</button>
</div>
`,
`
Alpine.store('test', {
init() {
document.querySelector('#button').addEventListener('click', () => {
this.count++
})
},
count: 0,
})
`],
({ get }) => {
get('span').should(haveText('0'))
get('button').click()
get('span').should(haveText('1'))
}
)