Skip to content

Commit

Permalink
Classifier: validate simple dropdown options (#1861)
Browse files Browse the repository at this point in the history
Add a minimum and maximum options count to the simple dropdown task. Add a MenuOptions type which validates the task options against those counts.
  • Loading branch information
eatyourgreens authored Oct 29, 2020
1 parent ff9276c commit c4f819c
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import SimpleDropdownAnnotation from './SimpleDropdownAnnotation'
// TODO: should we make question/instruction consistent between task types?
// What should be it called? I think we should use 'instruction'

const MIN_OPTIONS = 4
const MAX_OPTIONS = 20

const MenuOptions = types.refinement('MenuOptions',
types.array(types.string),
value => value.length >= MIN_OPTIONS && value.length <= MAX_OPTIONS
)

const SimpleDropdown = types.model('SimpleDropdown', {
allowCreate: types.optional(types.boolean, false),
annotation: types.safeReference(SimpleDropdownAnnotation),
help: types.optional(types.string, ''),
instruction: types.optional(types.string, ''),
options: types.array(types.string),
options: MenuOptions,
type: types.literal('dropdown-simple'),
})
.preProcessSnapshot(snapshot => {
Expand All @@ -31,3 +39,5 @@ const SimpleDropdown = types.model('SimpleDropdown', {
const SimpleDropdownTask = types.compose('SimpleDropdownTask', Task, SimpleDropdown)

export default SimpleDropdownTask
export { MIN_OPTIONS, MAX_OPTIONS }

Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { types } from 'mobx-state-tree'
import SimpleDropdownTask from '@plugins/tasks/SimpleDropdownTask'

const simpleDropdownTask = {
instruction: 'Choose your favourite colour',
allowCreate: false,
options: [
'Red',
'Blue',
'Yellow',
'Green',
'White',
'Black',
],
required: false,
taskKey: 'T1',
type: 'dropdown-simple'
}
import { MIN_OPTIONS, MAX_OPTIONS } from './SimpleDropdownTask'

describe('Model > SimpleDropdownTask', function () {
const { TaskModel, AnnotationModel } = SimpleDropdownTask
const simpleDropdownTask = {
instruction: 'Choose your favourite colour',
allowCreate: false,
options: [
'Red',
'Blue',
'Yellow',
'Green',
'White',
'Black',
],
required: false,
taskKey: 'T1',
type: 'dropdown-simple'
}

it('should exist', function () {
const simpleDropdownTaskInstance = SimpleDropdownTask.TaskModel.create(simpleDropdownTask)
const simpleDropdownTaskInstance = TaskModel.create(simpleDropdownTask)
expect(simpleDropdownTaskInstance).to.be.ok()
expect(simpleDropdownTaskInstance).to.be.an('object')
})
Expand All @@ -38,11 +40,11 @@ describe('Model > SimpleDropdownTask', function () {
let task

before(function () {
task = SimpleDropdownTask.TaskModel.create(simpleDropdownTask)
task = TaskModel.create(simpleDropdownTask)
const annotation = task.defaultAnnotation
const store = types.model('MockStore', {
annotation: SimpleDropdownTask.AnnotationModel,
task: SimpleDropdownTask.TaskModel
annotation: AnnotationModel,
task: TaskModel
})
.create({
annotation,
Expand Down Expand Up @@ -70,11 +72,11 @@ describe('Model > SimpleDropdownTask', function () {

before(function () {
const requiredTask = Object.assign({}, simpleDropdownTask, { required: true })
task = SimpleDropdownTask.TaskModel.create(requiredTask)
task = TaskModel.create(requiredTask)
const annotation = task.defaultAnnotation
const store = types.model('MockStore', {
annotation: SimpleDropdownTask.AnnotationModel,
task: SimpleDropdownTask.TaskModel
annotation: AnnotationModel,
task: TaskModel
})
.create({
annotation,
Expand All @@ -99,4 +101,62 @@ describe('Model > SimpleDropdownTask', function () {
})
})
})

describe('with too few options', function () {
let taskSnapshot

before(function () {
const options = []
for (let i = 0; i < MIN_OPTIONS - 1 ; i++) {
options.push(i.toString())
}
taskSnapshot = {
instruction: 'Choose your favourite number',
allowCreate: false,
options,
required: false,
taskKey: 'T1',
type: 'dropdown-simple'
}
})

it('should throw an error', function () {
let errorThrown = false
try {
TaskModel.create(taskSnapshot)
} catch (e) {
errorThrown = true
}
expect(errorThrown).to.be.true()
})
})

describe('with too many options', function () {
let taskSnapshot

before(function () {
const options = []
for (let i = 0; i < MAX_OPTIONS + 1; i++) {
options.push(i.toString())
}
taskSnapshot = {
instruction: 'Choose your favourite number',
allowCreate: false,
options,
required: false,
taskKey: 'T1',
type: 'dropdown-simple'
}
})

it('should throw an error', function () {
let errorThrown = false
try {
TaskModel.create(taskSnapshot)
} catch (e) {
errorThrown = true
}
expect(errorThrown).to.be.true()
})
})
})

0 comments on commit c4f819c

Please sign in to comment.