add tests

This commit is contained in:
DD 2017-09-14 11:56:12 -04:00
parent 6048c9f866
commit 25478a87fb
3 changed files with 58 additions and 1 deletions

View file

@ -1,3 +1,4 @@
import paper from 'paper';
import log from '../log/log'; import log from '../log/log';
const CHANGE_HOVERED = 'scratch-paint/hover/CHANGE_HOVERED'; const CHANGE_HOVERED = 'scratch-paint/hover/CHANGE_HOVERED';
@ -7,7 +8,8 @@ const reducer = function (state, action) {
if (typeof state === 'undefined') state = initialState; if (typeof state === 'undefined') state = initialState;
switch (action.type) { switch (action.type) {
case CHANGE_HOVERED: 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.`); log.warn(`Hovered item should not be set to undefined. Use null.`);
return state; return state;
} }

View file

@ -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(
<SelectModeComponent onMouseDown={onClick}/>
);
componentShallowWrapper.simulate('click');
expect(onClick).toHaveBeenCalled();
});
});

View file

@ -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);
});