forked from splendo/HeartstoneAssessment
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[splendo#9] Created test for cardAction
- Loading branch information
1 parent
9254d98
commit 97d87ab
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
hearthstone/__tests__/redux/actions/cardAction.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}); | ||
}); | ||
}); | ||
}); | ||
}); |