-
Notifications
You must be signed in to change notification settings - Fork 0
Expand test coverage using node:test #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { test, beforeEach } from 'node:test'; | ||
| import { strict as assert } from 'node:assert'; | ||
| import { _js, setTranslations } from '../src/translations.js'; | ||
|
|
||
| beforeEach(() => { | ||
| // Reset the translations state before each test | ||
| setTranslations(undefined); | ||
| }); | ||
|
|
||
| test('_js: falls back to returning the input string when no translations are set', (t) => { | ||
| const errors = []; | ||
| t.mock.method(console, 'error', (msg) => errors.push(msg)); | ||
| assert.equal(_js('Hello'), 'Hello'); | ||
| assert.equal(errors.length, 1); | ||
| assert.match(errors[0], /UI Translations not found/); | ||
| }); | ||
|
|
||
| test('_js: only logs the missing-translations warning once', (t) => { | ||
| const errors = []; | ||
| t.mock.method(console, 'error', (msg) => errors.push(msg)); | ||
| _js('first'); | ||
| _js('second'); | ||
| assert.equal(errors.length, 1); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,84 @@ | ||
| import { _js, setTranslations } from "../dist/translations.js"; | ||
| import { strict as assert } from 'assert'; | ||
| import { test, beforeEach, afterEach } from 'node:test'; | ||
| import { strict as assert } from 'node:assert'; | ||
| import { _js, ___p, setTranslations } from '../src/translations.js'; | ||
|
|
||
| const translatedHi = "Translated Hi (%1)"; | ||
| beforeEach(() => { | ||
| setTranslations({}); | ||
| delete globalThis.window; | ||
| }); | ||
|
|
||
| setTranslations({Hi: translatedHi}); | ||
| assert.equal("Translated Hi (param)", _js("Hi", "param")); | ||
| afterEach(() => { | ||
| delete globalThis.window; | ||
| }); | ||
|
|
||
| test('_js: returns the input string when no translation matches', () => { | ||
| assert.equal(_js('Hello'), 'Hello'); | ||
| }); | ||
|
|
||
| test('_js: returns the translated string when a match is found', () => { | ||
| setTranslations({ Hello: 'Hola' }); | ||
| assert.equal(_js('Hello'), 'Hola'); | ||
| }); | ||
|
|
||
| test('_js: collapses runs of whitespace before lookup', () => { | ||
| setTranslations({ 'Foo bar baz': 'translated' }); | ||
| assert.equal(_js('Foo bar\n\tbaz'), 'translated'); | ||
| }); | ||
|
|
||
| test('_js: returns the compacted string when no match found', () => { | ||
| assert.equal(_js('Foo bar'), 'Foo bar'); | ||
| }); | ||
|
|
||
| test('_js: substitutes a single %1 placeholder', () => { | ||
| setTranslations({ Hi: 'Translated Hi (%1)' }); | ||
| assert.equal(_js('Hi', 'param'), 'Translated Hi (param)'); | ||
| }); | ||
|
|
||
| test('_js: substitutes multiple positional placeholders', () => { | ||
| assert.equal(_js('a %1 b %2', 'x', 'y'), 'a x b y'); | ||
| }); | ||
|
|
||
| test('_js: replaces every occurrence of a placeholder', () => { | ||
| assert.equal(_js('%1 and %1', 'foo'), 'foo and foo'); | ||
| }); | ||
|
|
||
| test('_js: coerces non-string input via String()', () => { | ||
| assert.equal(_js(42), '42'); | ||
| }); | ||
|
|
||
| test('_js: returns "Translated" when window.App.showTranslatedPlaceholder is set', () => { | ||
| globalThis.window = { App: { showTranslatedPlaceholder: true } }; | ||
| setTranslations({ x: 'translated x' }); | ||
| assert.equal(_js('x'), 'Translated'); | ||
| }); | ||
|
|
||
| test('_js: ignores window.App when the placeholder flag is falsy', () => { | ||
| globalThis.window = { App: { showTranslatedPlaceholder: false } }; | ||
| setTranslations({ x: 'translated x' }); | ||
| assert.equal(_js('x'), 'translated x'); | ||
| }); | ||
|
|
||
| test('___p: uses the singular form when number === 1', () => { | ||
| assert.equal(___p(1, '%1 step', '%1 steps', 1), '1 step'); | ||
| }); | ||
|
|
||
| test('___p: uses the plural form when number === 0', () => { | ||
| assert.equal(___p(0, '%1 step', '%1 steps', 0), '0 steps'); | ||
| }); | ||
|
|
||
| test('___p: uses the plural form when number > 1', () => { | ||
| assert.equal(___p(5, '%1 step', '%1 steps', 5), '5 steps'); | ||
| }); | ||
|
|
||
| test('___p: forwards args to _js for translation lookup', () => { | ||
| setTranslations({ '%1 step': 'TS:%1', '%1 steps': 'TP:%1' }); | ||
| assert.equal(___p(1, '%1 step', '%1 steps', 7), 'TS:7'); | ||
| assert.equal(___p(2, '%1 step', '%1 steps', 7), 'TP:7'); | ||
| }); | ||
|
|
||
| test('setTranslations: replaces the translation table on each call', () => { | ||
| setTranslations({ a: 'first' }); | ||
| assert.equal(_js('a'), 'first'); | ||
| setTranslations({ a: 'second' }); | ||
| assert.equal(_js('a'), 'second'); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.