-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(state-wire): add useSubscribe hook (#19)
- Loading branch information
Showing
5 changed files
with
101 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
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,66 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useCallback } from 'react'; | ||
import { act } from 'react-dom/test-utils'; | ||
import { useStateWire } from './use-state-wire'; | ||
import { useSubscribe } from './use-subscribe'; | ||
|
||
describe('useSubscribe', () => { | ||
it('should not call the callback function on subscribing', () => { | ||
const fn = jest.fn(); | ||
|
||
renderHook(() => { | ||
const wire = useStateWire(null, 5); | ||
useSubscribe( | ||
wire, | ||
useCallback(value => { | ||
fn(value); | ||
}, []), | ||
); | ||
return { wire }; | ||
}); | ||
expect(fn).not.toBeCalled(); | ||
}); | ||
|
||
it('should call the callback function when the wire value updates', () => { | ||
const fn = jest.fn(); | ||
|
||
const { result } = renderHook(() => { | ||
const wire = useStateWire(null, 5); | ||
useSubscribe( | ||
wire, | ||
useCallback(value => { | ||
fn(value); | ||
}, []), | ||
); | ||
return { wire }; | ||
}); | ||
act(() => { | ||
result.current.wire.setValue(3); | ||
}); | ||
|
||
expect(fn).toBeCalledWith(3); | ||
}); | ||
|
||
it('should not call the callback function after unmount', () => { | ||
const fn = jest.fn(); | ||
|
||
const { result, unmount } = renderHook(() => { | ||
const wire = useStateWire(null, 5); | ||
useSubscribe( | ||
wire, | ||
useCallback(value => { | ||
fn(value); | ||
}, []), | ||
); | ||
return { wire }; | ||
}); | ||
act(() => { | ||
unmount(); | ||
}); | ||
act(() => { | ||
result.current.wire.setValue(3); | ||
}); | ||
|
||
expect(fn).not.toBeCalled(); | ||
}); | ||
}); |
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 { useEffect } from 'react'; | ||
import { Defined } from '../utils/type-utils'; | ||
import { useStabilityGuard } from '../utils/use-stability-guard'; | ||
import { ReadonlyStateWire } from './readonly-state-wire'; | ||
|
||
export function useSubscribe<V>( | ||
wire: ReadonlyStateWire<V>, | ||
callback: (value: Defined<V>) => void, | ||
): void { | ||
useStabilityGuard(wire); | ||
useEffect(() => { | ||
return wire.subscribe(callback); | ||
}, [wire, callback]); | ||
} |