mirror of
https://github.com/scratchfoundation/scratch-paint.git
synced 2024-12-22 21:42:30 -05:00
add tests
This commit is contained in:
parent
7a9399d0db
commit
720f83605d
11 changed files with 138 additions and 7 deletions
|
@ -65,7 +65,7 @@ const SelectionHOC = function (WrappedComponent) {
|
||||||
if (selectedItems.length === 0) {
|
if (selectedItems.length === 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const colorState = getColorsFromSelection();
|
const colorState = getColorsFromSelection(selectedItems);
|
||||||
dispatch(changeFillColor(colorState.fillColor));
|
dispatch(changeFillColor(colorState.fillColor));
|
||||||
dispatch(changeStrokeColor(colorState.strokeColor));
|
dispatch(changeStrokeColor(colorState.strokeColor));
|
||||||
dispatch(changeStrokeWidth(colorState.strokeWidth));
|
dispatch(changeStrokeWidth(colorState.strokeWidth));
|
||||||
|
|
|
@ -82,12 +82,12 @@ const applyStrokeWidthToSelection = function (value) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get state of colors and stroke width for selection
|
* Get state of colors and stroke width for selection
|
||||||
|
* @param {!Array<paper.Item>} selectedItems Selected paper items
|
||||||
* @return {object} Object of strokeColor, strokeWidth, fillColor of the selection.
|
* @return {object} Object of strokeColor, strokeWidth, fillColor of the selection.
|
||||||
* Gives MIXED when there are mixed values for a color, and null for transparent.
|
* Gives MIXED when there are mixed values for a color, and null for transparent.
|
||||||
* Gives null when there are mixed values for stroke width.
|
* Gives null when there are mixed values for stroke width.
|
||||||
*/
|
*/
|
||||||
const getColorsFromSelection = function () {
|
const getColorsFromSelection = function (selectedItems) {
|
||||||
const selectedItems = getSelectedItems(true /* recursive */);
|
|
||||||
let selectionFillColorString;
|
let selectionFillColorString;
|
||||||
let selectionStrokeColorString;
|
let selectionStrokeColorString;
|
||||||
let selectionStrokeWidth;
|
let selectionStrokeWidth;
|
||||||
|
|
|
@ -21,7 +21,7 @@ const reducer = function (state, action) {
|
||||||
if (!action.selectedItems || !action.selectedItems.length) {
|
if (!action.selectedItems || !action.selectedItems.length) {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
return getColorsFromSelection().fillColor;
|
return getColorsFromSelection(action.selectedItems).fillColor;
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import log from '../log/log';
|
||||||
const CHANGE_SELECTED_ITEMS = 'scratch-paint/select/CHANGE_SELECTED_ITEMS';
|
const CHANGE_SELECTED_ITEMS = 'scratch-paint/select/CHANGE_SELECTED_ITEMS';
|
||||||
const initialState = [];
|
const initialState = [];
|
||||||
|
|
||||||
|
@ -5,11 +6,15 @@ 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_SELECTED_ITEMS:
|
case CHANGE_SELECTED_ITEMS:
|
||||||
|
if (!action.selectedItems || !(action.selectedItems instanceof Array)) {
|
||||||
|
log.warn(`No selected items or wrong format provided: ${action.selectedItems}`);
|
||||||
|
return state;
|
||||||
|
}
|
||||||
// If they are not equal, return the new list of items. Else return old list
|
// If they are not equal, return the new list of items. Else return old list
|
||||||
if (action.selectedItems.length !== state.length) {
|
if (action.selectedItems.length !== state.length) {
|
||||||
return action.selectedItems;
|
return action.selectedItems;
|
||||||
}
|
}
|
||||||
// Shallow equality check
|
// Shallow equality check (we may need to update this later for more granularity)
|
||||||
for (let i = 0; i < action.selectedItems.length; i++) {
|
for (let i = 0; i < action.selectedItems.length; i++) {
|
||||||
if (action.selectedItems[i] !== state[i]) {
|
if (action.selectedItems[i] !== state[i]) {
|
||||||
return action.selectedItems;
|
return action.selectedItems;
|
||||||
|
|
|
@ -21,7 +21,7 @@ const reducer = function (state, action) {
|
||||||
if (!action.selectedItems || !action.selectedItems.length) {
|
if (!action.selectedItems || !action.selectedItems.length) {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
return getColorsFromSelection().strokeColor;
|
return getColorsFromSelection(action.selectedItems).strokeColor;
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ const reducer = function (state, action) {
|
||||||
if (!action.selectedItems || !action.selectedItems.length) {
|
if (!action.selectedItems || !action.selectedItems.length) {
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
return getColorsFromSelection().strokeWidth;
|
return getColorsFromSelection(action.selectedItems).strokeWidth;
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
23
test/__mocks__/paperMocks.js
Normal file
23
test/__mocks__/paperMocks.js
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
/**
|
||||||
|
* Pretend paper.Item whose parent is a layer.
|
||||||
|
* @param {object} options Item params
|
||||||
|
* @param {string} options.strokeColor Value to return for the item's stroke color
|
||||||
|
* @param {string} options.fillColor Value to return for the item's fill color
|
||||||
|
* @param {string} options.strokeWidth Value to return for the item's stroke width
|
||||||
|
* @return {object} mock item
|
||||||
|
*/
|
||||||
|
const mockPaperRootItem = function (options) {
|
||||||
|
return {
|
||||||
|
strokeColor: {toCSS: function () {
|
||||||
|
return options.strokeColor;
|
||||||
|
}},
|
||||||
|
fillColor: {toCSS: function () {
|
||||||
|
return options.fillColor;
|
||||||
|
}},
|
||||||
|
strokeWidth: options.strokeWidth,
|
||||||
|
parent: {className: 'Layer'},
|
||||||
|
data: {}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export {mockPaperRootItem};
|
|
@ -1,6 +1,9 @@
|
||||||
/* eslint-env jest */
|
/* eslint-env jest */
|
||||||
import fillColorReducer from '../../src/reducers/fill-color';
|
import fillColorReducer from '../../src/reducers/fill-color';
|
||||||
import {changeFillColor} from '../../src/reducers/fill-color';
|
import {changeFillColor} from '../../src/reducers/fill-color';
|
||||||
|
import {setSelectedItems} from '../../src/reducers/selected-items';
|
||||||
|
import {MIXED} from '../../src/helper/style-path';
|
||||||
|
import {mockPaperRootItem} from '../__mocks__/paperMocks';
|
||||||
|
|
||||||
test('initialState', () => {
|
test('initialState', () => {
|
||||||
let defaultState;
|
let defaultState;
|
||||||
|
@ -26,6 +29,22 @@ test('changeFillColor', () => {
|
||||||
.toEqual(newFillColor);
|
.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 */))
|
||||||
|
.toEqual(fillColor1);
|
||||||
|
selectedItems = [mockPaperRootItem({fillColor: fillColor2})];
|
||||||
|
expect(fillColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
|
||||||
|
.toEqual(fillColor2);
|
||||||
|
selectedItems = [mockPaperRootItem({fillColor: fillColor1}), mockPaperRootItem({fillColor: fillColor2})];
|
||||||
|
expect(fillColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
|
||||||
|
.toEqual(MIXED);
|
||||||
|
});
|
||||||
|
|
||||||
test('invalidChangeFillColor', () => {
|
test('invalidChangeFillColor', () => {
|
||||||
const origState = '#fff';
|
const origState = '#fff';
|
||||||
|
|
||||||
|
|
47
test/unit/selected-items-reducer.test.js
Normal file
47
test/unit/selected-items-reducer.test.js
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/* eslint-env jest */
|
||||||
|
import selectedItemsReducer from '../../src/reducers/selected-items';
|
||||||
|
import {setSelectedItems, clearSelectedItems} from '../../src/reducers/selected-items';
|
||||||
|
|
||||||
|
test('initialState', () => {
|
||||||
|
let defaultState;
|
||||||
|
|
||||||
|
expect(selectedItemsReducer(defaultState /* state */, {type: 'anything'} /* action */)).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('setSelectedItems', () => {
|
||||||
|
let defaultState;
|
||||||
|
|
||||||
|
const newSelected1 = ['selected1', 'selected2'];
|
||||||
|
const newSelected2 = ['selected1', 'selected3'];
|
||||||
|
const unselected = [];
|
||||||
|
expect(selectedItemsReducer(defaultState /* state */, setSelectedItems(newSelected1) /* action */))
|
||||||
|
.toEqual(newSelected1);
|
||||||
|
expect(selectedItemsReducer(newSelected1, setSelectedItems(newSelected2) /* action */))
|
||||||
|
.toEqual(newSelected2);
|
||||||
|
expect(selectedItemsReducer(newSelected1, setSelectedItems(unselected) /* action */))
|
||||||
|
.toEqual(unselected);
|
||||||
|
expect(selectedItemsReducer(defaultState, setSelectedItems(unselected) /* action */))
|
||||||
|
.toEqual(unselected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('clearSelectedItems', () => {
|
||||||
|
let defaultState;
|
||||||
|
|
||||||
|
const selectedState = ['selected1', 'selected2'];
|
||||||
|
const unselectedState = [];
|
||||||
|
expect(selectedItemsReducer(defaultState /* state */, clearSelectedItems() /* action */))
|
||||||
|
.toHaveLength(0);
|
||||||
|
expect(selectedItemsReducer(selectedState /* state */, clearSelectedItems() /* action */))
|
||||||
|
.toHaveLength(0);
|
||||||
|
expect(selectedItemsReducer(unselectedState /* state */, clearSelectedItems() /* action */))
|
||||||
|
.toHaveLength(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('invalidsetSelectedItems', () => {
|
||||||
|
const origState = ['selected1', 'selected2'];
|
||||||
|
|
||||||
|
expect(selectedItemsReducer(origState /* state */, setSelectedItems() /* action */))
|
||||||
|
.toBe(origState);
|
||||||
|
expect(selectedItemsReducer(origState /* state */, setSelectedItems('notAnArray') /* action */))
|
||||||
|
.toBe(origState);
|
||||||
|
});
|
|
@ -1,6 +1,9 @@
|
||||||
/* eslint-env jest */
|
/* eslint-env jest */
|
||||||
import strokeColorReducer from '../../src/reducers/stroke-color';
|
import strokeColorReducer from '../../src/reducers/stroke-color';
|
||||||
import {changeStrokeColor} 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', () => {
|
test('initialState', () => {
|
||||||
let defaultState;
|
let defaultState;
|
||||||
|
@ -26,6 +29,22 @@ test('changeStrokeColor', () => {
|
||||||
.toEqual(newStrokeColor);
|
.toEqual(newStrokeColor);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('changeStrokeColorViaSelectedItems', () => {
|
||||||
|
let defaultState;
|
||||||
|
|
||||||
|
const strokeColor1 = 6;
|
||||||
|
const strokeColor2 = null; // transparent
|
||||||
|
let selectedItems = [mockPaperRootItem({strokeColor: strokeColor1})];
|
||||||
|
expect(strokeColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
|
||||||
|
.toEqual(strokeColor1);
|
||||||
|
selectedItems = [mockPaperRootItem({strokeColor: strokeColor2})];
|
||||||
|
expect(strokeColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
|
||||||
|
.toEqual(strokeColor2);
|
||||||
|
selectedItems = [mockPaperRootItem({strokeColor: strokeColor1}), mockPaperRootItem({strokeColor: strokeColor2})];
|
||||||
|
expect(strokeColorReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
|
||||||
|
.toEqual(MIXED);
|
||||||
|
});
|
||||||
|
|
||||||
test('invalidChangeStrokeColor', () => {
|
test('invalidChangeStrokeColor', () => {
|
||||||
const origState = '#fff';
|
const origState = '#fff';
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
/* eslint-env jest */
|
/* eslint-env jest */
|
||||||
import strokeWidthReducer from '../../src/reducers/stroke-width';
|
import strokeWidthReducer from '../../src/reducers/stroke-width';
|
||||||
import {MAX_STROKE_WIDTH, changeStrokeWidth} from '../../src/reducers/stroke-width';
|
import {MAX_STROKE_WIDTH, changeStrokeWidth} from '../../src/reducers/stroke-width';
|
||||||
|
import {setSelectedItems} from '../../src/reducers/selected-items';
|
||||||
|
import {mockPaperRootItem} from '../__mocks__/paperMocks';
|
||||||
|
|
||||||
test('initialState', () => {
|
test('initialState', () => {
|
||||||
let defaultState;
|
let defaultState;
|
||||||
|
@ -23,6 +25,22 @@ test('changestrokeWidth', () => {
|
||||||
.toEqual(MAX_STROKE_WIDTH);
|
.toEqual(MAX_STROKE_WIDTH);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('changeStrokeWidthViaSelectedItems', () => {
|
||||||
|
let defaultState;
|
||||||
|
|
||||||
|
const strokeWidth1 = 6;
|
||||||
|
let strokeWidth2; // no outline
|
||||||
|
let selectedItems = [mockPaperRootItem({strokeWidth: strokeWidth1})];
|
||||||
|
expect(strokeWidthReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
|
||||||
|
.toEqual(strokeWidth1);
|
||||||
|
selectedItems = [mockPaperRootItem({strokeWidth: strokeWidth2})];
|
||||||
|
expect(strokeWidthReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
|
||||||
|
.toEqual(0); // Convert no outline to stroke width 0
|
||||||
|
selectedItems = [mockPaperRootItem({strokeWidth: strokeWidth1}), mockPaperRootItem({strokeWidth: strokeWidth2})];
|
||||||
|
expect(strokeWidthReducer(defaultState /* state */, setSelectedItems(selectedItems) /* action */))
|
||||||
|
.toEqual(null); // null indicates mixed for stroke width
|
||||||
|
});
|
||||||
|
|
||||||
test('invalidChangestrokeWidth', () => {
|
test('invalidChangestrokeWidth', () => {
|
||||||
const origState = {strokeWidth: 1};
|
const origState = {strokeWidth: 1};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue