Skip to content

Commit

Permalink
Handle params passing
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Sep 5, 2023
1 parent d119696 commit 5b84d35
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
30 changes: 27 additions & 3 deletions packages/atomic-router/src/__tests__/fresh.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createQuery } from '@farfetched/core';
import { chainRoute, createRoute } from 'atomic-router';
import { allSettled, createWatch, fork } from 'effector';
import { describe, expect, test, vi } from 'vitest';

import { createDefer } from '../defer';
import { freshChain } from '../fresh';

Expand All @@ -27,7 +29,7 @@ describe('freshChain', () => {
// First open — execute
allSettled(chain.beforeOpen, {
scope,
params: { params: undefined, query: {} },
params: { params: 1, query: {} },
});

expect(handler).toBeCalledTimes(1);
Expand All @@ -43,7 +45,7 @@ describe('freshChain', () => {
// Second open — just openOp immediately
await allSettled(chain.beforeOpen, {
scope,
params: { params: undefined, query: {} },
params: { params: 1, query: {} },
});

expect(handler).toBeCalledTimes(1);
Expand All @@ -53,7 +55,7 @@ describe('freshChain', () => {
// Third open — execute, because of changed params
allSettled(chain.beforeOpen, {
scope,
params: { params: undefined, query: { some: 2 } },
params: { params: 2, query: {} },
});

expect(handler).toBeCalledTimes(2);
Expand All @@ -66,4 +68,26 @@ describe('freshChain', () => {
expect(openOnListener).toBeCalledTimes(3);
expect(cancelOnListener).not.toBeCalled();
});

test('pass route params to query', async () => {
const handler = vi.fn().mockImplementation(() => null);
const query = createQuery({
handler,
});

const route = createRoute<{ id: number }>();
const chainedRoute = chainRoute({ route, ...freshChain(query) });

const scope = fork();

await allSettled(route.open, { scope, params: { id: 1 } });

expect(handler).toBeCalledTimes(1);
expect(handler).toBeCalledWith({ id: 1 });

await allSettled(route.open, { scope, params: { id: 2 } });

expect(handler).toBeCalledTimes(2);
expect(handler).toBeCalledWith({ id: 2 });
});
});
24 changes: 24 additions & 0 deletions packages/atomic-router/src/__tests__/start.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createQuery } from '@farfetched/core';
import { chainRoute, createRoute } from 'atomic-router';
import { allSettled, createStore, createWatch, fork } from 'effector';
import { describe, expect, test, vi } from 'vitest';

import { createDefer } from '../defer';
import { startChain } from '../start';

Expand Down Expand Up @@ -87,4 +89,26 @@ describe('startChain', () => {
expect(openOnListener).not.toBeCalled();
expect(cancelOnListener).toBeCalledTimes(1);
});

test('pass route params to query', async () => {
const handler = vi.fn().mockImplementation(() => null);
const query = createQuery({
handler,
});

const route = createRoute<{ id: number }>();
const chainedRoute = chainRoute({ route, ...startChain(query) });

const scope = fork();

await allSettled(route.open, { scope, params: { id: 1 } });

expect(handler).toBeCalledTimes(1);
expect(handler).toBeCalledWith({ id: 1 });

await allSettled(route.open, { scope, params: { id: 2 } });

expect(handler).toBeCalledTimes(2);
expect(handler).toBeCalledWith({ id: 2 });
});
});
6 changes: 5 additions & 1 deletion packages/atomic-router/src/fresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export function freshChain(
const openOn = createEvent<any>();
const cancelOn = createEvent<any>();

sample({ clock: beforeOpen, target: query.refresh });
sample({
clock: beforeOpen,
fn: ({ params }) => params,
target: query.refresh,
});
sample({
clock: [
query.finished.success,
Expand Down
6 changes: 5 additions & 1 deletion packages/atomic-router/src/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export function startChain(
const openOn = createEvent<any>();
const cancelOn = createEvent<any>();

sample({ clock: beforeOpen, target: query.start });
sample({
clock: beforeOpen,
fn: ({ params }) => params,
target: query.start,
});
sample({ clock: query.finished.success, target: openOn });
sample({
clock: [query.finished.failure, query.finished.skip],
Expand Down

0 comments on commit 5b84d35

Please sign in to comment.