@instructure/quiz-interactions
Version:
A React UI component Library for quiz interaction types.
113 lines (93 loc) • 3.63 kB
JavaScript
import {beforeAll, describe, expect, it} from 'vitest'
import {loadMathjs, mathjsLoadingWrapper, substituteVars} from '../util'
describe('additionalFunctions', () => {
// note: not testing functions that are just renamings of mathjs functions
beforeAll(async () => {
await loadMathjs()
})
it("validates our 'deg_to_rad' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('deg_to_rad(180)')).toBe(Math.PI)
})
it("validates our 'rad_to_deg' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('rad_to_deg(pi)')).toBe(180)
})
it("validates our 'at' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('at([1,2,3], 1)')).toBe(2)
// QUIZ-7531 ensure negative indexes don't break mathjs
expect(isNaN(mathjs.eval('at([1,2,3], -1)'))).toBe(true)
expect(isNaN(mathjs.eval('at([1,2,3], -1) + 1'))).toBe(true)
})
it("validates our 'first' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('first([2,3,4])')).toBe(2)
})
it("validates our 'last' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('last([3,4,5])')).toBe(5)
})
it("validates our 'length' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('length([3,4,5])')).toBe(3)
})
it("validates our 'range' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('range([6,2,8,4])')).toBe(6) // max - min
})
it("validates our 'reverse' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('reverse(7,8,9)')).toEqual([9, 8, 7])
})
it("validates our 'if' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(mathjs.eval('if(1, 5, 7)')).toBe(5)
expect(mathjs.eval('if(0, 5, 7)')).toBe(7)
})
it("validates our 'divide' implementation", () => {
const mathjs = mathjsLoadingWrapper.mathjs
expect(isNaN(mathjs.eval('8/(3/0)'))).toBe(true)
expect(isNaN(mathjs.eval('8/Infinity'))).toBe(true)
expect(isNaN(mathjs.eval('Infinity/8'))).toBe(true)
expect(mathjs.eval('1983/6')).toBe(330.5)
expect(mathjs.eval('21/7')).toBe(3)
})
})
describe('substituteVars', () => {
it('replaces all occurrences of each variable', () => {
const input = '`x` + (`y` / `x`)'
const vars = [
{name: 'x', value: '42'},
{name: 'y', value: '7'},
]
expect(substituteVars(input, vars, 'en')).toBe('42 + (7 / 42)')
})
it('preserves trailing zeros after a decimal', () => {
const input = 'how many sig figs in `x` and \\(`a`\\)?'
const vars = [
{name: 'x', value: '0.10'},
{name: 'a', value: '0.20'},
]
expect(substituteVars(input, vars, 'en')).toBe('how many sig figs in 0.10 and \\(0.20\\)?')
})
describe('i18n', () => {
const locale = 'fr'
it('replaces variables with localized values without grouping or rounding', () => {
const input = '`x` + `y`'
const vars = [
{name: 'x', value: '1111.1111'},
{name: 'y', value: '2222.2222'},
]
expect(substituteVars(input, vars, locale)).toBe('1\u202f111,1111 + 2\u202f222,2222')
})
it('does not localize variables in scientific notation', () => {
const input = '`x` + `y`'
const vars = [
{name: 'x', value: '5.1*10^-2'},
{name: 'y', value: '0.01'},
]
expect(substituteVars(input, vars, locale)).toBe('5.1*10^-2 + 0,01')
})
})
})