diff --git a/packages/lib-classifier/src/plugins/drawingTools/models/tools/Tool/Tool.spec.js b/packages/lib-classifier/src/plugins/drawingTools/models/tools/Tool/Tool.spec.js index 41c2fe446c..c866e36fd1 100644 --- a/packages/lib-classifier/src/plugins/drawingTools/models/tools/Tool/Tool.spec.js +++ b/packages/lib-classifier/src/plugins/drawingTools/models/tools/Tool/Tool.spec.js @@ -94,7 +94,7 @@ describe('Model > DrawingTools > Tool', function () { taskKey: 'multiple', type: 'multiple', answers: ['apples', 'oranges', 'pears'], - required: '', + required: false, strings: { question: 'which fruit?' } @@ -103,7 +103,7 @@ describe('Model > DrawingTools > Tool', function () { taskKey: 'single', type: 'single', answers: ['one', 'two', 'three'], - required: '', + required: false, strings: { question: 'how many?' } @@ -111,7 +111,7 @@ describe('Model > DrawingTools > Tool', function () { { taskKey: 'text', type: 'text', - required: '', + required: false, strings: { instruction: 'Transcribe something' }, diff --git a/packages/lib-classifier/src/plugins/tasks/models/Task.js b/packages/lib-classifier/src/plugins/tasks/models/Task.js index b945deca13..702b9eb83c 100644 --- a/packages/lib-classifier/src/plugins/tasks/models/Task.js +++ b/packages/lib-classifier/src/plugins/tasks/models/Task.js @@ -6,10 +6,23 @@ const Task = types.model('Task', { // override annotation in individual task models with specific annotation types annotation: types.safeReference(Annotation), taskKey: types.identifier, - required: types.maybe(types.union(types.string, types.boolean)), // text task required default = false + required: types.optional(types.boolean, false), strings: types.map(types.string), type: types.literal('default') }) + .preProcessSnapshot(snapshot => { + if (typeof snapshot.required !== 'boolean') { + // 'false' is a true value, so handle that here + const required = (snapshot.required === 'false') + ? false + : Boolean(snapshot.required) + return { + ...snapshot, + required + } + } + return snapshot + }) .views(self => ({ defaultAnnotation(id = cuid()) { diff --git a/packages/lib-classifier/src/plugins/tasks/models/Task.spec.js b/packages/lib-classifier/src/plugins/tasks/models/Task.spec.js index 44fa727c99..d5738bd64b 100644 --- a/packages/lib-classifier/src/plugins/tasks/models/Task.spec.js +++ b/packages/lib-classifier/src/plugins/tasks/models/Task.spec.js @@ -29,6 +29,27 @@ describe('Model > Task', function () { expect(taskInstance.validate).to.be.a('function') }) + describe('task.required', function () { + it('should default to false', function () { + const task = Task.create(mockTask) + expect(task.required).to.be.false() + }) + + it('should accept a boolean value', function () { + const task = Task.create({ ...mockTask, required: true }) + expect(task.required).to.be.true() + }) + + it('should accept a string value', function () { + let task = Task.create({ ...mockTask, required: 'true' }) + expect(task.required).to.be.true() + task = Task.create({ ...mockTask, required: 'false' }) + expect(task.required).to.be.false() + task = Task.create({ ...mockTask, required: '' }) + expect(task.required).to.be.false() + }) + }) + describe('Views > defaultAnnotation', function () { let task diff --git a/packages/lib-classifier/test/factories/tasks/MultipleChoiceTaskFactory.js b/packages/lib-classifier/test/factories/tasks/MultipleChoiceTaskFactory.js index b90836750e..3a7a6dd57e 100644 --- a/packages/lib-classifier/test/factories/tasks/MultipleChoiceTaskFactory.js +++ b/packages/lib-classifier/test/factories/tasks/MultipleChoiceTaskFactory.js @@ -6,7 +6,7 @@ export default new Factory() { label: 'blue' }, { label: 'green' } ]) - .attr('required', '') + .attr('required', false) .attr('strings', { question: 'Check all of the colors that apply' }) diff --git a/packages/lib-classifier/test/factories/tasks/SingleChoiceTaskFactory.js b/packages/lib-classifier/test/factories/tasks/SingleChoiceTaskFactory.js index 88204a289c..7fea266ed0 100644 --- a/packages/lib-classifier/test/factories/tasks/SingleChoiceTaskFactory.js +++ b/packages/lib-classifier/test/factories/tasks/SingleChoiceTaskFactory.js @@ -5,7 +5,7 @@ export default new Factory() { label: 'Yes' }, { label: 'No' } ]) - .attr('required', 'true') + .attr('required', true) .attr('strings', { question: 'Is there a galaxy?' })