UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

269 lines (240 loc) • 6.05 kB
import NumericInteractionType from '../numeric' import checkUserResponseTransform from '../../__tests__/sharedRecordTests' describe('NumericInteractionType', () => { const IntTypeRecord = NumericInteractionType const intType = new IntTypeRecord() describe('#getDefaultInteractionData', () => { it('returns correct data when slug is "numeric" ', () => { const intData = intType.getDefaultInteractionData() expect(intData).toEqual({}) }) }) describe('#getDefaultScoringData', () => { it('returns scoringData when slug is "numeric" ', () => { const scoringData = intType.getDefaultScoringData() expect(scoringData.value).toEqual([ { type: 'exactResponse', value: '', id: '1', }, ]) }) }) describe('validations', () => { const runTest = (scoringDataValue, errors) => { const data = { scoringData: { value: scoringDataValue, }, } expect(intType.getErrors(data)).toMatchObject(errors) } it('validates exactResponse', () => { const scoringDataValue = [ { type: 'exactResponse', value: '1', id: 'abc', }, { type: 'exactResponse', value: '', id: 'abc', }, ] const errors = { scoringData: { value: { 1: { value: ['Answer must be a number'], }, }, }, } runTest(scoringDataValue, errors) }) it('validates marginOfError', () => { const scoringDataValue = [ { type: 'marginOfError', value: '1', margin: '2', id: 'abc', }, { type: 'marginOfError', value: '', margin: '', id: 'abc', }, ] const errors = { scoringData: { value: { 1: { value: ['Answer must be a number'], margin: ['Margin must be a number'], }, }, }, } runTest(scoringDataValue, errors) }) it('validates withinARange', () => { const scoringDataValue = [ { type: 'withinARange', start: '1', end: '2', id: 'abc', }, { type: 'withinARange', start: '', end: '', id: 'abc', }, ] const errors = { scoringData: { value: { 1: { start: ['Range start must be a number'], end: ['Range end must be a number'], }, }, }, } runTest(scoringDataValue, errors) }) it('validates withinARange when the end is smaller than the start', () => { const scoringDataValue = [ { type: 'withinARange', start: '8', end: '9', id: 'abc', }, { type: 'withinARange', start: '10', end: '9', id: 'abc', }, ] const errors = { scoringData: { value: { 1: { end: ['Range end must be greater than range start'], }, }, }, } runTest(scoringDataValue, errors) }) it('validates preciseResponse', () => { const scoringDataValue = [ { type: 'preciseResponse', value: '1.1', precision: '1', id: 'abc', precisionType: 'decimals', }, { type: 'preciseResponse', value: '', precision: '', id: 'abc', precisionType: 'decimals', }, ] const errors = { scoringData: { value: { 1: { value: ['Answer must be a number'], precision: ['Precision must be a number'], }, }, }, } runTest(scoringDataValue, errors) }) it('validates preciseResponse when decimals do not match number of precision', () => { const scoringDataValue = [ { type: 'preciseResponse', value: '1.1', precision: '1', id: 'abc', precisionType: 'decimals', }, { type: 'preciseResponse', value: '1', precision: '1', id: 'abc', precisionType: 'decimals', }, ] const errors = { scoringData: { value: { 1: { precision: ['Must have at least the same number of decimals as precision'], }, }, }, } runTest(scoringDataValue, errors) }) it('validates preciseResponse when sigfigs do not match number of precision', () => { const scoringDataValue = [ { type: 'preciseResponse', value: '1.1', precision: '1', id: 'abc', precisionType: 'significantDigits', }, { type: 'preciseResponse', value: '1', precision: '2', id: 'abc', precisionType: 'significantDigits', }, ] const errors = { scoringData: { value: { 1: { precision: ['Must have at least the same number of significant figures as precision'], }, }, }, } runTest(scoringDataValue, errors) }) }) describe('#hasResponse', () => { it('returns true when there is a response', () => { const resp = '18' expect(intType.hasResponse(resp)).toBe(true) }) it('returns false when there is no response', () => { const resp = null expect(intType.hasResponse(resp)).toBe(false) }) }) const response = {interactionData: {}, response: '12', expected: '12'} checkUserResponseTransform( new NumericInteractionType(), response, response, out => out.props.children, ) })