-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: useMountId hook for uuid (#659)
- Loading branch information
Conrad Chan
authored
Dec 17, 2020
1 parent
f445cef
commit d67fef3
Showing
9 changed files
with
64 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function useMountId(callback?: (uuid: string) => void): string { | ||
if (callback) { | ||
callback('123'); | ||
} | ||
|
||
return '123'; | ||
} |
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,25 @@ | ||
import * as React from 'react'; | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
import useMountId from '../useMountId'; | ||
|
||
jest.mock('uuid', () => ({ | ||
v4: () => '123', | ||
})); | ||
|
||
describe('useMountId', () => { | ||
function TestComponent({ onMount }: { onMount: (uuid: string) => void }): JSX.Element { | ||
const uuid = useMountId(onMount); | ||
|
||
return <div data-testid="uuid" data-uuid={uuid} />; | ||
} | ||
|
||
const getWrapper = (onMount: (uuid: string) => void): ReactWrapper => mount(<TestComponent onMount={onMount} />); | ||
|
||
test('should render the component with a generated uuid and call the callback with the generated uuid', () => { | ||
const onMount = jest.fn(); | ||
const wrapper = getWrapper(onMount); | ||
|
||
expect(wrapper.find('[data-testid="uuid"]').prop('data-uuid')).toEqual('123'); | ||
expect(onMount).toHaveBeenCalledWith('123'); | ||
}); | ||
}); |
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,14 @@ | ||
import React from 'react'; | ||
import * as uuid from 'uuid'; | ||
|
||
// Returns a generated uuid, then calls the provided callback via an effect | ||
export default function useMountId(callback: (uuid: string) => void): string { | ||
const uuidRef = React.useRef<string>(uuid.v4()); | ||
|
||
// The callback is only invoked after the parent component has rendered | ||
React.useEffect(() => { | ||
callback(uuidRef.current); | ||
}, [callback]); | ||
|
||
return uuidRef.current; | ||
} |
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
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