-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use nullish coalescing operator in useParameter(..)
The `useParameter(..)` hook accepts a key and a default value. It then provides the default value if the current story has not provided a desired value for the relevant `parameters` object. This change updates the logic of when to fall back to the default. The old logic used a boolean operator (`||`), which worked as long as the provided value wasn't falsey. Instead, account for this by using the nullish coalescing operator (`??`).
- Loading branch information
1 parent
b82ecb5
commit 09d44bc
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