Created
April 24, 2018 04:04
-
-
Save kveratis/6185d6ded226172ea236bf6978d00052 to your computer and use it in GitHub Desktop.
Example Jest Tests
This file contains hidden or 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
import { createStore } from 'redux'; | |
const INCREASE_COUNT = 'INCREASE_COUNT'; | |
// Action Creator using ES5 Arrow Syntax | |
export const increaseCount = (amount = 1) => ({ | |
type: INCREASE_COUNT, | |
payload: amount, | |
}); | |
const DEFAULT_STATE = { | |
count: 0, | |
nightMode: true, | |
}; | |
export function reducer(state = DEFAULT_STATE, action = {}) { | |
switch(action.type) { | |
case INCREASE_COUNT: | |
return { | |
...state, | |
count: state.count + action.payload, | |
}; | |
} | |
return state; | |
} | |
export const store = createStore(reducer); |
This file contains hidden or 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
import { increaseCount, reducer, store } from './counter'; | |
describe('Counter', function() { | |
it('should have a default state', function() { | |
const result = reducer(); | |
expect(result.count).toEqual(0); | |
}); | |
it('should increase the count', function() { | |
const action = increaseCount(); | |
const result = reducer(undefined, action); | |
expect(result.count).toEqual(1); | |
}); | |
it('should increase the count by custom count value', function() { | |
const action = increaseCount(5); | |
const result = reducer(undefined, action); | |
expect(result.count).toEqual(5); | |
}); | |
}); | |
describe('Sheep counting store', function() { | |
it('should return the state', function() { | |
const state = store.getState(); | |
expect(state.count).toEqual(0); | |
}); | |
it('should dispatch actions and update the state', function() { | |
const action = increaseCount(); | |
store.dispatch(action); | |
expect(store.getState().count).toEqual(1); | |
}); | |
it('should call the subscribers when the store data changes', function() { | |
const listener = jest.fn(); | |
store.subscribe(listener); | |
const action = increaseCount(); | |
store.dispatch(action); | |
expect(listener).toHaveBeenCalled(); | |
}); | |
it('should not call the unsubscribed components when the store data changes', function() { | |
const listener = jest.fn(); | |
const unsubscribe = store.subscribe(listener); | |
const action = increaseCount(); | |
unsubscribe(); | |
store.dispatch(action); | |
expect(listener).not.toHaveBeenCalled(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment