Skip to content

Commit

Permalink
[splendo#9] Created test for cardAction
Browse files Browse the repository at this point in the history
  • Loading branch information
corrado4eyes committed Dec 3, 2019
1 parent 9254d98 commit 97d87ab
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions hearthstone/__tests__/redux/actions/cardAction.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import * as fromCard from '../../../src/redux/actions/cardActions';
import {generateMockStore} from '../../__mocks__/mockStore';
import { MockStore } from 'redux-mock-store';
import CardService from '../../../src/services/cardService';
import ServiceFactory from '../../../src/services/serviceFactory';
import { spyRejects, spyResolves } from '../../testUtils/promiseUtils';
import { dummyCardArray } from '../../__mocks__/mockObjects';

const cardNotFoundError = "Cards not Found!";

const mockCardServiceFailure = (error?: any): CardService => {
return {
getAll: spyRejects(error),
get: spyRejects(error),
save: spyRejects(error),
}
}

const mockSyncCardReject = mockCardServiceFailure()

const mockCardServiceSuccess = (resolvedData?: any): CardService => {
return {
getAll: spyResolves(resolvedData),
get: spyResolves(resolvedData),
save: spyResolves(resolvedData),
}
}

const serviceSetup = (result: CardService) => {
return jest.spyOn(ServiceFactory.prototype, "getCardService")
.mockImplementation(() => result);
};

describe('Card Actions', () => {
let store: MockStore<any, any>;
let spy: jest.SpyInstance<CardService>;

afterAll(() => {
spy.mockRestore();
});

beforeEach(() => {
spy.mockClear();
store = generateMockStore({});
store.clearActions();
});

describe('onSyncCard Action', () => {
beforeEach(async () => {
await store.dispatch(fromCard.dispatchSyncCard())
});
describe('onSyncCardSucceeded', () => {
beforeAll(() => {
jest.resetModules();
spy = serviceSetup(mockCardServiceSuccess(dummyCardArray));
});

beforeEach(() => {
spy.mockClear();
store.dispatch(fromCard.dispatchSyncCard());
});

it("creates the right payload", () => {
const action = fromCard.onSyncCardsConstructor();
expect(action).toEqual({
type: fromCard.CardActions.onSyncCards,
});
});

it("dispatches the right action", () => {
const action = store.getActions();
expect(action.length).toBe(2);
expect(action[1]).toEqual({type: fromCard.CardActions.onSyncCardsSucceed, cards: dummyCardArray});
});
});
describe('onSyncCardFailed', () => {
beforeAll(() => {
jest.resetModules();
spy = serviceSetup(mockCardServiceFailure(cardNotFoundError))
});
beforeEach(() => {
spy.mockClear();
store.dispatch(fromCard.dispatchSyncCard());
});

it("creates the right payload", () => {
const action = fromCard.onSyncCardsConstructor();
expect(action).toEqual({
type: fromCard.CardActions.onSyncCards,
});
});

it("dispatches the right action", () => {
const action = store.getActions();
expect(action.length).toBe(2);
expect(action[1]).toEqual({type: fromCard.CardActions.onSyncCardsFailed, error: cardNotFoundError});
});
});
});
});

0 comments on commit 97d87ab

Please sign in to comment.