@tramvai/test-mocks
TypeScript icon, indicating that this package has built-in type declarations

3.40.58 • Public • Published

Tramvai test 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

@inline src/store.spec.ts

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

@inline src/context.spec.ts

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/' });
  });
});

Readme

Keywords

none

Package Sidebar

Install

npm i @tramvai/test-mocks

Weekly Downloads

686

Version

3.40.58

License

Apache-2.0

Unpacked Size

39.6 kB

Total Files

38

Last publish

Collaborators

  • meskill
  • super_oleg
  • tinkoffbank