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

refactor(classifier): make task.required a boolean #6094

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe('Model > DrawingTools > Tool', function () {
taskKey: 'multiple',
type: 'multiple',
answers: ['apples', 'oranges', 'pears'],
required: '',
required: false,
strings: {
question: 'which fruit?'
}
Expand All @@ -103,15 +103,15 @@ describe('Model > DrawingTools > Tool', function () {
taskKey: 'single',
type: 'single',
answers: ['one', 'two', 'three'],
required: '',
required: false,
strings: {
question: 'how many?'
}
},
{
taskKey: 'text',
type: 'text',
required: '',
required: false,
strings: {
instruction: 'Transcribe something'
},
Expand Down
15 changes: 14 additions & 1 deletion packages/lib-classifier/src/plugins/tasks/models/Task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
21 changes: 21 additions & 0 deletions packages/lib-classifier/src/plugins/tasks/models/Task.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?'
})
Expand Down