Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for input grouping #22421

Merged
merged 1 commit into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 14 additions & 10 deletions apps/src/block_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -570,15 +570,7 @@ const STANDARD_INPUT_TYPES = {
},
};

/**
* Adds the specified inputs to the block
* @param {Blockly} blockly The Blockly object provided to install()
* @param {Block} block The block to add the inputs to
* @param {LabeledInputConfig[]} inputs The list of inputs to interpolate.
* @param {Object.<string, InputType>} inputTypes A map of input type names to their definitions,
* @param {boolean} inline Whether inputs are being rendered inline
*/
const interpolateInputs = function (blockly, block, inputs, inputTypes=STANDARD_INPUT_TYPES, inline) {
const groupInputsByRow = function (inputs, inputTypes=STANDARD_INPUT_TYPES) {
const inputRows = [];
let lastGroup = [];
inputRows.push(lastGroup);
Expand All @@ -595,8 +587,20 @@ const interpolateInputs = function (blockly, block, inputs, inputTypes=STANDARD_
} else {
inputRows.pop();
}
return inputRows;
};
exports.groupInputsByRow = groupInputsByRow;

inputRows.forEach(inputRow => {
/**
* Adds the specified inputs to the block
* @param {Blockly} blockly The Blockly object provided to install()
* @param {Block} block The block to add the inputs to
* @param {LabeledInputConfig[]} inputs The list of inputs to interpolate.
* @param {Object.<string, InputType>} inputTypes A map of input type names to their definitions,
* @param {boolean} inline Whether inputs are being rendered inline
*/
const interpolateInputs = function (blockly, block, inputs, inputTypes=STANDARD_INPUT_TYPES, inline) {
groupInputsByRow(inputs, inputTypes).forEach(inputRow => {
// Create the last input in the row first
const lastInputConfig = inputRow[inputRow.length - 1];
const lastInput = inputTypes[lastInputConfig.mode]
Expand Down
55 changes: 55 additions & 0 deletions apps/test/unit/blockUtilsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
cleanBlocks,
determineInputs,
interpolateInputs,
groupInputsByRow,
} from '@cdo/apps/block_utils';
import { parseElement, serialize } from '@cdo/apps/xml.js';
import { expect } from '../util/configuredChai';
Expand Down Expand Up @@ -154,6 +155,60 @@ describe('block utils', () => {
expect(appendDummyInput).to.have.been.calledOnce;
expect(appendTitle).to.have.been.calledWith('suffix');
});

describe('groupInputsByRow', () => {
const valueInput1 = {
mode: 'value',
name: 'VALUE1',
label: 'hello',
};
const valueInput2 = {
mode: 'value',
name: 'VALUE2',
label: 'world',
};
const fieldInput = {
mode: 'dropdown',
name: 'DROPDOWN',
label: 'foo',
};
const dummyInput = {
mode: 'dummy',
};
it('groups a single value input as one row', () => {
const groupedInputs = groupInputsByRow([valueInput1]);
expect(groupedInputs).to.deep.equal([
[valueInput1],
]);
});
it('groups two value inputs as two rows', () => {
const groupedInputs = groupInputsByRow([valueInput1, valueInput2]);
expect(groupedInputs).to.deep.equal([
[valueInput1],
[valueInput2],
]);
});
it('groups a field and value input as one row', () => {
const groupedInputs = groupInputsByRow([fieldInput, valueInput1]);
expect(groupedInputs).to.deep.equal([
[fieldInput, valueInput1],
]);
});
it('groups a value and field input as two rows with a dummy input on the second', () => {
const groupedInputs = groupInputsByRow([valueInput1, fieldInput]);
expect(groupedInputs).to.deep.equal([
[valueInput1],
[fieldInput, dummyInput],
]);
});
it('groups a field and two value inputs as two rows', () => {
const groupedInputs = groupInputsByRow([fieldInput, valueInput1, valueInput2]);
expect(groupedInputs).to.deep.equal([
[fieldInput, valueInput1],
[valueInput2],
]);
});
});
});

describe('determineInputs', () => {
Expand Down