File tree Expand file tree Collapse file tree 2 files changed +9
-1
lines changed
Expand file tree Collapse file tree 2 files changed +9
-1
lines changed Original file line number Diff line number Diff line change 11/**
22 * @param app app ID, e.g. "mail"
33 * @param key name of the property
4+ * @param fallback optional parameter to use as default value
45 * @throws if the key can't be found
56 */
6- export function loadState < T > ( app : string , key : string ) : T {
7+ export function loadState < T > ( app : string , key : string , fallback ?: T ) : T {
78 const elem = < HTMLInputElement > document . querySelector ( `#initial-state-${ app } -${ key } ` )
89 if ( elem === null ) {
10+ if ( fallback !== undefined ) {
11+ return fallback
12+ }
913 throw new Error ( `Could not find initial state ${ key } of ${ app } ` )
1014 }
1115
Original file line number Diff line number Diff line change @@ -4,6 +4,10 @@ test('throw if nothing found', () => {
44 expect ( ( ) => loadState ( 'test' , 'key' ) ) . toThrow ( new Error ( "Could not find initial state key of test" ) )
55} )
66
7+ test ( 'return default if provided' , ( ) => {
8+ expect ( loadState ( 'test' , 'key' , 'default' ) ) . toBe ( 'default' )
9+ } )
10+
711test ( 'find correct value' , ( ) => {
812 const input = document . createElement ( 'input' )
913 input . setAttribute ( 'type' , 'hidden' )
You can’t perform that action at this time.
0 commit comments