@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
120 lines (103 loc) • 3.92 kB
JavaScript
import MatchingInteractionType from '../matching'
import checkUserResponseTransform from '../../__tests__/sharedRecordTests'
describe('MatchingInteractionType', () => {
const IntTypeRecord = MatchingInteractionType
const intType = new IntTypeRecord()
it('has slug matching', () => {
expect(intType.slug).toBe('matching')
})
describe('#getDefaultInteractionData', () => {
it('returns 4 questions with empty itemBody fields', () => {
const questions = intType.getDefaultInteractionData().questions
const bodies = questions.map(q => q.itemBody)
expect(bodies).toEqual(['', '', '', ''])
})
it('returns 4 empty answers', () => {
const answers = intType.getDefaultInteractionData().answers
expect(answers).toEqual(['', '', '', ''])
})
})
describe('#getDefaultUserResponse', () => {
it('returns an object with an empty value object', () => {
const defaultUserResponse = intType.getDefaultUserResponse().value
expect(defaultUserResponse).toEqual({})
})
})
describe('#getDefaultScoringData', () => {
const fakeData = {
questions: [
{id: '1', itemBody: ''},
{id: '2', itemBody: ''},
{id: '3', itemBody: ''},
],
answers: ['', '', ''],
}
it('returns 4 valid matches when slug is matching', () => {
const scoringData = intType.getDefaultScoringData(fakeData)
const expected = {
value: {1: '', 2: '', 3: ''},
editData: {
matches: [
{questionId: '1', questionBody: '', answerBody: ''},
{questionId: '2', questionBody: '', answerBody: ''},
{questionId: '3', questionBody: '', answerBody: ''},
],
distractors: [],
},
}
expect(scoringData).toEqual(expected)
})
})
describe('#validations', () => {
const IntTypeRecord = MatchingInteractionType
const validations = IntTypeRecord.validations({
scoringData: {
editData: {
matches: [
{questionId: '1', questionBody: 'present1', answerBody: 'present'},
{questionId: '2', questionBody: 'present2', answerBody: 'present'},
{questionId: '3', questionBody: 'present3', answerBody: ''},
],
distractors: [],
},
},
})
it('outputs a hash that gives matches and distractors validation rules', () => {
expect(validations.scoringData.editData.matches.length).toBe(2)
expect(validations.scoringData.editData.distractors.length).toBe(3)
})
})
describe('#hasResponse', () => {
it('returns true when all questions have a response', () => {
const resp = {1: 'a', 2: 'b', 3: 'c'}
const intData = {questions: [1, 2, 3]}
expect(intType.hasResponse(resp, intData)).toBe(true)
})
it('returns false when there is no response', () => {
const resp = {}
const intData = {questions: [1, 2, 3]}
expect(intType.hasResponse(resp, intData)).toBe(false)
})
it('returns false when only partially answered', () => {
const resp = {1: 'a', 2: 'b'}
const intData = {questions: [1, 2, 3]}
expect(intType.hasResponse(resp, intData)).toBe(false)
})
})
const interactionData = {
questions: [
{id: 'quuid1', itemBody: '1st President'},
{id: 'quuid2', itemBody: '16th President'},
{id: 'quuid3', itemBody: '44th President'},
],
answers: ['George Washington', 'Thomas Jefferson', 'Abraham Lincoln', 'Barack Obama'],
}
const validResponse = {quuid1: 'George Washington'}
const invalidResponse = {quuid112: 'George Washington'}
checkUserResponseTransform(
new MatchingInteractionType(),
{response: validResponse, interactionData, expected: '1st President - George Washington'},
{response: invalidResponse, interactionData, expected: ' - George Washington'},
out => out.props.children,
)
})