UNPKG

@instructure/quiz-interactions

Version:

A React UI component Library for quiz interaction types.

153 lines (130 loc) 4.96 kB
import {expect} from '@instructure/ui-test-utils' import FormulaInteractionType from '../formula' import checkUserResponseTransform from '../../__tests__/sharedRecordTests' import {loadMathjs} from '../../../components/formula/common/util' describe('FormulaInteractionType', () => { const IntTypeRecord = FormulaInteractionType const intType = new IntTypeRecord() describe('validation', () => { beforeAll(loadMathjs) const getErrors = ({itemBody = '`x` + `y`', formula = 'x + y', numeric = null} = {}) => { return new FormulaInteractionType({ itemBody, scoringData: { value: { variables: [ { name: 'x', min: '0', max: '5', precision: 0, }, { name: 'y', min: '10', max: '5', precision: 9, }, ], formula, numeric, }, }, }).getErrors() } it('ensures the itemBody is present', () => { const errors = getErrors({itemBody: ''}) expect(errors.itemBody[0]).to.eq('Question Stem cannot be blank') }) it('ensures the itemBody has variables', () => { const errors = getErrors({itemBody: 'x + y'}) expect(errors.itemBody[0]).to.eq('Question Stem must contain variables') }) it('passes if the itemBody is valid', () => { const errors = getErrors() expect(errors.itemBody).to.be.undefined() }) it('ensures the formula is present', () => { const errors = getErrors({formula: ''}) expect(errors.scoringData.value.formula[0]).to.eq('Formula cannot be blank') }) it('ensures the formula has no syntax errors', () => { const errors = getErrors({formula: 'x+y+z'}) expect(errors.scoringData.value.formula[0]).to.eq('Undefined symbol z') }) it('passes if the formula is valid', () => { const errors = getErrors() expect(errors.scoringData.value.formula).to.be.undefined() }) it('ensures the variable max is not less than variable min', () => { const errors = getErrors() expect(errors.scoringData.value.variables['1']).to.deep.include({ max: ['Must be larger than minimum value'], }) }) it('allows not having margin of error', () => { const errors = getErrors() expect(errors.scoringData.value.numeric).to.be.undefined() }) it('does not allow margin of error to be null', () => { const errors = getErrors({numeric: {type: 'marginOfError', margin: null}}) expect(errors.scoringData.value.numeric.margin[0]).to.eq('Margin must be a number') }) it('allows margin of error to be present and positive', () => { const errors = getErrors({numeric: {type: 'marginOfError', margin: '1000'}}) expect(errors.scoringData.value.numeric).to.be.undefined() }) it('allows margin of error to be present and zero', () => { const errors = getErrors({numeric: {type: 'marginOfError', margin: '0'}}) expect(errors.scoringData.value.numeric).to.be.undefined() }) it('does not allow margin of error to be negative', () => { const errors = getErrors({numeric: {type: 'marginOfError', margin: '-10'}}) expect(errors.scoringData.value.numeric.margin[0]).to.eq('must be greater or equal to 0') }) it('does not allow margin of error to be non-numeric', () => { const errors = getErrors({numeric: {type: 'marginOfError', margin: 'not a number'}}) expect(errors.scoringData.value.numeric.margin[0]).to.eq('Margin must be a number') }) }) describe('#getDefaultInteractionData', () => { it('returns correct data when slug is "formula" ', () => { const intData = intType.getDefaultInteractionData() expect(intData).to.eql({}) }) }) describe('#getDefaultScoringData', () => { it('returns scoringData when slug is "formula" ', () => { const scoringData = intType.getDefaultScoringData() expect(scoringData.value).to.eql({ answerCount: '200', formula: '', generatedSolutions: [], variables: [], // TODO: set this from the UI, rather than being stuck with the default numeric: { type: 'marginOfError', margin: '0', marginType: 'absolute', }, }) }) }) describe('#hasResponse', () => { it('returns true when there is a response', () => { const resp = 18 expect(intType.hasResponse(resp)).to.be.true() }) it('returns false when there is no response', () => { const resp = null expect(intType.hasResponse(resp)).to.be.false() }) }) const response = {interactionData: {}, response: '12', expected: '12'} checkUserResponseTransform( new FormulaInteractionType(), response, response, out => out.props.children, ) })