-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17327 from evancharlton/useParameter-fallback
Core: Fix `useParameter` with nullish coalescing
- Loading branch information
Showing
2 changed files
with
58 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,57 @@ | ||
import { useParameter, useStoryContext } from './hooks'; | ||
|
||
const { window: globalWindow } = global; | ||
|
||
describe('addons/hooks', () => { | ||
beforeEach(() => { | ||
globalWindow.STORYBOOK_HOOKS_CONTEXT = undefined; | ||
}); | ||
|
||
afterEach(() => { | ||
globalWindow.STORYBOOK_HOOKS_CONTEXT = undefined; | ||
}); | ||
|
||
describe('useStoryContext', () => { | ||
test('should throw', () => { | ||
expect(() => useStoryContext()).toThrowError( | ||
'Storybook preview hooks can only be called inside decorators and story functions.' | ||
); | ||
}); | ||
}); | ||
|
||
describe('useParameter', () => { | ||
beforeEach(() => { | ||
globalWindow.STORYBOOK_HOOKS_CONTEXT = { | ||
currentContext: { | ||
parameters: { | ||
'undefined key': undefined, | ||
'null key': null, | ||
'false key': false, | ||
'zero key': 0, | ||
'object key': { defined: true }, | ||
}, | ||
}, | ||
}; | ||
}); | ||
|
||
test('undefined key', () => { | ||
expect(useParameter('undefined key', 'undefined default')).toEqual('undefined default'); | ||
}); | ||
|
||
test('null key', () => { | ||
expect(useParameter('null key', 'null default')).toEqual('null default'); | ||
}); | ||
|
||
test('false key', () => { | ||
expect(useParameter('false key', 'false default')).toEqual(false); | ||
}); | ||
|
||
test('zero key', () => { | ||
expect(useParameter('zero key', 'zero default')).toEqual(0); | ||
}); | ||
|
||
test('object key', () => { | ||
expect(useParameter('object key', 'object default')).toMatchObject({ defined: true }); | ||
}); | ||
}); | ||
}); |
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