@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
99 lines (78 loc) • 3.39 kB
JavaScript
import {loadMathjs} from '../../components/formula/common/util'
import {variablesFromItemBody, formulaSyntaxError, parseFormulaDecimalSeparator} from '../formula'
describe('util', () => {
describe('variablesFromItemBody', () => {
it('extracts the variables in backticks from the item body', () => {
const str = 'find `x` + `y`'
expect(variablesFromItemBody(str)).toEqual(['x', 'y'])
})
it('works on undefined', () => {
expect(variablesFromItemBody(void 0)).toEqual([])
})
it('automatically sorts variables', () => {
const str = 'find `y` + `x`'
expect(variablesFromItemBody(str)).toEqual(['x', 'y'])
})
it('variables starting with a letter, and containing _ or digits', () => {
const str = 'find `a` + `b1` + `c_d` + `d2e`'
expect(variablesFromItemBody(str)).toEqual(['a', 'b1', 'c_d', 'd2e'])
})
it('ignores invalid variables', () => {
const str = 'find `x ` + ` y` + `z$`'
expect(variablesFromItemBody(str)).toEqual([])
})
})
describe('formulaSyntaxError', () => {
beforeAll(loadMathjs)
it('passes for a valid formula', () => {
expect(formulaSyntaxError(['x', 'y'], 'x+y')).toBeNull()
})
it('passes for a valid formula using e() and pi()', () => {
expect(formulaSyntaxError([], 'e()+pi()')).toBeNull()
})
it('passes for a valid formula using e() and pi() when custom variables are defined', () => {
expect(formulaSyntaxError(['e', 'pi'], 'e()+pi()')).toBeNull()
})
it('passes for a valid formula using custom nested functions', () => {
expect(formulaSyntaxError(['a', 'b'], 'rad_to_deg(asin(a/b))')).toBeNull()
})
it('gives the correct error for a missing variable', () => {
expect(formulaSyntaxError(['x'], 'x+y')).toBe('Undefined symbol y')
})
it('gives the correct error for an incomplete expression', () => {
expect(formulaSyntaxError(['x', 'y'], 'x+')).toBe('Unexpected end of expression')
})
it('gives the correct error for an extra paren', () => {
expect(formulaSyntaxError(['x', 'y'], 'x+y)')).toBe('Unexpected operator )')
})
it('gives a generic error message for any other error', () => {
expect(formulaSyntaxError(['x', 'y'], '[x+y')).toBe('Invalid formula')
})
it('throws error if the locale does not use comma as decimal separator', () => {
expect(formulaSyntaxError([], '5,1')).toBe('Unexpected operator ,')
})
describe('locale that uses comma as decimal separator', () => {
beforeEach(() => {
window.document.documentElement.lang = 'pt-BR'
})
afterEach(() => {
window.document.documentElement.lang = 'en-US'
})
it('does not throw errors', () => {
expect(formulaSyntaxError([], '5,1')).not.toBe('Unexpected operator ,')
})
})
})
describe('parseFormulaDecimalSeparator', () => {
describe('decimal comma format', () => {
it('parses the formula if the locale uses comma as decimal separator', () => {
expect(parseFormulaDecimalSeparator('pt-BR', '5,1 + x*2,1')).toBe('5.1 + x*2.1')
})
})
describe('decimal point format', () => {
it('does not parse the formula if dot is used as decimal separator', () => {
expect(parseFormulaDecimalSeparator('ja-JP', '5,1 + x*2,1')).toBe('5,1 + x*2,1')
})
})
})
})