-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add useMutableCallback hook * Apply lint on tests * Replace testHook with runHooks
- Loading branch information
Showing
14 changed files
with
211 additions
and
226 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// @flow | ||
|
||
import { useCallback, useRef } from 'react'; | ||
|
||
/** | ||
* Hook to create a stable callback from a mutable one. | ||
* | ||
* @param fn the mutable callback | ||
* @return a stable callback | ||
*/ | ||
export const useMutableCallback = (fn: (...args : any[]) => any) => { | ||
const fnRef = useRef(fn); | ||
fnRef.current = fn; | ||
|
||
return useCallback((...args: any[]) => fnRef.current && (0, fnRef.current)(...args), []); | ||
}; |
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 |
---|---|---|
@@ -1,42 +1,42 @@ | ||
import { testHook } from '../.jest/helpers'; | ||
import { runHooks } from '../.jest/helpers'; | ||
import { useClassName } from '../src'; | ||
|
||
describe('useClassName hook', () => { | ||
const componentClassName = 'component'; | ||
|
||
it('accepts only the component className', () => { | ||
const newClassName = testHook(() => useClassName(componentClassName)); | ||
const [newClassName] = runHooks(() => useClassName(componentClassName)); | ||
expect(newClassName).toEqual(componentClassName); | ||
}); | ||
|
||
it('composes with a true-valued boolean modifier', () => { | ||
const newClassName = testHook(() => useClassName(componentClassName, { a: true })); | ||
const [newClassName] = runHooks(() => useClassName(componentClassName, { a: true })); | ||
expect(newClassName).toEqual(`${ componentClassName } ${ componentClassName }--a`); | ||
}); | ||
|
||
it('does not compose with a false-valued boolean modifier', () => { | ||
const newClassName = testHook(() => useClassName(componentClassName, { a: false })); | ||
const [newClassName] = runHooks(() => useClassName(componentClassName, { a: false })); | ||
expect(newClassName).toEqual(componentClassName); | ||
}); | ||
|
||
it('composes with a non-boolean modifier', () => { | ||
const newClassName = testHook(() => useClassName(componentClassName, { a: 'b' })); | ||
const [newClassName] = runHooks(() => useClassName(componentClassName, { a: 'b' })); | ||
expect(newClassName).toEqual(`${ componentClassName } ${ componentClassName }--a-b`); | ||
}); | ||
|
||
it('appends an arbitrary amount of additional classNames', () => { | ||
const classNames = new Array(5).fill(undefined).map((i) => `class-${ i }`); | ||
const newClassName = testHook(() => useClassName(componentClassName, {}, ...classNames)); | ||
const [newClassName] = runHooks(() => useClassName(componentClassName, {}, ...classNames)); | ||
expect(newClassName).toEqual(`${ componentClassName } ${ classNames.join(' ') }`); | ||
}); | ||
|
||
it('formats a modifier name from camelCase to kebab-case', () => { | ||
const newClassName = testHook(() => useClassName(componentClassName, { camelCaseModifier: true })); | ||
const [newClassName] = runHooks(() => useClassName(componentClassName, { camelCaseModifier: true })); | ||
expect(newClassName).toEqual(`${ componentClassName } ${ componentClassName }--camel-case-modifier`); | ||
}); | ||
|
||
it('formats a modifier value from camelCase to kebab-case', () => { | ||
const newClassName = testHook(() => useClassName(componentClassName, { a: 'camelCaseValue' })); | ||
const [newClassName] = runHooks(() => useClassName(componentClassName, { a: 'camelCaseValue' })); | ||
expect(newClassName).toEqual(`${ componentClassName } ${ componentClassName }--a-camel-case-value`); | ||
}); | ||
}); |
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
Oops, something went wrong.