Skip to main content

mocks

Library for creating mocks for various tramvai entities

Подключение

npm i --save-dev @tramvai/test-mocks

Api

STORE_TOKEN

Creates mock instance for token STORE_TOKEN which used in app as a common storage for store

Empty State

import { createMockStore } from '@tramvai/test-mocks';

const store = createMockStore();
const state = store.getState();

store.dispatch('event');

Initial State

Pass required stores, initial state will be applied automatically:

import { createMockStore } from '@tramvai/test-mocks';

const reducer = createReducer('test', 'value');

const store = createMockStore({ stores: [reducer] });

const state = store.getState(); // { test: 'value' }

Or pass just initialState, fake reducers will be created under the hood:

import { createMockStore } from '@tramvai/test-mocks';

const initialState = { a: 1, b: 2 };

const store = createMockStore({ initialState });

const state = store.getState(); // { a: 1, b: 2 }

Also you can change initial state of passed reducer:

import { createMockStore } from '@tramvai/test-mocks';

const initialState = { test: 'modified' };
const reducer = createReducer('test', 'default');

const store = createMockStore({ stores: [reducer], initialState });

const state = store.getState(); // { test: 'modified' }

More examples

import { createReducer, createEvent } from '@tramvai/state';
import { createMockStore } from './store';

describe('test/unit/mocks/store', () => {
it('should create empty store', () => {
const store = createMockStore();
const spyGetState = jest.spyOn(store, 'getState');

expect(store.getState()).toEqual({});
expect(spyGetState).toHaveBeenCalled();
});

it('should update stores with dispatch', () => {
const event = createEvent<string>('testEvent');
const reducer = createReducer('test', { a: 'test' }).on(event, (_, data) => {
return {
a: data,
};
});
const store = createMockStore({ stores: [reducer] });

expect(store.getState()).toEqual({ test: { a: 'test' } });

store.dispatch(event('dispatched'));

expect(store.getState()).toEqual({ test: { a: 'dispatched' } });
});

it('should create store by initialState', () => {
const initialState = { a: 1, b: 2 };
const reducerA = createReducer('a', {});
const reducerB = createReducer('b', {});
const store = createMockStore({ stores: [reducerA, reducerB], initialState });

expect(store.getState()).toEqual(initialState);
});

it('should create fake reducer stores for every key in initialState', () => {
const initialState = { a: 1, b: 2 };
const reducerC = createReducer('c', 3);
const store = createMockStore({ stores: [reducerC], initialState });

expect(store.getState()).toEqual({
a: 1,
b: 2,
c: 3,
});
});
});

DI

Creates mock instance of DI-container

import { createMockDi } from '@tramvai/test-mocks';

const di = createMockDi();

const dep = di.get(SOME_TOKEN);

Context

Creates mock instance for CONTEXT_TOKEN

import { createMockContext } from '@tramvai/test-mocks';

it('test', async () => {
const context = createMockContext();

await context.dispatch('event');
await context.executeAction(action);

const spyExecuteAction = jest.spyOn(context, 'executeAction');

expect(spyExecuteAction).toHaveBeenCalled();
});

More examples

import { createEvent, createReducer } from '@tramvai/state';
import { createMockContext } from './context';

describe('test/unit/mocks/context', () => {
it('should create consumer context', () => {
const context = createMockContext();

expect(context.executeAction).toBeInstanceOf(Function);
expect(context.getState).toBeInstanceOf(Function);
expect(context.dispatch).toBeInstanceOf(Function);

expect(context.getState()).toEqual({});
});

it('should dispatch data', async () => {
const event = createEvent<string>('mockEvent');
const reducer = createReducer('a', 'data').on(event, (_, data) => data);
const context = createMockContext({
stores: [reducer],
});

const spyDispatch = jest.spyOn(context, 'dispatch');

await context.dispatch(event('mock1'));

expect(spyDispatch).toHaveBeenCalledWith(event('mock1'));

expect(context.getState()).toEqual({ a: 'mock1' });
});
});

Router

Creates mock instance for @tinkoff/router

import { createMockRouter } from '@tramvai/test-mocks';

describe('test', () => {
it('should create router mock', () => {
const router = createMockRouter();

expect(router.getCurrentRoute()).toMatchObject({ path: '/' });
expect(router.getCurrentUrl()).toMatchObject({ path: '/' });
});

it('should allow to specify currentRoute', () => {
const router = createMockRouter({ currentRoute: { name: 'page', path: '/page/test/' } });

expect(router.getCurrentRoute()).toMatchObject({ path: '/page/test/' });
expect(router.getCurrentUrl()).toMatchObject({ path: '/page/test/' });
});
});