From 25478a87fb81811aa3f50433dc456c68e941cc49 Mon Sep 17 00:00:00 2001 From: DD Date: Thu, 14 Sep 2017 11:56:12 -0400 Subject: [PATCH] add tests --- src/reducers/hover.js | 4 ++- test/unit/components/select-mode.test.jsx | 15 +++++++++ test/unit/hover-reducer.test.js | 40 +++++++++++++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 test/unit/components/select-mode.test.jsx create mode 100644 test/unit/hover-reducer.test.js diff --git a/src/reducers/hover.js b/src/reducers/hover.js index 0c4353b0..2a3e31ee 100644 --- a/src/reducers/hover.js +++ b/src/reducers/hover.js @@ -1,3 +1,4 @@ +import paper from 'paper'; import log from '../log/log'; const CHANGE_HOVERED = 'scratch-paint/hover/CHANGE_HOVERED'; @@ -7,7 +8,8 @@ const reducer = function (state, action) { if (typeof state === 'undefined') state = initialState; switch (action.type) { case CHANGE_HOVERED: - if (typeof action.hoveredItem === 'undefined') { + if (typeof action.hoveredItem === 'undefined' || + (action.hoveredItem !== null && !(action.hoveredItem instanceof paper.Item))) { log.warn(`Hovered item should not be set to undefined. Use null.`); return state; } diff --git a/test/unit/components/select-mode.test.jsx b/test/unit/components/select-mode.test.jsx new file mode 100644 index 00000000..3ec1ba29 --- /dev/null +++ b/test/unit/components/select-mode.test.jsx @@ -0,0 +1,15 @@ +/* eslint-env jest */ +import React from 'react'; // eslint-disable-line no-unused-vars +import {shallow} from 'enzyme'; +import SelectModeComponent from '../../../src/components/select-mode.jsx'; // eslint-disable-line no-unused-vars + +describe('SelectModeComponent', () => { + test('triggers callback when clicked', () => { + const onClick = jest.fn(); + const componentShallowWrapper = shallow( + + ); + componentShallowWrapper.simulate('click'); + expect(onClick).toHaveBeenCalled(); + }); +}); diff --git a/test/unit/hover-reducer.test.js b/test/unit/hover-reducer.test.js new file mode 100644 index 00000000..b8c63cd0 --- /dev/null +++ b/test/unit/hover-reducer.test.js @@ -0,0 +1,40 @@ +/* eslint-env jest */ +import paper from 'paper'; +import reducer from '../../src/reducers/hover'; +import {clearHoveredItem, setHoveredItem} from '../../src/reducers/hover'; + +beforeEach(() => { + paper.setup(); +}); + +test('initialState', () => { + let defaultState; + expect(reducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeNull(); +}); + +test('setHoveredItem', () => { + let defaultState; + const item1 = new paper.Path(); + const item2 = new paper.Path(); + expect(reducer(defaultState /* state */, setHoveredItem(item1) /* action */)).toBe(item1); + expect(reducer(item1 /* state */, setHoveredItem(item2) /* action */)).toBe(item2); +}); + +test('clearHoveredItem', () => { + let defaultState; + const item = new paper.Path(); + expect(reducer(defaultState /* state */, clearHoveredItem() /* action */)).toBeNull(); + expect(reducer(item /* state */, clearHoveredItem() /* action */)).toBeNull(); +}); + +test('invalidSetHoveredItem', () => { + let defaultState; + const item = new paper.Path(); + const nonItem = {random: 'object'}; + let undef; + expect(reducer(defaultState /* state */, setHoveredItem(nonItem) /* action */)).toBeNull(); + expect(reducer(item /* state */, setHoveredItem(nonItem) /* action */)) + .toBe(item); + expect(reducer(item /* state */, setHoveredItem(undef) /* action */)) + .toBe(item); +});