forked from salesforce/akita
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store,storeconfig): config for deepFreeze function
Adds deepFreezeFunction in StoreConfigOptions and unit tests for feature Can use custom deepFreeze for complex objects in store Resolves salesforce#124
- Loading branch information
1 parent
eea3106
commit 1d7535c
Showing
3 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Store } from '../src/store'; | ||
import { StoreConfig } from '../src/storeConfig'; | ||
import { deepFreeze } from '../src/deepFreeze'; | ||
|
||
class SpecialObject { | ||
specialString: string = 'special'; | ||
specialNumber: number = 2; | ||
|
||
constructor(params: Partial<ComplexState>) { | ||
Object.assign(this, params); | ||
} | ||
} | ||
|
||
class ComplexState { | ||
propertyString: string = ''; | ||
propertyNumber: number = 1; | ||
specialObject: SpecialObject = new SpecialObject({}); | ||
|
||
constructor(params: Partial<ComplexState>) { | ||
Object.assign(this, params); | ||
} | ||
} | ||
|
||
@StoreConfig({ | ||
name: 'complexState' | ||
}) | ||
class ComplexStore extends Store<ComplexState> { | ||
constructor() { | ||
super(new ComplexState({})); | ||
} | ||
} | ||
|
||
const complexStore = new ComplexStore(); | ||
|
||
describe('store with no custom deepFreeze', () => { | ||
it('should use default function', () => { | ||
expect(complexStore.deepFreeze).toEqual(deepFreeze); | ||
}); | ||
|
||
it('should freeze all properties', () => { | ||
expect(Object.isFrozen(complexStore._value().propertyNumber)).toBeTruthy(); | ||
expect(Object.isFrozen(complexStore._value().propertyString)).toBeTruthy(); | ||
expect(Object.isFrozen(complexStore._value().specialObject)).toBeTruthy(); | ||
expect(Object.isFrozen(complexStore._value().specialObject.specialNumber)).toBeTruthy(); | ||
expect(Object.isFrozen(complexStore._value().specialObject.specialString)).toBeTruthy(); | ||
}); | ||
}); | ||
|
||
function deepFreezeCustom(o: ComplexState) { | ||
Object.freeze(o); | ||
|
||
return o; | ||
} | ||
|
||
@StoreConfig({ | ||
name: 'complexState2', | ||
deepFreezeFunction: deepFreezeCustom | ||
}) | ||
class ComplexStoreCustom extends Store<ComplexState> { | ||
constructor() { | ||
super(new ComplexState({})); | ||
} | ||
} | ||
|
||
const complexStoreCustom = new ComplexStoreCustom(); | ||
|
||
describe('store with custom deepFreeze', () => { | ||
it('should use custom function', () => { | ||
expect(complexStoreCustom.deepFreeze).toEqual(deepFreezeCustom); | ||
}); | ||
|
||
it('should not freeze all properties', () => { | ||
expect(Object.isFrozen(complexStoreCustom._value().propertyNumber)).toBeTruthy(); | ||
expect(Object.isFrozen(complexStoreCustom._value().propertyString)).toBeTruthy(); | ||
expect(Object.isFrozen(complexStoreCustom._value().specialObject)).toBeFalsy(); | ||
expect(Object.isFrozen(complexStoreCustom._value().specialObject.specialNumber)).toBeTruthy(); | ||
expect(Object.isFrozen(complexStoreCustom._value().specialObject.specialString)).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters