Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
fix: move tests in own describe block
Browse files Browse the repository at this point in the history
fix: move tests in own describe block
  • Loading branch information
Metnew committed Oct 30, 2017
1 parent e41ea18 commit 1a61c67
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 151 deletions.
138 changes: 71 additions & 67 deletions src/common/actions/auth/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,85 +17,89 @@ const middlewares = [thunk]
// Create mockStore for testing
const mockStore = configureMockStore(middlewares)

const loginPending = {
meta: null,
type: LOGIN_AUTH_PENDING
}

describe('Auth actions', () => {
/**
describe('LOGIN_AUTH', () => {
const loginPending = {
meta: null,
type: LOGIN_AUTH_PENDING
}
/**
* @arg {Function} done - is a callback that you need to execute,
* If your action performing async task (e.g. request to API)
*/
test('creates LOGIN_AUTH_SUCCESS when LOGIN_AUTH was successful', done => {
const successPayload = {
token: 'nothing'
}

nock('http://localhost:3000/api/v1')
.post('/auth')
.reply(200, successPayload)
// Create expected output of your action
const expectedActions = [
loginPending,
{
type: LOGIN_AUTH_SUCCESS,
payload: successPayload
test('creates LOGIN_AUTH_SUCCESS when LOGIN_AUTH was successful', done => {
const successPayload = {
token: 'nothing'
}
]
// Create store for testing
const store = mockStore({})
// Dispatch action
store.dispatch(LOGIN_AUTH()).then(() => {
// Compare expected and real outputs
expect(store.getActions()).toEqual(expectedActions)
// Call `done()` callback, because action is async
done()
})
})

test('creates LOGIN_AUTH_FAIL when LOGIN_AUTH was unsuccessful', done => {
// Create expected output of your action
const errorPayload = {
errors: {}
}
nock(process.env.BASE_API)
.post('/auth')
.reply(200, successPayload)
// Create expected output of your action
const expectedActions = [
loginPending,
{
type: LOGIN_AUTH_SUCCESS,
payload: successPayload
}
]
// Create store for testing
const store = mockStore({})
// Dispatch action
store.dispatch(LOGIN_AUTH()).then(() => {
// Compare expected and real outputs
expect(store.getActions()).toEqual(expectedActions)
// Call `done()` callback, because action is async
done()
})
})

const expectedActions = [
loginPending,
{
type: LOGIN_AUTH_FAIL,
error: true,
meta: null,
payload: errorPayload
test('creates LOGIN_AUTH_FAIL when LOGIN_AUTH was unsuccessful', done => {
// Create expected output of your action
const errorPayload = {
errors: {}
}
]

nock('http://localhost:3000/api/v1')
.post('/auth')
.reply(400, errorPayload)
// Create store for testing
const store = mockStore({})
// Dispatch action
store.dispatch(LOGIN_AUTH()).then(res => {
// Compare expected and real outputs
expect(store.getActions()).toEqual(expectedActions)
// Call `done()`, because test is async
done()
const expectedActions = [
loginPending,
{
type: LOGIN_AUTH_FAIL,
error: true,
meta: null,
payload: errorPayload
}
]

nock(process.env.BASE_API)
.post('/auth')
.reply(400, errorPayload)
// Create store for testing
const store = mockStore({})
// Dispatch action
store.dispatch(LOGIN_AUTH()).then(res => {
// Compare expected and real outputs
expect(store.getActions()).toEqual(expectedActions)
// Call `done()`, because test is async
done()
})
})
})

test('creates LOGOUT_AUTH_SUCCESS on LOGOUT_AUTH', () => {
// Create expected output of your action
const expectedActions = [
{
type: LOGOUT_AUTH_SUCCESS
}
]
// Create store for testing
const store = mockStore({})
// Dispatch action
store.dispatch(LOGOUT_AUTH())
// Compare expected and real outputs
expect(store.getActions()).toEqual(expectedActions)
describe('LOGOUT_AUTH', () => {
test('creates LOGOUT_AUTH_SUCCESS on LOGOUT_AUTH', () => {
// Create expected output of your action
const expectedActions = [
{
type: LOGOUT_AUTH_SUCCESS
}
]
// Create store for testing
const store = mockStore({})
// Dispatch action
store.dispatch(LOGOUT_AUTH())
// Compare expected and real outputs
expect(store.getActions()).toEqual(expectedActions)
})
})
})
87 changes: 45 additions & 42 deletions src/common/actions/links/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,59 @@ import {
const middlewares = [thunk]
const mockStore = configureMockStore(middlewares)

const pending = {
meta: null,
type: GET_LINKS_PENDING
}

describe('Links actions', () => {
it('creates GET_LINKS_SUCCESS when GET_LINKS was successful', done => {
const store = mockStore({})
const samplePayload = [
{
link: 'string',
header: 'string'
}
]
nock('http://localhost:3000/api/v1')
.get('/links')
.reply(200, samplePayload)
describe('GET_LINKS', () => {
const pending = {
meta: null,
type: GET_LINKS_PENDING
}

it('creates GET_LINKS_SUCCESS when GET_LINKS was successful', done => {
const store = mockStore({})
const payload = [
{
link: 'string',
header: 'string'
}
]

nock(process.env.BASE_API)
.get('/links')
.reply(200, payload)

return store.dispatch(GET_LINKS()).then(res => {
const actions = store.getActions()
const success = {
meta: null,
type: GET_LINKS_SUCCESS,
payload
}
const expectedActions = [pending, success]

expect(actions).toEqual(expectedActions)
done()
})
})

it('creates GET_LINKS_FAIL when GET_LINKS was unsuccessful', async done => {
const payload = {errors: {}}
nock(process.env.BASE_API)
.get('/links')
.reply(400, payload)

return store.dispatch(GET_LINKS()).then(res => {
const store = mockStore({})
await store.dispatch(GET_LINKS())
const actions = store.getActions()
const success = {
const fail = {
meta: null,
type: GET_LINKS_SUCCESS,
payload: samplePayload
type: GET_LINKS_FAIL,
error: true,
payload
}
const expectedActions = [pending, success]
const expectedActions = [pending, fail]

expect(actions).toEqual(expectedActions)
done()
})
})

it('creates GET_LINKS_FAIL when GET_LINKS was unsuccessful', async done => {
const errorPayload = {errors: {}}
nock('http://localhost:3000/api/v1')
.get('/links')
.reply(400, errorPayload)

const store = mockStore({})
await store.dispatch(GET_LINKS())
const actions = store.getActions()
const fail = {
meta: null,
type: GET_LINKS_FAIL,
error: true,
payload: errorPayload
}
const expectedActions = [pending, fail]

expect(actions).toEqual(expectedActions)
done()
})
})
38 changes: 19 additions & 19 deletions src/common/reducers/auth/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,6 @@ describe('AUTH REDUCER', () => {
type: LOGOUT_AUTH_SUCCESS
}

const loginFail = {
type: LOGIN_AUTH_FAIL,
payload: {
errors: {
hello: 'world'
}
}
}

const loginSuccess = {
type: LOGIN_AUTH_SUCCESS,
payload: {
token: 'iamnotatoken'
}
}

const loginPending = {
type: LOGIN_AUTH_PENDING
}
Expand All @@ -64,19 +48,35 @@ describe('AUTH REDUCER', () => {
})

it('should handle LOGIN_AUTH_FAIL if not logged in', () => {
const action = {
type: LOGIN_AUTH_FAIL,
payload: {
errors: {
hello: 'world'
}
}
}
// User is logged out and has `errors` after LOGIN_AUTH_FAIL
expect(reducer(initialState, loginFail)).toEqual({
expect(reducer(initialState, action)).toEqual({
...initialState,
isLoggedIn: false,
isLoaded: true,
isLoading: false,
errors: loginFail.payload.errors
errors: {
hello: 'world'
}
})
})

it('should handle LOGIN_AUTH_SUCCESS if not logged in', () => {
const action = {
type: LOGIN_AUTH_SUCCESS,
payload: {
token: 'iamnotatoken'
}
}
// User is logged in and has `token` after LOGIN_AUTH_SUCCESS
expect(reducer(initialState, loginSuccess)).toEqual({
expect(reducer(initialState, action)).toEqual({
...initialState,
isLoaded: true,
isLoading: false,
Expand Down
43 changes: 20 additions & 23 deletions src/common/reducers/links/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,37 @@ import {
GET_LINKS_PENDING
} from 'actions/links'

const fail = {
type: GET_LINKS_FAIL,
payload: {
errors: {
hmm: 'thatsanerror'
}
}
}

const samplePayloadItem = {
item: 'payload'
}

const success = {
type: GET_LINKS_SUCCESS,
payload: samplePayloadItem
}

const pending = {
type: GET_LINKS_PENDING
}

describe('LINKS REDUCER', () => {
it('should return the initial state', () => {
expect(reducer(undefined, {x: 'string'})).toEqual(initialState)
})

it('should handle GET_LINKS_SUCCESS', () => {
const payload = {
item: 'payload'
}

const success = {
type: GET_LINKS_SUCCESS,
payload
}
expect(reducer(initialState, success)).toEqual({
...initialState,
entities: samplePayloadItem,
entities: payload,
isLoaded: true,
isLoading: false
})
})

it('should handle GET_LINKS_FAIL', () => {
const fail = {
type: GET_LINKS_FAIL,
payload: {
errors: {
hmm: 'thatsanerror'
}
}
}
expect(reducer(initialState, fail)).toEqual({
...initialState,
errors: {
Expand All @@ -53,6 +47,9 @@ describe('LINKS REDUCER', () => {
})

it('should handle GET_LINKS_PENDING', () => {
const pending = {
type: GET_LINKS_PENDING
}
expect(reducer(initialState, pending)).toEqual({
...initialState,
errors: {},
Expand Down

0 comments on commit 1a61c67

Please sign in to comment.