Skip to content

Commit

Permalink
add tests for create query model shared util
Browse files Browse the repository at this point in the history
  • Loading branch information
Bricks666 committed Jul 28, 2024
1 parent 1196986 commit ed7d1fe
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 2 deletions.
4 changes: 2 additions & 2 deletions configs/tests/utils/use-test-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export interface UseTestRouterResult {
export const useTestRouter = (params: UseTestRouterParams) => {
const { getScope, router, options } = params;

beforeEach(() => {
allSettled(router.setHistory, {
beforeEach(async () => {
await allSettled(router.setHistory, {
scope: getScope(),
params: createMemoryHistory(options),
});
Expand Down
199 changes: 199 additions & 0 deletions src/shared/lib/create-query-model.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import { createRoute } from 'atomic-router';
import { allSettled, createEvent } from 'effector';
import { beforeEach, describe, expect, test } from 'vitest';

import { router } from '@/shared/configs';

import { QueryModel, createQueryModel } from './create-query-model';

import { useTestRouter, useTestScope } from '~/tests';

describe('shared/lib/create-query-model', () => {
const name = 'test';
const defaultValue = 'default';
const value = 'another-value';
let queryModel: QueryModel<string>;

const { getScope, } = useTestScope();
useTestRouter({ getScope, router, });

describe('simple variant', () => {
beforeEach(() => {
queryModel = createQueryModel({
name,
defaultValue,
});
});

test('should set new value on set action call', async () => {
await allSettled(queryModel.set, { scope: getScope(), params: value, });

expect(getScope().getState(router.$query)).toStrictEqual({
[name]: value,
});
expect(getScope().getState(queryModel.$value)).toBe(value);
});

test('should reset value on reset action call', async () => {
await allSettled(queryModel.set, { scope: getScope(), params: value, });
await allSettled(queryModel.reset, { scope: getScope(), });

expect(getScope().getState(router.$query)).toStrictEqual({
[name]: defaultValue,
});
expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
});

test('should correctly indicate empty state', async () => {
await allSettled(queryModel.set, {
scope: getScope(),
params: defaultValue,
});
expect(getScope().getState(queryModel.$isEmpty)).toBeTruthy();

await allSettled(queryModel.set, { scope: getScope(), params: value, });
expect(getScope().getState(queryModel.$isEmpty)).toBeFalsy();
});

test('should clear empty value from query', async () => {
await allSettled(queryModel.set, {
scope: getScope(),
params: '',
});
expect(getScope().getState(router.$query)).toStrictEqual({});
});

test('should not rewrite others queries', async () => {
const baseQueries = {
page: 123,
};

await allSettled(router.$query, {
scope: getScope(),
params: baseQueries,
});

await allSettled(queryModel.set, {
scope: getScope(),
params: value,
});
expect(getScope().getState(router.$query)).toStrictEqual({
page: '123',
[name]: value,
});
});
});

describe('with specific route', () => {
const route = createRoute();

beforeEach(() => {
queryModel = createQueryModel({
name,
defaultValue,
route,
});
});

test('should sync value if passed route is opened', async () => {
await allSettled(queryModel.set, { scope: getScope(), params: value, });

expect(getScope().getState(queryModel.$value)).toBe(value);
expect(getScope().getState(router.$query)).not.toStrictEqual({
[name]: value,
});

await allSettled(route.open, { scope: getScope(), });

await allSettled(queryModel.reset, { scope: getScope(), });

expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).toStrictEqual({
[name]: defaultValue,
});
});

test('should not sync value if passed route is closed', async () => {
expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).not.toStrictEqual({
[name]: defaultValue,
});

await allSettled(route.closed, { scope: getScope(), });

expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).not.toStrictEqual({
[name]: defaultValue,
});
});
});

describe('with clock method', () => {
const clock = createEvent();

beforeEach(() => {
queryModel = createQueryModel({
name,
defaultValue,
clock,
});
});

test('should sync value on clock call', async () => {
expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).not.toStrictEqual({
[name]: defaultValue,
});

await allSettled(clock, { scope: getScope(), });

expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).toStrictEqual({
[name]: defaultValue,
});
});
});

describe('with route and clock method', () => {
const clock = createEvent();
const route = createRoute();

beforeEach(() => {
queryModel = createQueryModel({
name,
defaultValue,
clock,
route,
});
});

test('should sync method on clock call if route is opened', async () => {
expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).not.toStrictEqual({
[name]: defaultValue,
});

await allSettled(route.open, { scope: getScope(), });
await allSettled(clock, { scope: getScope(), });

expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).toStrictEqual({
[name]: defaultValue,
});
});

test('should not sync method on clock call if route is closed', async () => {
expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).not.toStrictEqual({
[name]: defaultValue,
});

await allSettled(clock, { scope: getScope(), });

expect(getScope().getState(queryModel.$value)).toBe(defaultValue);
expect(getScope().getState(router.$query)).not.toStrictEqual({
[name]: defaultValue,
});
});
});
});

0 comments on commit ed7d1fe

Please sign in to comment.