/
githubmirror
/
oppia
Обзор
Документация
Войти
/
githubmirror
/
oppia
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
core/templates/domain/objects/objects-domain.constants.spec.ts
140 строк
4 KB
Kartik Suryavanshi
Fix part of #23496: Enforce strict typescript checks for group 17,18,19,23 (#25936)
03 май 2026, 04:05
Не верифицирован
03 май 2026, 04:05
89e9ea1
Код
Авторство
О чём код?
// Copyright 2024 The Oppia Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS-IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. /** * @fileoverview unit tests for the units object domain constants. */ import {all, create} from 'mathjs'; import {getCurrencyUnits} from './number-with-units.model'; import {ObjectsDomainConstants} from './objects-domain.constants'; describe('ObjectsDomainConstants', () => { let unitPrefixes: string[] = []; let currencyUnits: string[] = []; let mathjsUnits: string[] = []; const math = create(all, {}); const isValidUnit = (unit: string): boolean => { const unitHelper = math.Unit as unknown as { isValuelessUnit?: (u: string) => boolean; }; const isValueless = typeof unitHelper.isValuelessUnit === 'function' ? unitHelper.isValuelessUnit(unit) : false; return currencyUnits.includes(unit) || isValueless; }; const isValidPrefix = (prefix: string): boolean => { return unitPrefixes.includes(prefix); }; const getUnitPrefixes = (): string[] => { const unitClass = math.Unit as unknown as { PREFIXES?: Record<string, Record<string, unknown>>; }; const prefixes = unitClass.PREFIXES ?? {}; const prefixSet = new Set<string>(); Object.keys(prefixes).forEach(name => { if (name === 'NONE') { return; } const group = prefixes[name]; if (!group) { return; } Object.keys(group).forEach(prefix => { if (prefix !== '') { prefixSet.add(prefix); } }); }); return Array.from(prefixSet); }; const getAllMathjsUnits = (): string[] => { const unitClass = math.Unit as unknown as { units?: Record<string, unknown>; }; return unitClass.units ? Object.keys(unitClass.units) : []; }; beforeEach(() => { unitPrefixes = getUnitPrefixes(); currencyUnits = getCurrencyUnits(); mathjsUnits = getAllMathjsUnits(); }); it('should check that every value in UNIT_TO_NORMALIZED_UNIT_MAPPING is a valid unit', () => { Object.values( ObjectsDomainConstants.UNIT_TO_NORMALIZED_UNIT_MAPPING ).forEach((unit: string) => { expect(isValidUnit(unit)).toBe(true); }); }); it('should check that every key in UNIT_TO_NORMALIZED_UNIT_MAPPING is a valid unit', () => { Object.keys(ObjectsDomainConstants.UNIT_TO_NORMALIZED_UNIT_MAPPING).forEach( (unit: string) => { expect(isValidUnit(unit)).toBe(true); } ); }); it('should check that all mathjs units are present in UNIT_TO_NORMALIZED_UNIT_MAPPING', () => { const units = Object.keys( ObjectsDomainConstants.UNIT_TO_NORMALIZED_UNIT_MAPPING ).filter((unit: string) => !currencyUnits.includes(unit)); const missingUnits = mathjsUnits.filter( (unit: string) => !units.includes(unit) ); expect(missingUnits).toEqual([]); }); it('should check that every value in PREFIX_TO_NORMALIZED_PREFIX_MAPPING is a valid prefix', () => { Object.values( ObjectsDomainConstants.PREFIX_TO_NORMALIZED_PREFIX_MAPPING ).forEach((prefix: string) => { expect(isValidPrefix(prefix)).toBe(true); }); }); it('should check that every key in PREFIX_TO_NORMALIZED_PREFIX_MAPPING is a valid prefix', () => { Object.keys( ObjectsDomainConstants.PREFIX_TO_NORMALIZED_PREFIX_MAPPING ).forEach((prefix: string) => { expect(isValidPrefix(prefix)).toBe(true); }); }); it('should check that the PREFIX_TO_NORMALIZED_PREFIX_MAPPING contains all prefixes supported by mathjs', () => { const prefixes = Object.keys( ObjectsDomainConstants.PREFIX_TO_NORMALIZED_PREFIX_MAPPING ).sort(); expect(prefixes).toEqual(unitPrefixes.sort()); }); });