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

Commit

Permalink
fix(reducers): fix tests
Browse files Browse the repository at this point in the history
fix(reducers): fix tests
  • Loading branch information
Metnew committed Dec 5, 2017
1 parent 1e6f1b5 commit b260685
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 83 deletions.
52 changes: 3 additions & 49 deletions src/common/reducers/auth/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import {auth as reducer, initialState} from 'reducers/auth'
import {
LOGIN_AUTH_SUCCESS,
LOGIN_AUTH_FAIL,
LOGIN_AUTH_PENDING,
LOGOUT_AUTH_SUCCESS
} from 'actions/auth'
import {APPLICATION_INIT} from 'actions/common'

describe('AUTH REDUCER', () => {
// Does reducer return `initialState` on empty action type?
Expand All @@ -17,33 +15,19 @@ describe('AUTH REDUCER', () => {

const loggedInState = {
...initialState,
isLoggedIn: true,
isLoading: false,
isLoaded: true,
token: 'iamnotatoken'
isLoggedIn: true
}
// Create test actions for our reducer.

const logoutSuccess = {
type: LOGOUT_AUTH_SUCCESS
}

const loginPending = {
type: LOGIN_AUTH_PENDING
}

const appInit = {
type: APPLICATION_INIT
}

it('should handle LOGOUT_AUTH_SUCCESS if already logged in', () => {
// User is logged out after LOGOUT_AUTH_SUCCESS
expect(reducer(loggedInState, logoutSuccess)).toEqual({
...loggedInState,
errors: {},
isLoggedIn: false,
isLoading: false,
isLoaded: true
isLoggedIn: false
})
})

Expand All @@ -59,12 +43,7 @@ describe('AUTH REDUCER', () => {
// User is logged out and has `errors` after LOGIN_AUTH_FAIL
expect(reducer(initialState, action)).toEqual({
...initialState,
isLoggedIn: false,
isLoaded: true,
isLoading: false,
errors: {
hello: 'world'
}
isLoggedIn: false
})
})

Expand All @@ -78,32 +57,7 @@ describe('AUTH REDUCER', () => {
// User is logged in and has `token` after LOGIN_AUTH_SUCCESS
expect(reducer(initialState, action)).toEqual({
...initialState,
isLoaded: true,
isLoading: false,
isLoggedIn: true
})
})

it('should handle LOGIN_AUTH_PENDING', () => {
// User is logged in and has `token` after LOGIN_AUTH_SUCCESS
expect(reducer(initialState, loginPending)).toEqual({
...initialState,
isLoading: true,
isLoaded: false
})
})

const customState = {
isLoading: false,
isLoaded: true
}

it('should merge initialState with current state on APPLICATION_INIT', () => {
// User is logged in and has `token` after LOGIN_AUTH_SUCCESS
expect(reducer(customState, appInit)).toEqual({
...initialState,
isLoading: false,
isLoaded: true
})
})
})
42 changes: 16 additions & 26 deletions src/common/reducers/layout/index.test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import {layout as reducer, initialState} from 'reducers/layout'
import {
UI_CLOSE_SIDEBAR,
UI_OPEN_SIDEBAR,
UI_WINDOW_RESIZE
} from 'actions/layout'
import {UI_TOGGLE_SIDEBAR, UI_WINDOW_RESIZE} from 'actions/layout'
import {LOCATION_CHANGE} from 'actions/common'

const closeSidebar = {
type: UI_CLOSE_SIDEBAR
}

const openSidebar = {
type: UI_OPEN_SIDEBAR
const toggleSidebar = {
type: UI_TOGGLE_SIDEBAR
}

const locationChange = {
Expand All @@ -37,40 +29,38 @@ describe('LAYOUT REDUCER', () => {
expect(reducer(undefined, {x: 'string'})).toEqual(initialState)
})

it('should handle UI_OPEN_SIDEBAR', () => {
expect(reducer(initialState, openSidebar)).toEqual({
it('should handle UI_TOGGLE_SIDEBAR (if sidebar is opened)', () => {
expect(reducer(initialState, toggleSidebar)).toEqual({
...initialState,
sidebarOpened: true
})
})

it('should handle UI_TOGGLE_SIDEBAR (if sidebar is closed)', () => {
expect(
reducer({...initialState, sidebarOpened: true}, toggleSidebar)
).toEqual({
...initialState,
sidebarOpened: false
})
})

describe('Mobile properties', () => {
it('should handle WINDOW_RESIZE with 360px screen', () => {
expect(reducer(initialState, appInit)).toEqual({
...initialState,
isMobile: true,
isMobileXS: true,
isMobileSM: false
innerWidth: 360
})
})

it('should handle WINDOW_RESIZE with 1280px screen', () => {
expect(reducer(initialState, windowResize)).toEqual({
...initialState,
isMobile: false,
isMobileXS: false,
isMobileSM: false
innerWidth: 1280
})
})
})

it('should handle UI_CLOSE_SIDEBAR', () => {
expect(reducer(initialState, closeSidebar)).toEqual({
...initialState,
sidebarOpened: false
})
})

it('should handle LOCATION_CHANGE', () => {
expect(reducer(initialState, locationChange)).toEqual({
...initialState,
Expand Down
11 changes: 3 additions & 8 deletions src/common/reducers/links/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ describe('LINKS REDUCER', () => {
}
expect(reducer(initialState, success)).toEqual({
...initialState,
count: 1,
entities: payload,
isLoaded: true,
isLoading: false
fetchStatus: 'loaded'
})
})

Expand All @@ -37,12 +35,10 @@ describe('LINKS REDUCER', () => {
}
expect(reducer(initialState, fail)).toEqual({
...initialState,
count: 0,
errors: {
hmm: 'thatsanerror'
},
isLoaded: true,
isLoading: false
fetchStatus: 'loaded'
})
})

Expand All @@ -53,8 +49,7 @@ describe('LINKS REDUCER', () => {
expect(reducer(initialState, pending)).toEqual({
...initialState,
errors: {},
isLoaded: false,
isLoading: true
fetchStatus: 'loading'
})
})
})

0 comments on commit b260685

Please sign in to comment.