Skip to content

Commit

Permalink
🐛 Initializes Interceptors before store (#4278)
Browse files Browse the repository at this point in the history
* 🧪 Adds failing tests for store interceptors

* ✅ Initializes Interceptors before initializing store
  • Loading branch information
ekwoka authored Jul 2, 2024
1 parent ac63592 commit 09cac02
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/alpinejs/src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export function store(name, value) {

stores[name] = value

initInterceptors(stores[name])

if (typeof value === 'object' && value !== null && value.hasOwnProperty('init') && typeof value.init === 'function') {
stores[name].init()
}

initInterceptors(stores[name])
}

export function getStores() { return stores }
19 changes: 19 additions & 0 deletions tests/cypress/integration/plugins/persist.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,25 @@ test('can persist using global Alpine.$persist within Alpine.store',
},
)

test('persist in Stores is available in init call',
[html`
<div x-data>
<span x-text="$store.name.name"></span>
</div>
`, `
Alpine.store('name', {
firstName: Alpine.$persist('Daniel').as('dev-name'),
name: null,
init() {
this.name = String(this.firstName)
}
})
`],
({ get }) => {
get('span').should(haveText('Daniel'))
},
)

test('multiple aliases work when using global Alpine.$persist',
[html`
<div x-data>
Expand Down
23 changes: 23 additions & 0 deletions tests/cypress/integration/store.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,26 @@ test('store\'s "this" context is reactive for init function',
get('span').should(haveText('1'))
}
)

test('stores can have interceptors',
[html`
<div x-data>
<span x-text="$store.test.count"></span>
</div>
`,
`
Alpine.store('test', {
init() {
this.count++
},
count: {
_x_interceptor: true,
initialize() {
return 9
}
},
})
`],({ get }) => {
get('span').should(haveText('10'))
}
)

0 comments on commit 09cac02

Please sign in to comment.