Skip to content

Commit 09cac02

Browse files
authored
🐛 Initializes Interceptors before store (#4278)
* 🧪 Adds failing tests for store interceptors * ✅ Initializes Interceptors before initializing store
1 parent ac63592 commit 09cac02

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

packages/alpinejs/src/store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ export function store(name, value) {
1313

1414
stores[name] = value
1515

16+
initInterceptors(stores[name])
17+
1618
if (typeof value === 'object' && value !== null && value.hasOwnProperty('init') && typeof value.init === 'function') {
1719
stores[name].init()
1820
}
19-
20-
initInterceptors(stores[name])
2121
}
2222

2323
export function getStores() { return stores }

tests/cypress/integration/plugins/persist.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,25 @@ test('can persist using global Alpine.$persist within Alpine.store',
227227
},
228228
)
229229

230+
test('persist in Stores is available in init call',
231+
[html`
232+
<div x-data>
233+
<span x-text="$store.name.name"></span>
234+
</div>
235+
`, `
236+
Alpine.store('name', {
237+
firstName: Alpine.$persist('Daniel').as('dev-name'),
238+
name: null,
239+
init() {
240+
this.name = String(this.firstName)
241+
}
242+
})
243+
`],
244+
({ get }) => {
245+
get('span').should(haveText('Daniel'))
246+
},
247+
)
248+
230249
test('multiple aliases work when using global Alpine.$persist',
231250
[html`
232251
<div x-data>

tests/cypress/integration/store.spec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,26 @@ test('store\'s "this" context is reactive for init function',
8989
get('span').should(haveText('1'))
9090
}
9191
)
92+
93+
test('stores can have interceptors',
94+
[html`
95+
<div x-data>
96+
<span x-text="$store.test.count"></span>
97+
</div>
98+
`,
99+
`
100+
Alpine.store('test', {
101+
init() {
102+
this.count++
103+
},
104+
count: {
105+
_x_interceptor: true,
106+
initialize() {
107+
return 9
108+
}
109+
},
110+
})
111+
`],({ get }) => {
112+
get('span').should(haveText('10'))
113+
}
114+
)

0 commit comments

Comments
 (0)