Skip to content

Commit 30f0339

Browse files
committed
Tests for ProgramBlockEditor change loop iterations
1 parent b708c3b commit 30f0339

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

src/ProgramBlockEditor.test.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,11 @@ function getProgramBlockLoopLabel(programBlockEditorWrapper, index: number) {
145145
.find('.command-block-loop-label-container').getDOMNode().textContent;
146146
}
147147

148+
function getProgramBlockLoopIterationsInput(programBlockEditorWrapper, index: number) {
149+
return getProgramBlocks(programBlockEditorWrapper).at(index)
150+
.find('.command-block-loop-iterations');
151+
}
152+
148153
function getProgramBlockLoopIterations(programBlockEditorWrapper, index: number) {
149154
return ((getProgramBlocks(programBlockEditorWrapper).at(index)
150155
.find('.command-block-loop-iterations')
@@ -473,6 +478,87 @@ describe('Active loop container highlight', () => {
473478
});
474479
});
475480

481+
describe('Change loop iterations', () => {
482+
test('The ProgramSequence should be updated when the loop iterations are changed when the program is stopped', () => {
483+
const { wrapper, mockChangeProgramSequenceHandler } = createMountProgramBlockEditor({
484+
runningState: 'stopped',
485+
programSequence: new ProgramSequence(
486+
[
487+
{block: 'startLoop', label: 'A', iterations: 2},
488+
{block: 'endLoop', label: 'A'}
489+
],
490+
0,
491+
0,
492+
new Map([['A', 2]])
493+
)
494+
});
495+
496+
const input = getProgramBlockLoopIterationsInput(wrapper, 0);
497+
((input.getDOMNode(): any): HTMLInputElement).value = '4';
498+
input.simulate('change');
499+
input.getDOMNode().dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter'}));
500+
501+
expect(mockChangeProgramSequenceHandler.mock.calls.length).toBe(1);
502+
expect(mockChangeProgramSequenceHandler.mock.calls[0][0]).toStrictEqual(
503+
new ProgramSequence(
504+
[
505+
{
506+
block: 'startLoop',
507+
label: 'A',
508+
iterations: 4
509+
},
510+
{
511+
block: 'endLoop',
512+
label: 'A'
513+
}
514+
],
515+
0,
516+
0,
517+
new Map([['A', 2]])
518+
)
519+
);
520+
});
521+
test('The ProgramSequence and loopIterationsLeft should be updated when the loop iterations are changed when the program is paused', () => {
522+
const { wrapper, mockChangeProgramSequenceHandler } = createMountProgramBlockEditor({
523+
runningState: 'paused',
524+
programSequence: new ProgramSequence(
525+
[
526+
{block: 'startLoop', label: 'A', iterations: 2},
527+
{block: 'endLoop', label: 'A'}
528+
],
529+
0,
530+
0,
531+
new Map([['A', 1]])
532+
)
533+
});
534+
535+
const input = getProgramBlockLoopIterationsInput(wrapper, 0);
536+
((input.getDOMNode(): any): HTMLInputElement).value = '4';
537+
input.simulate('change');
538+
input.getDOMNode().dispatchEvent(new KeyboardEvent('keydown', {key: 'Enter'}));
539+
540+
expect(mockChangeProgramSequenceHandler.mock.calls.length).toBe(1);
541+
expect(mockChangeProgramSequenceHandler.mock.calls[0][0]).toStrictEqual(
542+
new ProgramSequence(
543+
[
544+
{
545+
block: 'startLoop',
546+
label: 'A',
547+
iterations: 4
548+
},
549+
{
550+
block: 'endLoop',
551+
label: 'A'
552+
}
553+
],
554+
0,
555+
0,
556+
new Map([['A', 4]])
557+
)
558+
);
559+
});
560+
});
561+
476562
describe('The expand add node toggle switch should be configurable via properties', () => {
477563
describe('Given that addNodeExpandedMode is false', () => {
478564
test('Then the toggle switch should be off, and the change handler should be wired up', () => {

0 commit comments

Comments
 (0)