@webitel/ui-datalist 1.1.69 → 1.1.70

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@webitel/ui-datalist",
3
- "version": "1.1.69",
3
+ "version": "1.1.70",
4
4
  "description": "Toolkit for building data lists in webitel ui system",
5
5
  "scripts": {
6
6
  "build:types": "vue-tsc -b tsconfig.build.json",
@@ -0,0 +1,58 @@
1
+ import { createPinia, setActivePinia } from 'pinia';
2
+ import { beforeEach, describe, expect, it } from 'vitest';
3
+
4
+ import { createFilterPresetsStore } from '../createFilterPresetsStore';
5
+
6
+ const NAMESPACE = 'test';
7
+ const STORAGE_KEY = `${NAMESPACE}/presets/preset`;
8
+ const CACHED_PRESET_ID = 42;
9
+
10
+ const setupStore = async () => {
11
+ const store = createFilterPresetsStore(NAMESPACE)();
12
+ await store.setupPresetPersistence();
13
+
14
+ return store;
15
+ };
16
+
17
+ describe('filter presets persistence', () => {
18
+ beforeEach(() => {
19
+ setActivePinia(createPinia());
20
+ localStorage.clear();
21
+ });
22
+
23
+ it('restores the cached preset id', async () => {
24
+ localStorage.setItem(STORAGE_KEY, String(CACHED_PRESET_ID));
25
+
26
+ const store = await setupStore();
27
+
28
+ expect(store.presetId).toBe(CACHED_PRESET_ID);
29
+ });
30
+
31
+ /*
32
+ restore() snapshots the value through onStore before reading the storages,
33
+ so an onStore that clears used to delete the cached preset on every setup
34
+ */
35
+ it('keeps the cached preset id stored while restoring it', async () => {
36
+ localStorage.setItem(STORAGE_KEY, String(CACHED_PRESET_ID));
37
+
38
+ await setupStore();
39
+
40
+ expect(localStorage.getItem(STORAGE_KEY)).toBe(String(CACHED_PRESET_ID));
41
+ });
42
+
43
+ it('leaves presetId empty when nothing is cached, instead of NaN', async () => {
44
+ const store = await setupStore();
45
+
46
+ expect(store.presetId).toBeNull();
47
+ });
48
+
49
+ it('drops the cached preset on resetPreset', async () => {
50
+ localStorage.setItem(STORAGE_KEY, String(CACHED_PRESET_ID));
51
+ const store = await setupStore();
52
+
53
+ await store.resetPreset();
54
+
55
+ expect(store.presetId).toBeNull();
56
+ expect(localStorage.getItem(STORAGE_KEY)).toBeNull();
57
+ });
58
+ });
@@ -28,6 +28,8 @@ export const filterPresetsStoreBody = (namespace = 'presets') => {
28
28
 
29
29
  const presetId = ref<number | null>(null);
30
30
 
31
+ let resetPersistedPreset: (() => Promise<void>) | null = null;
32
+
31
33
  const setupPresetPersistence = async () => {
32
34
  const { restore: restorePreset, reset } = usePersistedStorage({
33
35
  name: 'preset',
@@ -36,11 +38,15 @@ export const filterPresetsStoreBody = (namespace = 'presets') => {
36
38
  PersistedStorageType.LocalStorage,
37
39
  ],
38
40
  storagePath: presetsNamespace,
39
- onStore: (save, { name }) => {
41
+ /*
42
+ side-effect free on purpose: serialize() runs this path merely to snapshot
43
+ the value, so clearing the storage from here wipes the cached preset
44
+ before restore() gets to read it
45
+ */
46
+ onStore: async (save, { name }) => {
40
47
  const value = presetId.value;
41
- if (!value) {
42
- return reset();
43
- }
48
+ if (!value) return;
49
+
44
50
  return save({
45
51
  name,
46
52
  value,
@@ -48,9 +54,13 @@ export const filterPresetsStoreBody = (namespace = 'presets') => {
48
54
  },
49
55
  onRestore: async (restore, name) => {
50
56
  const value = await restore(name);
51
- presetId.value = Number(value);
57
+ /* absent key resolves undefined, and Number(undefined) is NaN */
58
+ if (value) presetId.value = Number(value);
52
59
  },
53
60
  });
61
+
62
+ resetPersistedPreset = reset;
63
+
54
64
  await restorePreset();
55
65
  };
56
66
 
@@ -59,8 +69,10 @@ export const filterPresetsStoreBody = (namespace = 'presets') => {
59
69
  presetsTableConfig,
60
70
  );
61
71
 
62
- const resetPreset = () => {
72
+ /* onStore no longer clears, so dropping the cached preset is explicit */
73
+ const resetPreset = async () => {
63
74
  presetId.value = null;
75
+ await resetPersistedPreset?.();
64
76
  };
65
77
 
66
78
  return {
@@ -36,6 +36,10 @@ export interface PersistedPropertyConfig {
36
36
  storagePath?: string;
37
37
  startWatchManually?: boolean;
38
38
  watchConfig?: WatchOptions;
39
+ // must be side-effect free apart from the `save` it is handed: serialize()
40
+ // runs this path with a `save` that only captures, so touching the storages
41
+ // from here mutates them at snapshot time – including before restore() has
42
+ // read them
39
43
  onStore?: (
40
44
  save: ({
41
45
  name,