From b9750e3c83b2adf8325678e182dfdfe2a71d97c7 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Tue, 10 Oct 2017 11:20:00 +0100 Subject: [PATCH] Don't map over state for reducer tests --- .babelrc | 2 +- package.json | 7 +- src/scripts/src/components/container.test.js | 0 src/scripts/src/components/dropdown.test.js | 0 src/scripts/src/components/input.test.js | 0 src/scripts/src/components/list.test.js | 0 src/scripts/src/reducers/choices.test.js | 118 +++++++++++-------- 7 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 src/scripts/src/components/container.test.js create mode 100644 src/scripts/src/components/dropdown.test.js create mode 100644 src/scripts/src/components/input.test.js create mode 100644 src/scripts/src/components/list.test.js diff --git a/.babelrc b/.babelrc index c13c5f6..0558174 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,3 @@ { - "presets": ["es2015"] + "presets": ["es2015", "stage-2"] } diff --git a/package.json b/package.json index 0809698..d4c28c6 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "scripts": { "start": "node server.js", "lint": "eslint assets/**/*.js", - "test": "nyc mocha --require ./config/test.js --compilers babel-core/register \"./src/**/**/**/**/*.test.js\"", + "test": "nyc mocha --require ./config/test.js --compilers js:babel-core/register \"./src/**/**/**/**/*.test.js\"", "test:watch": "npm run test -- --watch", "css:watch": "nodemon -e scss -x \"npm run css:build\"", "css:build": "npm run css:sass -s && npm run css:prefix -s && npm run css:min -s", @@ -31,10 +31,11 @@ "homepage": "https://github.com/jshjohnson/Choices#readme", "devDependencies": { "autoprefixer": "^6.3.3", - "babel-core": "^6.25.0", + "babel-core": "^6.26.0", "babel-eslint": "^7.2.3", "babel-loader": "^6.2.4", "babel-preset-es2015": "^6.6.0", + "babel-preset-stage-2": "^6.24.1", "chai": "^4.1.0", "concurrently": "^3.1.0", "core-js": "^2.4.1", @@ -48,7 +49,7 @@ "eslint-plugin-jsx-a11y": "^5.1.1", "eslint-plugin-react": "^7.2.1", "jasmine-core": "2.4.1", - "jsdom": "^11.1.0", + "jsdom": "^11.3.0", "mocha": "^3.4.2", "node-sass": "^3.4.2", "nodemon": "^1.9.1", diff --git a/src/scripts/src/components/container.test.js b/src/scripts/src/components/container.test.js new file mode 100644 index 0000000..e69de29 diff --git a/src/scripts/src/components/dropdown.test.js b/src/scripts/src/components/dropdown.test.js new file mode 100644 index 0000000..e69de29 diff --git a/src/scripts/src/components/input.test.js b/src/scripts/src/components/input.test.js new file mode 100644 index 0000000..e69de29 diff --git a/src/scripts/src/components/list.test.js b/src/scripts/src/components/list.test.js new file mode 100644 index 0000000..e69de29 diff --git a/src/scripts/src/reducers/choices.test.js b/src/scripts/src/reducers/choices.test.js index c80d489..9ce9eef 100644 --- a/src/scripts/src/reducers/choices.test.js +++ b/src/scripts/src/reducers/choices.test.js @@ -18,18 +18,6 @@ describe('choices reducer', () => { const customProperties = 'test'; const placeholder = 'test'; - const actualResponse = choices(undefined, { - type: 'ADD_CHOICE', - value, - label, - id, - groupId, - disabled, - elementId, - customProperties, - placeholder, - }); - const expectedResponse = [ { value, @@ -47,6 +35,18 @@ describe('choices reducer', () => { }, ]; + const actualResponse = choices(undefined, { + type: 'ADD_CHOICE', + value, + label, + id, + groupId, + disabled, + elementId, + customProperties, + placeholder, + }); + expect(actualResponse).to.eql(expectedResponse); }); }); @@ -92,7 +92,16 @@ describe('choices reducer', () => { it('sets active flag based on whether choice is in passed results', () => { const id = 1; const score = 10; - const actualResponse = choices([...state], { + const active = true; + + const expectedResponse = { + ...state[0], + active, + score, + }; + + + const actualResponse = choices(state, { type: 'FILTER_CHOICES', results: [{ item: { @@ -100,16 +109,7 @@ describe('choices reducer', () => { }, score, }], - }); - - const expectedResponse = state.map((choice) => { - const clonedChoice = choice; - if (clonedChoice.id === id) { - clonedChoice.active = true; - clonedChoice.score = 10; - } - return clonedChoice; - }); + }).find(choice => choice.id === id); expect(actualResponse).to.eql(expectedResponse); }); @@ -117,29 +117,36 @@ describe('choices reducer', () => { describe('ACTIVATE_CHOICES', () => { it('sets active flag to passed value', () => { - const actualResponse = choices([...state], { + const clonedState = state.slice(0); + + const expectedResponse = [ + { + ...state[0], + active: true, + }, + { + ...state[1], + active: true, + }, + ]; + + const actualResponse = choices(clonedState, { type: 'ACTIVATE_CHOICES', active: true, }); - const expectedResponse = state.map((choice) => { - const clonedChoice = choice; - clonedChoice.active = true; - return clonedChoice; - }); - expect(actualResponse).to.eql(expectedResponse); }); }); describe('CLEAR_CHOICES', () => { it('restores to defaultState', () => { - const actualResponse = choices([...state], { + const clonedState = state.slice(0); + const expectedResponse = defaultState; + const actualResponse = choices(clonedState, { type: 'CLEAR_CHOICES', }); - const expectedResponse = defaultState; - expect(actualResponse).to.eql(expectedResponse); }); }); @@ -147,24 +154,28 @@ describe('choices reducer', () => { describe('ADD_ITEM', () => { it('disables choice if action has choice id', () => { const id = 2; - const actualResponse = choices(state, { + const clonedState = state.slice(0); + const expectedResponse = [ + { + ...state[0], + }, + { + ...state[1], + selected: true, + }, + ]; + + const actualResponse = choices(clonedState, { type: 'ADD_ITEM', choiceId: id, }); - const expectedResponse = state.map((choice) => { - const clonedChoice = choice; - if (clonedChoice.id === id) { - clonedChoice.selected = false; - } - return clonedChoice; - }); - expect(actualResponse).to.eql(expectedResponse); }); it('activates all choices if activateOptions flag passed', () => { - const actualResponse = choices(state, { + const clonedState = state.slice(0); + const actualResponse = choices(clonedState, { type: 'ADD_ITEM', activateOptions: true, active: true, @@ -178,19 +189,22 @@ describe('choices reducer', () => { describe('REMOVE_ITEM', () => { it('selects choice by passed id', () => { const id = 2; - const actualResponse = choices([...state], { + const clonedState = state.slice(0); + const expectedResponse = [ + { + ...state[0], + }, + { + ...state[1], + selected: false, + }, + ]; + + const actualResponse = choices(clonedState, { type: 'REMOVE_ITEM', choiceId: id, }); - const expectedResponse = state.map((choice) => { - const clonedChoice = choice; - if (clonedChoice.id === id) { - clonedChoice.selected = false; - } - return clonedChoice; - }); - expect(actualResponse).to.eql(expectedResponse); }); });