From 09cac020e39f297d90da2e9ab6d98458ed09f775 Mon Sep 17 00:00:00 2001 From: Eric Kwoka <43540491+ekwoka@users.noreply.github.com> Date: Wed, 3 Jul 2024 01:31:11 +0400 Subject: [PATCH] :bug: Initializes Interceptors before store (#4278) * :test_tube: Adds failing tests for store interceptors * :white_check_mark: Initializes Interceptors before initializing store --- packages/alpinejs/src/store.js | 4 ++-- .../integration/plugins/persist.spec.js | 19 +++++++++++++++ tests/cypress/integration/store.spec.js | 23 +++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/packages/alpinejs/src/store.js b/packages/alpinejs/src/store.js index dc7bf94f5..60a44dcf5 100644 --- a/packages/alpinejs/src/store.js +++ b/packages/alpinejs/src/store.js @@ -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 } diff --git a/tests/cypress/integration/plugins/persist.spec.js b/tests/cypress/integration/plugins/persist.spec.js index 0fe1a6c28..32595fdea 100644 --- a/tests/cypress/integration/plugins/persist.spec.js +++ b/tests/cypress/integration/plugins/persist.spec.js @@ -227,6 +227,25 @@ test('can persist using global Alpine.$persist within Alpine.store', }, ) +test('persist in Stores is available in init call', + [html` +
+ +
+ `, ` + 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`
diff --git a/tests/cypress/integration/store.spec.js b/tests/cypress/integration/store.spec.js index bd7dc992b..fde31a3c3 100644 --- a/tests/cypress/integration/store.spec.js +++ b/tests/cypress/integration/store.spec.js @@ -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` +
+ +
+ `, + ` + Alpine.store('test', { + init() { + this.count++ + }, + count: { + _x_interceptor: true, + initialize() { + return 9 + } + }, + }) + `],({ get }) => { + get('span').should(haveText('10')) + } +) \ No newline at end of file