From 31867d73d84667871c122029b339dc247208f984 Mon Sep 17 00:00:00 2001 From: Bricks666 Date: Sun, 25 Aug 2024 20:17:53 +0300 Subject: [PATCH] add test for session model --- src/shared/models/session.spec.ts | 240 ++++++++++++++++++++++++++++++ src/shared/models/session.ts | 3 +- 2 files changed, 241 insertions(+), 2 deletions(-) create mode 100644 src/shared/models/session.spec.ts diff --git a/src/shared/models/session.spec.ts b/src/shared/models/session.spec.ts new file mode 100644 index 00000000..266ae019 --- /dev/null +++ b/src/shared/models/session.spec.ts @@ -0,0 +1,240 @@ +import { RouteInstance, createRoute } from 'atomic-router'; +import { Event, createEvent } from 'effector'; +import { beforeEach, describe, test, expect } from 'vitest'; + +import { ChainedParams } from '../types'; + +import { $user, chainAnonymous, chainAuthorized, query } from './session'; + +import { + Scope, + allSettled, + defaultUser, + fork, + handlers, + server +} from '~/test-utils'; + +describe('shared/models/session', () => { + let scope: Scope; + + beforeEach(() => { + scope = fork(); + }); + + describe('authorization', () => { + test('should set fetched user into $user store', async () => { + await allSettled(query.start, { scope, }); + + expect(scope.getState($user)).toStrictEqual(defaultUser); + }); + + test.skip('should reset user if query finished with error', async () => { + await allSettled(query.start, { scope, }); + + server.use(handlers.auth.error.auth); + + await allSettled(query.start, { scope, }); + + server.use(handlers.auth.success.auth); + + expect(scope.getState($user)).toBeNull(); + }); + }); + + describe('chainAuthorized', () => { + let route: RouteInstance; + let chainedRoute: RouteInstance; + + const chainRoute = (options?: ChainedParams) => { + chainedRoute = chainAuthorized(route, options); + }; + + beforeEach(() => { + route = createRoute(); + }); + + describe('open', () => { + beforeEach(() => { + server.use(handlers.auth.success.auth); + }); + + test('should open route if user authorized successfuly', async () => { + chainRoute(); + + await allSettled(route.open, { scope, }); + + expect(scope.getState(chainedRoute.$isOpened)).toBeTruthy(); + }); + + test('should open route if user has already authorized successfuly', async () => { + await allSettled(query.start, { scope, }); + + chainRoute(); + + await allSettled(route.open, { scope, }); + + expect(scope.getState(chainedRoute.$isOpened)).toBeTruthy(); + }); + }); + + describe('close', () => { + let otherwise: Event; + + beforeEach(() => { + otherwise = createEvent(); + server.use(handlers.auth.error.auth); + }); + + test('should close route if user autorized failurly', async () => { + chainRoute(); + + await allSettled(route.open, { scope, }); + + expect(scope.getState(chainedRoute.$isOpened)).toBeFalsy(); + }); + + test('should close route if user has already autorized failurly', async () => { + await allSettled(query.start, { scope, }); + + chainRoute(); + + await allSettled(route.open, { scope, }); + + expect(scope.getState(chainedRoute.$isOpened)).toBeFalsy(); + }); + + test('should call otherwise option if user autorized failurly', async () => { + expect.assertions(1); + + await allSettled(query.start, { scope, }); + + chainRoute({ otherwise, }); + + const unwatch = otherwise.watch(() => { + expect(true).toBeTruthy(); + }); + + await allSettled(route.open, { scope, }); + + unwatch(); + }); + + test('should call otherwise option if user has already autorized failurly', async () => { + expect.assertions(1); + + await allSettled(query.start, { scope, }); + + chainRoute({ otherwise, }); + + const unwatch = otherwise.watch(() => { + expect(true).toBeTruthy(); + }); + + await allSettled(route.open, { scope, }); + + unwatch(); + }); + }); + }); + + describe('chainAnonymous', () => { + let route: RouteInstance; + let chainedRoute: RouteInstance; + + const chainRoute = (options?: ChainedParams) => { + chainedRoute = chainAnonymous(route, options); + }; + + beforeEach(() => { + route = createRoute(); + }); + + describe('open', () => { + beforeEach(() => { + server.use(handlers.auth.error.auth); + }); + + test('should open route if user authorized failurly', async () => { + chainRoute(); + + await allSettled(route.open, { scope, }); + + expect(scope.getState(chainedRoute.$isOpened)).toBeTruthy(); + }); + + test('should open route if user has already authorized failurly', async () => { + await allSettled(query.start, { scope, }); + + chainRoute(); + + await allSettled(route.open, { scope, }); + + expect(scope.getState(chainedRoute.$isOpened)).toBeTruthy(); + }); + }); + + describe('close', () => { + let otherwise: Event; + + beforeEach(() => { + otherwise = createEvent(); + server.use(handlers.auth.success.auth); + }); + + test('should close route if user autorized successfuly', async () => { + chainRoute(); + + await allSettled(route.open, { scope, }); + + expect(scope.getState(chainedRoute.$isOpened)).toBeFalsy(); + }); + + test.skip('should close route if user has already autorized successfuly', async () => { + await allSettled(query.start, { scope, }); + + chainRoute(); + + console.log(scope.getState($user)); + + await allSettled(route.open, { scope, }); + + expect(scope.getState(chainedRoute.$isOpened)).toBeFalsy(); + }); + + test('should call otherwise option if user autorized successfuly', async () => { + expect.assertions(1); + + await allSettled(query.start, { scope, }); + + chainRoute({ otherwise, }); + + const unwatch = otherwise.watch(() => { + expect(true).toBeTruthy(); + }); + + await allSettled(route.open, { scope, }); + + unwatch(); + }); + + test('should call otherwise option if user has already autorized successfuly', async () => { + expect.assertions(1); + + await allSettled(query.start, { scope, }); + + console.log(scope.getState($user)); + + chainRoute({ otherwise, }); + + const unwatch = otherwise.watch(() => { + expect(true).toBeTruthy(); + }); + + await allSettled(route.open, { scope, }); + + unwatch(); + }); + }); + }); +}); diff --git a/src/shared/models/session.ts b/src/shared/models/session.ts index 7a9211c9..f4137793 100644 --- a/src/shared/models/session.ts +++ b/src/shared/models/session.ts @@ -27,8 +27,7 @@ import { type Status = 'initial' | 'pending' | 'authorized' | 'anonymous'; export const $user = createStore(null); -export const $status = createStore('initial'); -export const $isAuth = $status.map((status) => status === 'authorized'); +const $status = createStore('initial'); const handlerFx = createEffect(authApi.auth);