-
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add watch to valtio/utils * Exclude watch types from exports * Change `currentCleanups` type to undefined union * Improve tests for `watch` * Remove unnecessary test
- Loading branch information
Showing
3 changed files
with
185 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { proxy } from '../src/vanilla' | ||
import { watch } from '../src/utils' | ||
|
||
describe('watch', () => { | ||
it('should re-run for individual proxy updates', async () => { | ||
const reference = proxy({ value: 'Example' }) | ||
|
||
const callback = jest.fn() | ||
|
||
watch((get) => { | ||
get(reference) | ||
callback() | ||
}) | ||
|
||
expect(callback).toBeCalledTimes(1) | ||
reference.value = 'Update' | ||
await Promise.resolve() | ||
expect(callback).toBeCalledTimes(2) | ||
}) | ||
it('should re-run for multiple proxy updates', async () => { | ||
const A = proxy({ value: 'A' }) | ||
const B = proxy({ value: 'B' }) | ||
|
||
const callback = jest.fn() | ||
|
||
watch((get) => { | ||
get(A) | ||
get(B) | ||
callback() | ||
}) | ||
|
||
expect(callback).toBeCalledTimes(1) | ||
A.value = 'B' | ||
await Promise.resolve() | ||
expect(callback).toBeCalledTimes(2) | ||
B.value = 'C' | ||
await Promise.resolve() | ||
expect(callback).toBeCalledTimes(3) | ||
}) | ||
it('should cleanup when state updates', async () => { | ||
const reference = proxy({ value: 'Example' }) | ||
|
||
const callback = jest.fn() | ||
|
||
watch((get) => { | ||
get(reference) | ||
|
||
return () => { | ||
callback() | ||
} | ||
}) | ||
|
||
expect(callback).toBeCalledTimes(0) | ||
reference.value = 'Update' | ||
await Promise.resolve() | ||
expect(callback).toBeCalledTimes(1) | ||
}) | ||
it('should cleanup when stopped', () => { | ||
const callback = jest.fn() | ||
|
||
const stop = watch(() => callback) | ||
|
||
expect(callback).toBeCalledTimes(0) | ||
stop() | ||
expect(callback).toBeCalledTimes(1) | ||
}) | ||
it('should cleanup internal effects when stopped', () => { | ||
const callback = jest.fn() | ||
|
||
const stop = watch(() => { | ||
watch(() => { | ||
watch(() => { | ||
watch(() => { | ||
watch(() => () => { | ||
callback() | ||
}) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
expect(callback).toBeCalledTimes(0) | ||
stop() | ||
expect(callback).toBeCalledTimes(1) | ||
}) | ||
}) |