From 164c47d1a6b7d28d1c21c76b31758c83b94f1f17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ken-H=C3=A5vard=20Lieng?= Date: Mon, 24 Apr 2017 03:08:48 +0200 Subject: [PATCH] Add tab reducer tests --- client/src/js/__tests__/reducer-tab.test.js | 107 ++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 client/src/js/__tests__/reducer-tab.test.js diff --git a/client/src/js/__tests__/reducer-tab.test.js b/client/src/js/__tests__/reducer-tab.test.js new file mode 100644 index 00000000..85f36092 --- /dev/null +++ b/client/src/js/__tests__/reducer-tab.test.js @@ -0,0 +1,107 @@ +import { LOCATION_CHANGE } from 'react-router-redux'; +import reducer from '../reducers/tab'; +import * as actions from '../actions'; +import { setSelectedTab } from '../actions/tab'; + +describe('reducers/tab', () => { + it('sets the tab and adds it to history', () => { + let state = reducer(undefined, setSelectedTab('srv', '#chan')); + + expect(state.toJS()).toEqual({ + selected: { server: 'srv', name: '#chan' }, + history: [ + { server: 'srv', name: '#chan' } + ] + }); + + state = reducer(state, setSelectedTab('srv', 'user1')); + + expect(state.toJS()).toEqual({ + selected: { server: 'srv', name: 'user1' }, + history: [ + { server: 'srv', name: '#chan' }, + { server: 'srv', name: 'user1' } + ] + }); + }); + + it('removes the tab from history on PART', () => { + let state = reducer(undefined, setSelectedTab('srv', '#chan')); + state = reducer(state, setSelectedTab('srv1', 'bob')); + state = reducer(state, setSelectedTab('srv', '#chan')); + state = reducer(state, setSelectedTab('srv', '#chan3')); + + state = reducer(state, { + type: actions.PART, + server: 'srv', + channels: ['#chan'] + }); + + expect(state.toJS()).toEqual({ + selected: { server: 'srv', name: '#chan3' }, + history: [ + { server: 'srv1', name: 'bob' }, + { server: 'srv', name: '#chan3' } + ] + }); + }); + + it('removes the tab from history on CLOSE_PRIVATE_CHAT', () => { + let state = reducer(undefined, setSelectedTab('srv', '#chan')); + state = reducer(state, setSelectedTab('srv1', 'bob')); + state = reducer(state, setSelectedTab('srv', '#chan')); + state = reducer(state, setSelectedTab('srv', '#chan3')); + + state = reducer(state, { + type: actions.CLOSE_PRIVATE_CHAT, + server: 'srv1', + nick: 'bob' + }); + + expect(state.toJS()).toEqual({ + selected: { server: 'srv', name: '#chan3' }, + history: [ + { server: 'srv', name: '#chan' }, + { server: 'srv', name: '#chan' }, + { server: 'srv', name: '#chan3' } + ] + }); + }); + + it('removes all tabs related to server from history on DISCONNECT', () => { + let state = reducer(undefined, setSelectedTab('srv', '#chan')); + state = reducer(state, setSelectedTab('srv1', 'bob')); + state = reducer(state, setSelectedTab('srv', '#chan')); + state = reducer(state, setSelectedTab('srv', '#chan3')); + + state = reducer(state, { + type: actions.DISCONNECT, + server: 'srv', + }); + + expect(state.toJS()).toEqual({ + selected: { server: 'srv', name: '#chan3' }, + history: [ + { server: 'srv1', name: 'bob' }, + ] + }); + }); + + it('clears the tab when navigating to a non-tab page', () => { + let state = reducer(undefined, setSelectedTab('srv', '#chan')); + + state = reducer(state, { + type: LOCATION_CHANGE, + payload: { + pathname: '/settings' + } + }); + + expect(state.toJS()).toEqual({ + selected: { server: null, name: null }, + history: [ + { server: 'srv', name: '#chan' } + ] + }); + }); +});