-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5a4ca2d
commit 843686f
Showing
5 changed files
with
140 additions
and
10 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,69 @@ | ||
import { createQuery } from '@farfetched/core'; | ||
import { allSettled, createWatch, fork } from 'effector'; | ||
import { describe, expect, test, vi } from 'vitest'; | ||
import { createDefer } from '../defer'; | ||
import { freshChain } from '../fresh'; | ||
|
||
describe('freshChain', () => { | ||
test('should start query after beforeOpen call and call openOn, respect $stale state', async () => { | ||
const firstDefer = createDefer(); | ||
const secondDefer = createDefer(); | ||
|
||
const handler = vi | ||
.fn() | ||
.mockImplementationOnce(() => firstDefer.promise) | ||
.mockImplementationOnce(() => secondDefer.promise); | ||
|
||
const query = createQuery({ handler }); | ||
const chain = freshChain(query); | ||
|
||
const scope = fork(); | ||
|
||
const openOnListener = vi.fn(); | ||
createWatch({ unit: chain.openOn, fn: openOnListener, scope }); | ||
const cancelOnListener = vi.fn(); | ||
createWatch({ unit: chain.cancelOn, fn: cancelOnListener, scope }); | ||
|
||
// First open — execute | ||
allSettled(chain.beforeOpen, { | ||
scope, | ||
params: { params: undefined, query: {} }, | ||
}); | ||
|
||
expect(handler).toBeCalledTimes(1); | ||
expect(openOnListener).not.toBeCalled(); | ||
expect(cancelOnListener).not.toBeCalled(); | ||
|
||
firstDefer.resolve(null); | ||
await allSettled(scope); | ||
|
||
expect(openOnListener).toBeCalledTimes(1); | ||
expect(cancelOnListener).not.toBeCalled(); | ||
|
||
// Second open — just openOp immediately | ||
await allSettled(chain.beforeOpen, { | ||
scope, | ||
params: { params: undefined, query: {} }, | ||
}); | ||
|
||
expect(handler).toBeCalledTimes(1); | ||
expect(openOnListener).toBeCalledTimes(2); | ||
expect(cancelOnListener).not.toBeCalled(); | ||
|
||
// Third open — execute, because of changed params | ||
allSettled(chain.beforeOpen, { | ||
scope, | ||
params: { params: undefined, query: { some: 2 } }, | ||
}); | ||
|
||
expect(handler).toBeCalledTimes(2); | ||
expect(openOnListener).toBeCalledTimes(2); | ||
expect(cancelOnListener).not.toBeCalled(); | ||
|
||
secondDefer.resolve(null); | ||
await allSettled(scope); | ||
|
||
expect(openOnListener).toBeCalledTimes(3); | ||
expect(cancelOnListener).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
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