Skip to content

ushiboy/cyclone-mock-store

Repository files navigation

Cyclone-Mock-Store

Build Status

Cyclone-Mock-Store is a testing tools for Cyclone.

Quick Sample

Here is an example of a test.

const assert = require('power-assert');
import { createMockStore } from '@ushiboy/cyclone-mock-store';
const { mockDispatcher } = createMockStore();

function increment() {
  return { type: 'increment' };
}

function greet() {
  return async ({ fetchMessage }) => {
    return {
      type: 'greeting',
      payload: {
        message: await fetchMessage()
      }
    };
  };
}

describe('actions', function() {
  describe('increment', () => {
    it('should returns "increment" type of action', async () => {
      const store = mockDispatcher();
      await store.dispatch(increment());

      const [a] = store.getActions();
      assert(a.type === 'increment');
    });
  });

  describe('greet', () => {
    let store;
    beforeEach(() => {
      store = mockDispatcher({
        async fetchMessage() {
          return 'hello';
        }
      });
    });
    it('should returns "greeting" type of action', async () => {
      await store.dispatch(greet());

      const [a] = store.getActions();
      assert(a.type === 'greeting');
    });
    it('should returns payload of message', async () => {
      await store.dispatch(greet());

      const [a] = store.getActions();
      assert(a.payload.message === 'hello');
    });
  });
});

API

createMockStore

It return a MockAPI.

createMockStore(): MockAPI

MockAPI

It is an API that mocks the Store.

type MockAPI = {
  mockDispatcher<E, A>(extra: ?E): MockStore<E, A>
};

mockDispatcher

It create and return a MockStore from the extra argument.

mockDispatcher<E, A>(extra: ?E): MockStore<E, A>

MockStore

Record the actions executed.

type MockStore<E, A> = {
  dispatch(a: A | Promise<A> | (e: E) => (A | Promise<A>)): Promise<void>,
  getActions(): Array<A>,
  clearActions(): void
};

dispatch

It execute Action against MockStore.

dispatch(a: A | Promise<A> | (e: E) => (A | Promise<A>)): Promise<void>

getActions

It returns the recorded actions.

getActions(): Array<A>

clearActions

It clear the recorded actions.

clearActions(): void

Change Log

0.1.0

Initial Cyclone-Mock-Store release.

License

MIT

About

Testing tools for Cyclone.

Resources

License

Stars

Watchers

Forks

Packages

No packages published