-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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 #2167 from Shopify/hookify-fvs
[FilterValueSelector] Convert to functional component & add isMountedRef
- Loading branch information
Showing
6 changed files
with
170 additions
and
113 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
5 changes: 2 additions & 3 deletions
5
src/components/ResourceList/components/FilterControl/components/FilterValueSelector/index.ts
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,5 +1,4 @@ | ||
import FilterValueSelector, { | ||
export { | ||
FilterValueSelector, | ||
FilterValueSelectorProps, | ||
} from './FilterValueSelector'; | ||
|
||
export {FilterValueSelector, FilterValueSelectorProps}; |
2 changes: 1 addition & 1 deletion
2
...omponents/FilterControl/components/FilterValueSelector/tests/FilterValueSelector.test.tsx
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,58 @@ | ||
import React, {useEffect} from 'react'; | ||
import {mount} from 'test-utilities'; | ||
import {useIsMountedRef} from '../use-is-mounted-ref'; | ||
|
||
describe('useIsMountedRef', () => { | ||
it('returns a false value before mounting', () => { | ||
const spy = jest.fn((_) => {}); | ||
|
||
function MockComponent() { | ||
const isMounted = useIsMountedRef(); | ||
spy(isMounted.current); | ||
return null; | ||
} | ||
|
||
mount(<MockComponent />); | ||
expect(spy).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('returns a false value after unmounting', () => { | ||
const spy = jest.fn((_) => {}); | ||
|
||
function MockComponent() { | ||
const isMounted = useIsMountedRef(); | ||
|
||
useEffect(() => { | ||
return () => { | ||
// This ref is not pointing to a node rendered by react so | ||
// we're ok ignoring the linting rule since it doesn't apply | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
spy(isMounted.current); | ||
}; | ||
}, [isMounted]); | ||
|
||
return null; | ||
} | ||
|
||
const mockComponent = mount(<MockComponent />); | ||
mockComponent.unmount(); | ||
expect(spy).toHaveBeenCalledWith(false); | ||
}); | ||
|
||
it('returns true after the component mounts', () => { | ||
const spy = jest.fn((_) => {}); | ||
|
||
function MockComponent() { | ||
const isMounted = useIsMountedRef(); | ||
|
||
useEffect(() => { | ||
spy(isMounted.current); | ||
}, [isMounted]); | ||
|
||
return null; | ||
} | ||
|
||
mount(<MockComponent />); | ||
expect(spy).toHaveBeenCalledWith(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import {useRef, useEffect, MutableRefObject} from 'react'; | ||
|
||
/** | ||
* Returns a MutatableRefObject containing a boolean value that | ||
* represents a components mounted status. | ||
* @returns MutableRefObject<boolean> The mounted status | ||
*/ | ||
export function useIsMountedRef(): MutableRefObject<boolean> { | ||
const isMounted = useRef(false); | ||
|
||
useEffect(() => { | ||
isMounted.current = true; | ||
return () => { | ||
isMounted.current = false; | ||
}; | ||
}, []); | ||
|
||
return isMounted; | ||
} |