Skip to content

Commit 145192a

Browse files
committed
test: use user instead of name
for some reason using a property called name in the state fails
1 parent a9ef6b6 commit 145192a

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

packages/pinia/__tests__/subscriptions.spec.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,22 @@ import { mount } from '@vue/test-utils'
33
import { nextTick } from 'vue'
44

55
describe('Subscriptions', () => {
6-
const useStore = () => {
7-
// create a new store
8-
setActivePinia(createPinia())
9-
return defineStore({
10-
id: 'main',
11-
state: () => ({
12-
name: 'Eduardo',
13-
}),
14-
})()
15-
}
6+
const useStore = defineStore({
7+
id: 'main',
8+
state: () => ({
9+
user: 'Eduardo',
10+
}),
11+
})
1612

17-
let store: ReturnType<typeof useStore>
1813
beforeEach(() => {
19-
store = useStore()
14+
setActivePinia(createPinia())
2015
})
2116

2217
it('fires callback when patch is applied', () => {
18+
const store = useStore()
2319
const spy = jest.fn()
2420
store.$subscribe(spy, { flush: 'sync' })
25-
store.$state.name = 'Cleiton'
21+
store.$state.user = 'Cleiton'
2622
expect(spy).toHaveBeenCalledTimes(1)
2723
expect(spy).toHaveBeenCalledWith(
2824
expect.objectContaining({
@@ -38,7 +34,7 @@ describe('Subscriptions', () => {
3834
const spy = jest.fn()
3935
store.$subscribe(spy, { flush: 'sync' })
4036

41-
const patch = { name: 'Cleiton' }
37+
const patch = { user: 'Cleiton' }
4238
store.$patch(patch)
4339

4440
expect(spy).toHaveBeenCalledWith(
@@ -53,34 +49,28 @@ describe('Subscriptions', () => {
5349

5450
it('unsubscribes callback when unsubscribe is called', () => {
5551
const spy = jest.fn()
52+
const store = useStore()
5653
const unsubscribe = store.$subscribe(spy, { flush: 'sync' })
5754
unsubscribe()
58-
store.$state.name = 'Cleiton'
55+
store.$state.user = 'Cleiton'
5956
expect(spy).not.toHaveBeenCalled()
6057
})
6158

6259
it('listeners are not affected when unsubscribe is called multiple times', () => {
6360
const func1 = jest.fn()
6461
const func2 = jest.fn()
62+
const store = useStore()
6563
const unsubscribe1 = store.$subscribe(func1, { flush: 'sync' })
6664
store.$subscribe(func2, { flush: 'sync' })
6765
unsubscribe1()
6866
unsubscribe1()
69-
store.$state.name = 'Cleiton'
67+
store.$state.user = 'Cleiton'
7068
expect(func1).not.toHaveBeenCalled()
7169
expect(func2).toHaveBeenCalledTimes(1)
7270
})
7371

7472
describe('multiple', () => {
75-
const useStore = defineStore({
76-
id: 'main',
77-
state: () => ({
78-
name: 'Eduardo',
79-
}),
80-
})
81-
8273
it('triggers subscribe only once', async () => {
83-
setActivePinia(createPinia())
8474
const s1 = useStore()
8575
const s2 = useStore()
8676

@@ -93,14 +83,15 @@ describe('Subscriptions', () => {
9383
expect(spy1).toHaveBeenCalledTimes(0)
9484
expect(spy2).toHaveBeenCalledTimes(0)
9585

96-
s1.name = 'Edu'
86+
s1.user = 'Edu'
9787

9888
expect(spy1).toHaveBeenCalledTimes(1)
9989
expect(spy2).toHaveBeenCalledTimes(1)
10090
})
10191

10292
it('removes on unmount', async () => {
10393
const pinia = createPinia()
94+
setActivePinia(pinia)
10495
const spy1 = jest.fn()
10596
const spy2 = jest.fn()
10697

@@ -123,41 +114,42 @@ describe('Subscriptions', () => {
123114
expect(spy1).toHaveBeenCalledTimes(0)
124115
expect(spy2).toHaveBeenCalledTimes(0)
125116

126-
s1.name = 'Edu'
117+
s1.user = 'Edu'
127118
expect(spy1).toHaveBeenCalledTimes(1)
128119
expect(spy2).toHaveBeenCalledTimes(1)
129120

130-
s1.$patch({ name: 'a' })
121+
s1.$patch({ user: 'a' })
131122
expect(spy1).toHaveBeenCalledTimes(2)
132123
expect(spy2).toHaveBeenCalledTimes(2)
133124

134125
s1.$patch((state) => {
135-
state.name = 'other'
126+
state.user = 'other'
136127
})
137128
expect(spy1).toHaveBeenCalledTimes(3)
138129
expect(spy2).toHaveBeenCalledTimes(3)
139130

140131
wrapper.unmount()
141132
await nextTick()
142133

143-
s1.$patch({ name: 'b' })
134+
s1.$patch({ user: 'b' })
144135
expect(spy1).toHaveBeenCalledTimes(3)
145136
expect(spy2).toHaveBeenCalledTimes(4)
146137
s1.$patch((state) => {
147-
state.name = 'c'
138+
state.user = 'c'
148139
})
149140
expect(spy1).toHaveBeenCalledTimes(3)
150141
expect(spy2).toHaveBeenCalledTimes(5)
151-
s1.name = 'd'
142+
s1.user = 'd'
152143
expect(spy1).toHaveBeenCalledTimes(3)
153144
expect(spy2).toHaveBeenCalledTimes(6)
154145
})
155146
})
156147

157148
it('subscribe is post by default', async () => {
158149
const spy = jest.fn()
150+
const store = useStore()
159151
store.$subscribe(spy)
160-
store.$state.name = 'Cleiton'
152+
store.$state.user = 'Cleiton'
161153
expect(spy).toHaveBeenCalledTimes(0)
162154
await nextTick()
163155
expect(spy).toHaveBeenCalledTimes(1)

packages/pinia/test-dts/state.test-d.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ expectType<{ msg: string }>(store.$state.aShallowRef)
7171
const onlyState = defineStore({
7272
id: 'main',
7373
state: () => ({
74-
counter: 0,
74+
// counter: 0,
75+
// TODO: having only name fails...
76+
name: 'hey',
77+
some: 'hello',
7578
}),
7679
})()
7780

78-
onlyState.$patch({ counter: 2 })
81+
onlyState.$patch({ some: 'other' })
7982
onlyState.$patch((state) => {
80-
expectType<number>(state.counter)
83+
expectType<string>(state.some)
84+
expectType<string>(state.name)
8185
})

0 commit comments

Comments
 (0)