Skip to content

Commit

Permalink
add test for session model
Browse files Browse the repository at this point in the history
  • Loading branch information
Bricks666 committed Aug 25, 2024
1 parent 8218525 commit 31867d7
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 2 deletions.
240 changes: 240 additions & 0 deletions src/shared/models/session.spec.ts
Original file line number Diff line number Diff line change
@@ -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<any>;
let chainedRoute: RouteInstance<any>;

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<any>;

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<any>;
let chainedRoute: RouteInstance<any>;

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<any>;

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();
});
});
});
});
3 changes: 1 addition & 2 deletions src/shared/models/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import {
type Status = 'initial' | 'pending' | 'authorized' | 'anonymous';

export const $user = createStore<User | null>(null);
export const $status = createStore<Status>('initial');
export const $isAuth = $status.map((status) => status === 'authorized');
const $status = createStore<Status>('initial');

const handlerFx = createEffect(authApi.auth);

Expand Down

0 comments on commit 31867d7

Please sign in to comment.