Skip to content

Commit

Permalink
feat(annotations): Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Sep 9, 2020
1 parent b786d05 commit 4828358
Showing 1 changed file with 86 additions and 26 deletions.
112 changes: 86 additions & 26 deletions src/lib/__tests__/AnnotationControlsFSM-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AnnotationMode } from '../AnnotationControls';

describe('lib/AnnotationControlsFSM', () => {
describe('AnnotationState.NONE', () => {
// Go to different states
[
{
input: AnnotationInput.CLICK,
Expand All @@ -29,14 +30,15 @@ describe('lib/AnnotationControlsFSM', () => {
output: AnnotationMode.REGION,
},
].forEach(({ input, mode, nextState, output }) => {
it(`should go to state ${nextState} and output ${output}`, () => {
it(`should go to state ${nextState} and output ${output} if input is ${input} and mode is ${mode}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM();

expect(annotationControlsFSM.transition(input, mode)).to.equal(output);
expect(annotationControlsFSM.getState()).to.equal(nextState);
});
});

// Stay in the same state
[AnnotationInput.CANCEL, AnnotationInput.SUCCESS, AnnotationInput.UPDATE].forEach(input => {
it(`should stay in state none if input is ${input}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM();
Expand All @@ -48,25 +50,55 @@ describe('lib/AnnotationControlsFSM', () => {
});

describe('AnnotationState.HIGHLIGHT/REGION', () => {
// Stay in the same state
[AnnotationState.HIGHLIGHT, AnnotationState.REGION].forEach(state => {
// non-Click input
[AnnotationInput.CANCEL, AnnotationInput.CREATE, AnnotationInput.SUCCESS, AnnotationInput.SUCCESS].forEach(
input => {
it(`should stay in state ${state}`, () => {
it(`should stay in state ${state} if input is ${input}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(state);

expect(annotationControlsFSM.transition(input)).to.equal(state);
expect(annotationControlsFSM.getState()).to.equal(state);
});
},
);
});

// Click input
[AnnotationMode.HIGHLIGHT, AnnotationMode.REGION].forEach(mode => {
it(`should go to state none/${mode} if input is click and mode is ${mode}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(state);
// Go to different states
describe('AnnotationState.HIGHLIGHT', () => {
[
{
mode: AnnotationMode.HIGHLIGHT,
output: AnnotationMode.NONE,
},
{
mode: AnnotationMode.REGION,
output: AnnotationMode.REGION,
},
].forEach(({ mode, output }) => {
it(`should output ${output} if input is click and mode is ${mode}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(AnnotationState.HIGHLIGHT);

const output = state === mode ? AnnotationMode.NONE : mode;
expect(annotationControlsFSM.transition(AnnotationInput.CLICK, mode)).to.equal(output);
expect(annotationControlsFSM.getState()).to.equal(output);
});
});
});

// Go to different states
describe('AnnotationState.REGION', () => {
[
{
mode: AnnotationMode.REGION,
output: AnnotationMode.NONE,
},
{
mode: AnnotationMode.HIGHLIGHT,
output: AnnotationMode.HIGHLIGHT,
},
].forEach(({ mode, output }) => {
it(`should output ${output} if input is click and mode is ${mode}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(AnnotationState.REGION);

expect(annotationControlsFSM.transition(AnnotationInput.CLICK, mode)).to.equal(output);
expect(annotationControlsFSM.getState()).to.equal(output);
Expand All @@ -76,6 +108,7 @@ describe('lib/AnnotationControlsFSM', () => {
});

describe('AnnotationState.HIGHLIGHT_TEMP/REGION_TEMP', () => {
// Go to none state
[
{
state: AnnotationState.HIGHLIGHT_TEMP,
Expand All @@ -87,36 +120,21 @@ describe('lib/AnnotationControlsFSM', () => {
},
].forEach(({ state, stateMode }) => {
[
{
input: AnnotationInput.CLICK,
mode: AnnotationMode.HIGHLIGHT,
nextState:
stateMode === AnnotationMode.HIGHLIGHT ? AnnotationState.NONE : AnnotationState.HIGHLIGHT,
output: stateMode === AnnotationMode.HIGHLIGHT ? AnnotationMode.NONE : AnnotationMode.HIGHLIGHT,
},
{
input: AnnotationInput.CLICK,
mode: AnnotationMode.REGION,
nextState: stateMode === AnnotationMode.REGION ? AnnotationState.NONE : AnnotationState.REGION,
output: stateMode === AnnotationMode.REGION ? AnnotationMode.NONE : AnnotationMode.REGION,
},
{
input: AnnotationInput.CANCEL,
mode: AnnotationMode.HIGHLIGHT,
nextState: AnnotationState.NONE,
output: AnnotationMode.NONE,
},
{
input: AnnotationInput.SUCCESS,
mode: undefined,
nextState: AnnotationState.NONE,
output: AnnotationMode.NONE,
},
].forEach(({ input, mode, nextState, output }) => {
it(`should go to state ${nextState} and output ${output}`, () => {
].forEach(({ input, nextState, output }) => {
it(`should go to state ${nextState} and output ${output} if input is ${input}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(state);

expect(annotationControlsFSM.transition(input, mode)).to.equal(output);
expect(annotationControlsFSM.transition(input)).to.equal(output);
expect(annotationControlsFSM.getState()).to.equal(nextState);
});
});
Expand All @@ -130,5 +148,47 @@ describe('lib/AnnotationControlsFSM', () => {
});
});
});

// Go to different states
describe('AnnotationState.HIGHLIGHT_TEMP', () => {
[
{
mode: AnnotationMode.HIGHLIGHT,
output: AnnotationMode.NONE,
},
{
mode: AnnotationMode.REGION,
output: AnnotationMode.REGION,
},
].forEach(({ mode, output }) => {
it(`should output ${output} if input is click and mode is ${mode}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(AnnotationState.HIGHLIGHT_TEMP);

expect(annotationControlsFSM.transition(AnnotationInput.CLICK, mode)).to.equal(output);
expect(annotationControlsFSM.getState()).to.equal(output);
});
});
});

// Go to different states
describe('AnnotationState.REGION_TEMP', () => {
[
{
mode: AnnotationMode.REGION,
output: AnnotationMode.NONE,
},
{
mode: AnnotationMode.HIGHLIGHT,
output: AnnotationMode.HIGHLIGHT,
},
].forEach(({ mode, output }) => {
it(`should output ${output} if input is click and mode is ${mode}`, () => {
const annotationControlsFSM = new AnnotationControlsFSM(AnnotationState.REGION_TEMP);

expect(annotationControlsFSM.transition(AnnotationInput.CLICK, mode)).to.equal(output);
expect(annotationControlsFSM.getState()).to.equal(output);
});
});
});
});
});

0 comments on commit 4828358

Please sign in to comment.