Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.

Commit 844a680

Browse files
committed
added migration tests
1 parent 6debd02 commit 844a680

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

packages/core/src/state/state.persistent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ export class StatePersistent<ValueType = any> extends Persistent {
202202
if (config['storage'] == null || config.storage) {
203203
getSharedStorageManager()?.set(
204204
storageItemKey,
205-
this.onLoad != null
206-
? this.onLoad(this.state().getPersistableValue())
205+
this.onSave != null
206+
? this.onSave(this.state().getPersistableValue())
207207
: this.state().getPersistableValue(),
208208
this.storageKeys
209209
);

packages/core/tests/unit/state/state.persistent.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,34 @@ describe('StatePersistent Tests', () => {
224224
}
225225
);
226226

227+
it(
228+
"should load State value with Persistent key from the corresponding Storage, migrate it with the 'onMigrate()' method " +
229+
'and apply it to the State if the loading was successful',
230+
async () => {
231+
statePersistent.ready = true;
232+
storageManager.get = jest.fn(() =>
233+
Promise.resolve('dummyValue' as any)
234+
);
235+
statePersistent.onMigrate = jest.fn((value) => `migrated_${value}`);
236+
237+
const response = await statePersistent.loadPersistedValue();
238+
239+
expect(response).toBeTruthy();
240+
expect(storageManager.get).toHaveBeenCalledWith(
241+
statePersistent._key,
242+
statePersistent.config.defaultStorageKey
243+
);
244+
expect(dummyState.set).toHaveBeenCalledWith('migrated_dummyValue', {
245+
storage: false,
246+
overwrite: true,
247+
});
248+
expect(statePersistent.onMigrate).toHaveBeenCalledWith('dummyValue');
249+
expect(statePersistent.setupSideEffects).toHaveBeenCalledWith(
250+
statePersistent._key
251+
);
252+
}
253+
);
254+
227255
it(
228256
"shouldn't load State value with Persistent key from the corresponding Storage " +
229257
"and apply it to the State if the loading wasn't successful",
@@ -349,7 +377,9 @@ describe('StatePersistent Tests', () => {
349377
() => {
350378
statePersistent.setupSideEffects();
351379

352-
expect(dummyState.addSideEffect).toHaveBeenCalledWith(
380+
expect(
381+
dummyState.addSideEffect
382+
).toHaveBeenCalledWith(
353383
StatePersistent.storeValueSideEffectKey,
354384
expect.any(Function),
355385
{ weight: 0 }
@@ -502,6 +532,25 @@ describe('StatePersistent Tests', () => {
502532
);
503533
});
504534

535+
it(
536+
'should store current State value in the corresponding Storage ' +
537+
"and format it with the 'onSave()' method (default config)",
538+
() => {
539+
statePersistent.onSave = jest.fn((value) => `save_${value}`);
540+
541+
statePersistent.rebuildStorageSideEffect(dummyState, 'coolKey');
542+
543+
expect(storageManager.set).toHaveBeenCalledWith(
544+
'coolKey',
545+
`save_${dummyState.getPersistableValue()}`,
546+
statePersistent.storageKeys
547+
);
548+
expect(statePersistent.onSave).toHaveBeenCalledWith(
549+
dummyState.getPersistableValue()
550+
);
551+
}
552+
);
553+
505554
it("shouldn't store State value in the corresponding Storage (config.storage = false)", () => {
506555
statePersistent.rebuildStorageSideEffect(dummyState, 'coolKey', {
507556
storage: false,

0 commit comments

Comments
 (0)