From 8e3ba08ad373c3c9854bd1575240fc70f3a4512c Mon Sep 17 00:00:00 2001 From: Mike Piccolo Date: Tue, 18 Oct 2016 13:59:13 -0700 Subject: [PATCH] Initial reducer tests --- src/reducers/channels.js | 2 +- src/reducers/login.js | 2 +- src/reducers/posts.js | 2 +- src/reducers/teams.js | 2 +- test/reducers/channels.test.js | 94 +++++++++++++++++++++++++++++++++ test/reducers/login.test.js | 75 +++++++++++++++++++++++++++ test/reducers/posts.test.js | 80 ++++++++++++++++++++++++++++ test/reducers/teams.test.js | 95 ++++++++++++++++++++++++++++++++++ 8 files changed, 348 insertions(+), 4 deletions(-) create mode 100644 test/reducers/channels.test.js create mode 100644 test/reducers/login.test.js create mode 100644 test/reducers/posts.test.js create mode 100644 test/reducers/teams.test.js diff --git a/src/reducers/channels.js b/src/reducers/channels.js index 803a05339..fb6bd45ed 100644 --- a/src/reducers/channels.js +++ b/src/reducers/channels.js @@ -1,7 +1,7 @@ import _ from 'lodash'; import {ChannelsTypes as types} from 'constants'; -const initState = { +export const initState = { status: 'not fetched', error: null, data: {}, diff --git a/src/reducers/login.js b/src/reducers/login.js index 0505ca059..ff11db8cf 100644 --- a/src/reducers/login.js +++ b/src/reducers/login.js @@ -3,7 +3,7 @@ import {LoginTypes as types} from 'constants'; -const initState = { +export const initState = { status: 'not fetched', error: null }; diff --git a/src/reducers/posts.js b/src/reducers/posts.js index eaab25913..5f9b3a80d 100644 --- a/src/reducers/posts.js +++ b/src/reducers/posts.js @@ -1,6 +1,6 @@ import {PostsTypes as types} from 'constants'; -const initState = { +export const initState = { status: 'not fetched', error: null, data: {} diff --git a/src/reducers/teams.js b/src/reducers/teams.js index a8324a745..f6e6cd8a1 100644 --- a/src/reducers/teams.js +++ b/src/reducers/teams.js @@ -1,6 +1,6 @@ import {TeamsTypes as types} from 'constants'; -const initState = { +export const initState = { status: 'not fetched', error: null, data: {}, diff --git a/test/reducers/channels.test.js b/test/reducers/channels.test.js new file mode 100644 index 000000000..85b83b1d6 --- /dev/null +++ b/test/reducers/channels.test.js @@ -0,0 +1,94 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import assert from 'assert'; +import reduceChannels, {initState} from 'reducers/channels'; +import {ChannelsTypes as types} from 'constants'; + +describe('channels reducer', () => { + describe('Init', () => { + let store; + let expectedStore; + before(() => { + store = reduceChannels(store, {type: ''}); + expectedStore = {...initState}; + }); + it('should be initial state', () => { + assert.equal(typeof store, 'object'); + }); + it('have a specifc initial state', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.SELECT_CHANNEL}`, () => { + let store; + let expectedStore; + before(() => { + store = reduceChannels(store, { + type: types.SELECT_CHANNEL, + channelId: '1' + }); + expectedStore = { + ...initState, + currentChannelId: '1' + }; + }); + it('should set status to fetching', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_CHANNELS_REQUEST}`, () => { + let store; + let expectedStore; + before(() => { + store = reduceChannels(store, { + type: types.FETCH_CHANNELS_REQUEST + }); + expectedStore = { + ...initState, + status: 'fetching' + }; + }); + it('should set status to fetching', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_CHANNELS_SUCCESS}`, () => { + let store; + let expectedStore; + before(() => { + store = reduceChannels(store, { + type: types.FETCH_CHANNELS_SUCCESS, + data: {channels: [{id: '1', attr: 'attr'}]} + }); + expectedStore = { + ...initState, + status: 'fetched', + data: {1: {id: '1', attr: 'attr'}} + }; + }); + it('should set status to fetched and data', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_CHANNELS_FAILURE}`, () => { + let store; + let expectedStore; + let error; + before(() => { + error = {id: 'the.error.id', message: 'Something went wrong'}; + store = reduceChannels(store, { + type: types.FETCH_CHANNELS_FAILURE, + error + }); + expectedStore = { + ...initState, + status: 'failed', + error + }; + }); + it('should set status to failed and error', () => { + assert.deepEqual(store, expectedStore); + }); + }); +}); diff --git a/test/reducers/login.test.js b/test/reducers/login.test.js new file mode 100644 index 000000000..42284a76c --- /dev/null +++ b/test/reducers/login.test.js @@ -0,0 +1,75 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import assert from 'assert'; +import reduceLogin, {initState} from 'reducers/login'; +import {LoginTypes as types} from 'constants'; + +describe('login reducer', () => { + describe('Init', () => { + let store; + let expectedStore; + before(() => { + store = reduceLogin(store, {type: ''}); + expectedStore = {...initState}; + }); + it('should be initial state', () => { + assert.equal(typeof store, 'object'); + }); + it('have a specifc initial state', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.LOGIN_REQUEST}`, () => { + let store; + let expectedStore; + before(() => { + store = reduceLogin(store, { + type: types.LOGIN_REQUEST + }); + expectedStore = { + ...initState, + status: 'fetching' + }; + }); + it('should set status to fetching', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.LOGIN_SUCCESS}`, () => { + let store; + let expectedStore; + before(() => { + store = reduceLogin(store, { + type: types.LOGIN_SUCCESS + }); + expectedStore = { + ...initState, + status: 'fetched' + }; + }); + it('should set status to fetched and data', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.LOGIN_FAILURE}`, () => { + let store; + let error; + let expectedStore; + before(() => { + error = {id: 'the.error.id', message: 'Something went wrong'}; + store = reduceLogin(store, { + type: types.LOGIN_FAILURE, + error + }); + expectedStore = { + ...initState, + status: 'failed', + error + }; + }); + it('should set status to failed and error', () => { + assert.deepEqual(store, expectedStore); + }); + }); +}); diff --git a/test/reducers/posts.test.js b/test/reducers/posts.test.js new file mode 100644 index 000000000..c582c83e8 --- /dev/null +++ b/test/reducers/posts.test.js @@ -0,0 +1,80 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import assert from 'assert'; +import reducePosts, {initState} from 'reducers/posts'; +import {PostsTypes as types} from 'constants'; + +describe('posts reducer', () => { + describe('Init', () => { + let store; + let expectedStore; + before(() => { + store = reducePosts(store, {type: ''}); + expectedStore = {...initState}; + }); + it('should be initial state', () => { + assert.equal(typeof store, 'object'); + }); + it('have a specifc initial state', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_POSTS_REQUEST}`, () => { + let store; + let expectedStore; + before(() => { + store = reducePosts(store, { + type: types.FETCH_POSTS_REQUEST + }); + expectedStore = { + ...initState, + status: 'fetching' + }; + }); + it('should set status to fetching', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_POSTS_SUCCESS}`, () => { + let store; + let expectedStore; + const data = {id: '1', attrs: 'attrs'}; + before(() => { + store = reducePosts(store, { + type: types.FETCH_POSTS_SUCCESS, + data: { + posts: data + } + }); + expectedStore = { + ...initState, + status: 'fetched', + data + }; + }); + it('should set status to fetched and data', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_POSTS_FAILURE}`, () => { + let store; + let error; + let expectedStore; + before(() => { + error = {id: 'the.error.id', message: 'Something went wrong'}; + store = reducePosts(store, { + type: types.FETCH_POSTS_FAILURE, + error + }); + expectedStore = { + ...initState, + status: 'failed', + error + }; + }); + it('should set status to failed and error', () => { + assert.deepEqual(store, expectedStore); + }); + }); +}); diff --git a/test/reducers/teams.test.js b/test/reducers/teams.test.js new file mode 100644 index 000000000..29dd00230 --- /dev/null +++ b/test/reducers/teams.test.js @@ -0,0 +1,95 @@ +// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +import assert from 'assert'; +import reduceTeams, {initState} from 'reducers/teams'; +import {TeamsTypes as types} from 'constants'; + +describe('teams reducer', () => { + describe('Init', () => { + let store; + let expectedStore; + before(() => { + store = reduceTeams(store, {type: ''}); + expectedStore = {...initState}; + }); + it('should be initial state', () => { + assert.equal(typeof store, 'object'); + }); + it('have a specifc initial state', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.SELECT_TEAM}`, () => { + let store; + let expectedStore; + before(() => { + store = reduceTeams(store, { + type: types.SELECT_TEAM, + teamId: '1' + }); + expectedStore = { + ...initState, + currentTeamId: '1' + }; + }); + it('should set status to fetching', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_TEAMS_REQUEST}`, () => { + let store; + let expectedStore; + before(() => { + store = reduceTeams(store, { + type: types.FETCH_TEAMS_REQUEST + }); + expectedStore = { + ...initState, + status: 'fetching' + }; + }); + it('should set status to fetching', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_TEAMS_SUCCESS}`, () => { + let store; + let data = {some: 'thing'}; + let expectedStore; + before(() => { + store = reduceTeams(store, { + type: types.FETCH_TEAMS_SUCCESS, + data + }); + expectedStore = { + ...initState, + status: 'fetched', + data + }; + }); + it('should set status to fetched and data', () => { + assert.deepEqual(store, expectedStore); + }); + }); + describe(`when ${types.FETCH_TEAMS_FAILURE}`, () => { + let store; + let expectedStore; + let error; + before(() => { + error = {id: 'the.error.id', message: 'Something went wrong'}; + store = reduceTeams(store, { + type: types.FETCH_TEAMS_FAILURE, + error + }); + expectedStore = { + ...initState, + status: 'failed', + error + }; + }); + it('should set status to failed and error', () => { + assert.deepEqual(store, expectedStore); + }); + }); +});