11import { configurePersistedStore } from "../src" ;
22import { TestSettings as Settings } from '../src/settings' ;
3- import { mockPersistedSlice , mockStorageHandler } from "./mocks" ;
3+ import { StorageHandler } from "../src/types" ;
4+ import { mockPersistedSlice , StorageMock } from "./mocks" ;
45
5- describe ( 'Global settings' , ( ) => {
6+ describe ( 'Settings' , ( ) => {
7+ let storage : StorageHandler ;
8+
9+ // Before each test, create a new storage mock and clear all global settings
10+ // to ensure tests are isolated.
611 beforeEach ( ( ) => {
12+ storage = new StorageMock ( ) ;
713 Settings . _clearSettings ( ) ;
814 } ) ;
9- it ( 'should allow to get and set static variables' , ( ) => {
10- Settings . storageHandler = mockStorageHandler ;
11- expect ( Settings . storageHandler ) . toEqual ( mockStorageHandler ) ;
15+
16+ describe ( 'storageHandler' , ( ) => {
17+ it ( 'should allow setting and getting the storage handler' , ( ) => {
18+ Settings . storageHandler = storage ;
19+ expect ( Settings . storageHandler ) . toBe ( storage ) ;
20+ } ) ;
21+
22+ it ( 'should be initialized by configurePersistedStore' , async ( ) => {
23+ await configurePersistedStore ( {
24+ reducer : ( { [ mockPersistedSlice . name ] : mockPersistedSlice . reducer } ) ,
25+ } , 'mockApp' , storage ) ;
26+ expect ( Settings . storageHandler ) . toBe ( storage ) ;
27+ } ) ;
28+
29+ it ( 'should throw a TypeError if accessed before being set' , ( ) => {
30+ expect ( ( ) => Settings . storageHandler ) . toThrow ( TypeError ) ;
31+ } ) ;
1232 } ) ;
1333
14- it ( 'should be initiated when configuring a store' , async ( ) => {
15- await configurePersistedStore ( {
16- reducer : ( { [ mockPersistedSlice . name ] : mockPersistedSlice . reducer } ) ,
17- } , 'mock' , mockStorageHandler ) ;
18- expect ( Settings . storageHandler ) . toEqual ( mockStorageHandler ) ;
34+ describe ( 'applicationId' , ( ) => {
35+ it ( 'should allow setting and getting the application ID' , ( ) => {
36+ Settings . applicationId = 'my-app' ;
37+ expect ( Settings . applicationId ) . toBe ( 'my-app' ) ;
38+ } ) ;
39+
40+ it ( 'should be initialized by configurePersistedStore' , async ( ) => {
41+ await configurePersistedStore ( {
42+ reducer : ( { [ mockPersistedSlice . name ] : mockPersistedSlice . reducer } ) ,
43+ } , 'mockApp' , storage ) ;
44+ expect ( Settings . applicationId ) . toBe ( 'mockApp' ) ;
45+ } ) ;
46+
47+ it ( 'should throw a TypeError if accessed before being set' , ( ) => {
48+ expect ( ( ) => Settings . applicationId ) . toThrow ( TypeError ) ;
49+ } ) ;
1950 } ) ;
2051
21- it ( 'should throw an exception when requesting variables not set' , ( ) => {
22- expect ( ( ) => Settings . storageHandler ) . toThrow ( TypeError ) ;
52+ describe ( 'slice subscription' , ( ) => {
53+ it ( 'should allow subscribing slices and retrieving the list' , ( ) => {
54+ Settings . subscribeSlice ( 'user' ) ;
55+ Settings . subscribeSlice ( 'posts' ) ;
56+ expect ( Settings . subscribedSliceIds ) . toEqual ( [ 'user' , 'posts' ] ) ;
57+ } ) ;
58+
59+ it ( 'should not allow duplicate slice subscriptions' , ( ) => {
60+ Settings . subscribeSlice ( 'user' ) ;
61+ Settings . subscribeSlice ( 'user' ) ;
62+ Settings . subscribeSlice ( 'posts' ) ;
63+ expect ( Settings . subscribedSliceIds ) . toEqual ( [ 'user' , 'posts' ] ) ;
64+ } ) ;
65+
66+ it ( 'should be cleared by _clearSettings' , ( ) => {
67+ Settings . subscribeSlice ( 'user' ) ;
68+ expect ( Settings . subscribedSliceIds ) . toEqual ( [ 'user' ] ) ;
69+ Settings . _clearSettings ( ) ;
70+ expect ( Settings . subscribedSliceIds ) . toEqual ( [ ] ) ;
71+ } ) ;
72+ } ) ;
73+
74+ describe ( 'persistence pause and resume' , ( ) => {
75+ it ( 'should be enabled by default' , ( ) => {
76+ expect ( Settings . isPersistenceEnabled ) . toBe ( true ) ;
77+ } ) ;
78+
79+ it ( 'should allow pausing persistence' , ( ) => {
80+ Settings . pause ( ) ;
81+ expect ( Settings . isPersistenceEnabled ) . toBe ( false ) ;
82+ } ) ;
83+
84+ it ( 'should allow resuming persistence after being paused' , ( ) => {
85+ Settings . pause ( ) ;
86+ expect ( Settings . isPersistenceEnabled ) . toBe ( false ) ;
87+ Settings . resume ( ) ;
88+ expect ( Settings . isPersistenceEnabled ) . toBe ( true ) ;
89+ } ) ;
90+
91+ it ( 'should be reset to enabled when settings are cleared' , ( ) => {
92+ Settings . pause ( ) ;
93+ expect ( Settings . isPersistenceEnabled ) . toBe ( false ) ;
94+ Settings . _clearSettings ( ) ;
95+ expect ( Settings . isPersistenceEnabled ) . toBe ( true ) ;
96+ } ) ;
2397 } ) ;
24- } ) ;
98+ } ) ;
0 commit comments