Update color reducer tests

This commit is contained in:
adroitwhiz 2020-04-15 12:28:52 -04:00
parent 6094953ef4
commit 2df22838b4
3 changed files with 86 additions and 147 deletions

View file

@ -0,0 +1,86 @@
/* eslint-env jest */
import fillColorReducer, {changeFillColor} from '../../src/reducers/fill-style';
import strokeColorReducer, {changeStrokeColor} from '../../src/reducers/stroke-style';
import {setSelectedItems} from '../../src/reducers/selected-items';
import {MIXED} from '../../src/helper/style-path';
import GradientTypes from '../../src/lib/gradient-types';
import {mockPaperRootItem} from '../__mocks__/paperMocks';
for (const [colorReducer, changeColor, colorProp] of [
[fillColorReducer, changeFillColor, 'fillColor'],
[strokeColorReducer, changeStrokeColor, 'strokeColor']
]) {
test('initialState', () => {
let defaultState;
expect(colorReducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeDefined();
});
test('changeColor', () => {
let defaultState;
// 3 value hex code
let newColor = '#fff';
expect(colorReducer(defaultState /* state */, changeColor(newColor) /* action */).primary)
.toEqual(newColor);
expect(colorReducer({
primary: '#010',
secondary: null,
gradientType: GradientTypes.SOLID
} /* state */, changeColor(newColor) /* action */).primary)
.toEqual(newColor);
// 6 value hex code
newColor = '#facade';
expect(colorReducer(defaultState /* state */, changeColor(newColor) /* action */).primary)
.toEqual(newColor);
expect(colorReducer({
primary: '#010',
secondary: null,
gradientType: GradientTypes.SOLID
} /* state */, changeColor(newColor) /* action */).primary)
.toEqual(newColor);
});
test('changeColorViaSelectedItems', () => {
let defaultState;
const color1 = 6;
const color2 = null; // transparent
let selectedItems = [mockPaperRootItem({[colorProp]: color1, strokeWidth: 1})];
expect(colorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */).primary)
.toEqual(color1);
selectedItems = [mockPaperRootItem({[colorProp]: color2, strokeWidth: 1})];
expect(colorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */).primary)
.toEqual(color2);
selectedItems = [
mockPaperRootItem({[colorProp]: color1, strokeWidth: 1}),
mockPaperRootItem({[colorProp]: color2, strokeWidth: 1})
];
expect(colorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */).primary)
.toEqual(MIXED);
});
test('invalidChangeColor', () => {
const origState = {primary: '#fff', secondary: null, gradientType: GradientTypes.SOLID};
expect(colorReducer(origState /* state */, changeColor() /* action */))
.toBe(origState);
expect(colorReducer(origState /* state */, changeColor('#') /* action */))
.toBe(origState);
expect(colorReducer(origState /* state */, changeColor('#1') /* action */))
.toBe(origState);
expect(colorReducer(origState /* state */, changeColor('#12') /* action */))
.toBe(origState);
expect(colorReducer(origState /* state */, changeColor('#1234') /* action */))
.toBe(origState);
expect(colorReducer(origState /* state */, changeColor('#12345') /* action */))
.toBe(origState);
expect(colorReducer(origState /* state */, changeColor('#1234567') /* action */))
.toBe(origState);
expect(colorReducer(origState /* state */, changeColor('invalid argument') /* action */))
.toBe(origState);
});
}

View file

@ -1,68 +0,0 @@
/* eslint-env jest */
import fillColorReducer from '../../src/reducers/fill-style';
import {changeFillColor} from '../../src/reducers/fill-style';
import {setSelectedItems} from '../../src/reducers/selected-items';
import {MIXED} from '../../src/helper/style-path';
import GradientTypes from '../../src/lib/gradient-types';
import {mockPaperRootItem} from '../__mocks__/paperMocks';
test('initialState', () => {
let defaultState;
expect(fillColorReducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeDefined();
});
test('changeFillColor', () => {
let defaultState;
// 3 value hex code
let newFillColor = '#fff';
expect(fillColorReducer(defaultState /* state */, changeFillColor(newFillColor) /* action */).primary)
.toEqual(newFillColor);
expect(fillColorReducer('#010' /* state */, changeFillColor(newFillColor) /* action */).primary)
.toEqual(newFillColor);
// 6 value hex code
newFillColor = '#facade';
expect(fillColorReducer(defaultState /* state */, changeFillColor(newFillColor) /* action */).primary)
.toEqual(newFillColor);
expect(fillColorReducer('#010' /* state */, changeFillColor(newFillColor) /* action */).primary)
.toEqual(newFillColor);
});
test('changefillColorViaSelectedItems', () => {
let defaultState;
const fillColor1 = 6;
const fillColor2 = null; // transparent
let selectedItems = [mockPaperRootItem({fillColor: fillColor1})];
expect(fillColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */).primary)
.toEqual(fillColor1);
selectedItems = [mockPaperRootItem({fillColor: fillColor2})];
expect(fillColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */).primary)
.toEqual(fillColor2);
selectedItems = [mockPaperRootItem({fillColor: fillColor1}), mockPaperRootItem({fillColor: fillColor2})];
expect(fillColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */).primary)
.toEqual(MIXED);
});
test('invalidChangeFillColor', () => {
const origState = {primary: '#fff', secondary: null, gradientType: GradientTypes.SOLID};
expect(fillColorReducer(origState /* state */, changeFillColor() /* action */))
.toBe(origState);
expect(fillColorReducer(origState /* state */, changeFillColor('#') /* action */))
.toBe(origState);
expect(fillColorReducer(origState /* state */, changeFillColor('#1') /* action */))
.toBe(origState);
expect(fillColorReducer(origState /* state */, changeFillColor('#12') /* action */))
.toBe(origState);
expect(fillColorReducer(origState /* state */, changeFillColor('#1234') /* action */))
.toBe(origState);
expect(fillColorReducer(origState /* state */, changeFillColor('#12345') /* action */))
.toBe(origState);
expect(fillColorReducer(origState /* state */, changeFillColor('#1234567') /* action */))
.toBe(origState);
expect(fillColorReducer(origState /* state */, changeFillColor('invalid argument') /* action */))
.toBe(origState);
});

View file

@ -1,79 +0,0 @@
/* eslint-env jest */
import strokeColorReducer from '../../src/reducers/stroke-color';
import {changeStrokeColor} from '../../src/reducers/stroke-color';
import {setSelectedItems} from '../../src/reducers/selected-items';
import {MIXED} from '../../src/helper/style-path';
import {mockPaperRootItem} from '../__mocks__/paperMocks';
test('initialState', () => {
let defaultState;
expect(strokeColorReducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeDefined();
});
test('changeStrokeColor', () => {
let defaultState;
// 3 value hex code
let newStrokeColor = '#fff';
expect(strokeColorReducer(defaultState /* state */, changeStrokeColor(newStrokeColor) /* action */))
.toEqual(newStrokeColor);
expect(strokeColorReducer('#010' /* state */, changeStrokeColor(newStrokeColor) /* action */))
.toEqual(newStrokeColor);
// 6 value hex code
newStrokeColor = '#facade';
expect(strokeColorReducer(defaultState /* state */, changeStrokeColor(newStrokeColor) /* action */))
.toEqual(newStrokeColor);
expect(strokeColorReducer('#010' /* state */, changeStrokeColor(newStrokeColor) /* action */))
.toEqual(newStrokeColor);
});
test('changeStrokeColorViaSelectedItems', () => {
let defaultState;
const strokeColor1 = 6;
const strokeColor2 = null; // transparent
let selectedItems = [mockPaperRootItem({strokeColor: strokeColor1, strokeWidth: 1})];
expect(strokeColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
.toEqual(strokeColor1);
selectedItems = [mockPaperRootItem({strokeColor: strokeColor2, strokeWidth: 1})];
expect(strokeColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
.toEqual(strokeColor2);
selectedItems = [mockPaperRootItem({strokeColor: strokeColor1, strokeWidth: 1}),
mockPaperRootItem({strokeColor: strokeColor2, strokeWidth: 1})];
expect(strokeColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
.toEqual(MIXED);
});
test('showNoStrokeColorIfNoStrokeWidth', () => {
let defaultState;
let selectedItems = [mockPaperRootItem({strokeColor: '#fff', strokeWidth: null})];
expect(strokeColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
.toEqual(null);
selectedItems = [mockPaperRootItem({strokeColor: '#fff', strokeWidth: 0})];
expect(strokeColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
.toEqual(null);
});
test('invalidChangeStrokeColor', () => {
const origState = '#fff';
expect(strokeColorReducer(origState /* state */, changeStrokeColor() /* action */))
.toBe(origState);
expect(strokeColorReducer(origState /* state */, changeStrokeColor('#') /* action */))
.toBe(origState);
expect(strokeColorReducer(origState /* state */, changeStrokeColor('#1') /* action */))
.toBe(origState);
expect(strokeColorReducer(origState /* state */, changeStrokeColor('#12') /* action */))
.toBe(origState);
expect(strokeColorReducer(origState /* state */, changeStrokeColor('#1234') /* action */))
.toBe(origState);
expect(strokeColorReducer(origState /* state */, changeStrokeColor('#12345') /* action */))
.toBe(origState);
expect(strokeColorReducer(origState /* state */, changeStrokeColor('#1234567') /* action */))
.toBe(origState);
expect(strokeColorReducer(origState /* state */, changeStrokeColor('invalid argument') /* action */))
.toBe(origState);
});