|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +/** |
| 4 | + * @file locale-exports.test.mjs |
| 5 | + * @input The package's exports map, read through Node's own package |
| 6 | + * resolution from a workspace app that depends on the package |
| 7 | + * @output Verifies the public locale surface: every whole catalog under |
| 8 | + * `@astryxdesign/core/locales/<locale>.generated.js` resolves, and the |
| 9 | + * per-namespace English slices under `locales/en/` do not — they are the |
| 10 | + * package's own, rendered through the internal useComponentTranslator |
| 11 | + * @position Regression test for the `./locales/en/*` null export |
| 12 | + */ |
| 13 | + |
| 14 | +import {createRequire} from 'node:module'; |
| 15 | +import path from 'node:path'; |
| 16 | +import {fileURLToPath} from 'node:url'; |
| 17 | +import {describe, expect, it} from 'vitest'; |
| 18 | + |
| 19 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 20 | +const REPO_ROOT = path.resolve(__dirname, '../../../..'); |
| 21 | + |
| 22 | +/** |
| 23 | + * Resolve a specifier the way a consumer's bundler or Node would: through |
| 24 | + * the package's exports map, from a workspace app that depends on the |
| 25 | + * package. Only the map is consulted here; a pattern whose target exists |
| 26 | + * resolves to that path, and a `null` target or no match throws |
| 27 | + * ERR_PACKAGE_PATH_NOT_EXPORTED before any file is read. |
| 28 | + */ |
| 29 | +const consumerRequire = createRequire( |
| 30 | + path.join(REPO_ROOT, 'apps/sandbox/package.json'), |
| 31 | +); |
| 32 | + |
| 33 | +function resolveFromConsumer(specifier) { |
| 34 | + try { |
| 35 | + return {ok: consumerRequire.resolve(specifier)}; |
| 36 | + } catch (error) { |
| 37 | + return {code: error.code}; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +describe('the public locale surface of @astryxdesign/core', () => { |
| 42 | + it('resolves a whole catalog by locale', () => { |
| 43 | + expect( |
| 44 | + resolveFromConsumer('@astryxdesign/core/locales/en.generated.js').ok, |
| 45 | + ).toMatch(/\/dist\/i18n\/generated-locales\/en\.generated\.js$/); |
| 46 | + expect( |
| 47 | + resolveFromConsumer('@astryxdesign/core/locales/fr-FR.generated.js').ok, |
| 48 | + ).toMatch(/\/dist\/i18n\/generated-locales\/fr-FR\.generated\.js$/); |
| 49 | + }); |
| 50 | + |
| 51 | + it('does not resolve a per-namespace English slice', () => { |
| 52 | + for (const namespace of ['button', 'pagination']) { |
| 53 | + expect( |
| 54 | + resolveFromConsumer( |
| 55 | + `@astryxdesign/core/locales/en/${namespace}.generated.js`, |
| 56 | + ), |
| 57 | + ).toEqual({code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'}); |
| 58 | + } |
| 59 | + }); |
| 60 | +}); |
0 commit comments