LabelTemplatePickerKeys.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { describe, it, expect } from 'vitest';
  2. import { readFileSync } from 'node:fs';
  3. import { resolve } from 'node:path';
  4. import en from '../../i18n/locales/en';
  5. // The dialog's tests render without i18next, so a key in the wrong place in
  6. // the locale files only shows as the English fallback in every language and no
  7. // test sees it (#3296 review). Every key the label picker names literally has
  8. // to resolve in en.ts; the parity check carries it to the other locales.
  9. const SOURCES = [
  10. 'components/LabelTemplatePickerModal.tsx',
  11. 'components/LabelSheetLayoutEditor.tsx',
  12. 'utils/labelSheetLayout.ts',
  13. ];
  14. function resolveKey(path: string): unknown {
  15. return path.split('.').reduce<unknown>((node, part) => (node as Record<string, unknown> | undefined)?.[part], en);
  16. }
  17. describe('label picker translation keys', () => {
  18. const keys = new Set<string>();
  19. for (const file of SOURCES) {
  20. const source = readFileSync(resolve(__dirname, '../..', file), 'utf8');
  21. for (const match of source.matchAll(/\bt\(\s*'(inventory\.labels\.[\w.]+)'/g)) keys.add(match[1]);
  22. }
  23. // The layout problems are looked up as inventory.labels.custom.problems.<key>.
  24. const utils = readFileSync(resolve(__dirname, '../../utils/labelSheetLayout.ts'), 'utf8');
  25. for (const match of utils.matchAll(/key: '(\w+)'/g)) keys.add(`inventory.labels.custom.problems.${match[1]}`);
  26. it('finds the keys it checks', () => {
  27. expect(keys).toContain('inventory.labels.custom.nameTaken');
  28. expect(keys).toContain('inventory.labels.custom.problems.tooSmall');
  29. });
  30. it.each([...keys])('%s is a string in en.ts', (key) => {
  31. expect(typeof resolveKey(key)).toBe('string');
  32. });
  33. });