-
-
Notifications
You must be signed in to change notification settings - Fork 270
test: add initial server-side e2e test for i18n layer #480
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
Open
Yabi23
wants to merge
11
commits into
coderaiser:master
Choose a base branch
from
Yabi23:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−0
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2227041
test: add initial server-side e2e test for i18n layer
Yabi23 4ef963b
test: move i18n e2e test to client directory
Yabi23 757ceaf
test: fix createServer import path after moving test to client directory
Yabi23 824a705
test: ensure explicit dynamic port allocation and wait condition for …
Yabi23 a1a3818
test: remove custom server import to align with playwright base url c…
Yabi23 452b265
test: remove explanatory comments from i18n e2e spec for code cleanli…
Yabi23 7fcec9d
feat: implement stateless request-local i18n factory and expand e2e U…
Yabi23 0d959ed
feat: implement secure server-side i18n injection layer inside route …
Yabi23 d2fb519
feat: implement robust multi-level path resolution pipeline for i18n …
Yabi23 0f72bcc
feat: implement secure server-side i18n injection layer inside route …
Yabi23 9954631
fix: remove localized typo from comments to satisfy linter rules
Yabi23 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| export const createTranslator = (dictionary = {}) => (key) => { | ||
| return dictionary[key] ?? key; | ||
| }; | ||
|
|
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,20 @@ | ||
| import {readFileSync as nativeReadFileSync} from 'node:fs'; | ||
| import {join} from 'node:path'; | ||
| import tryCatch from 'try-catch'; | ||
|
|
||
| export const readTranslations = (lang, baseDir, {readFileSync = nativeReadFileSync} = {}) => { | ||
| const paths = [ | ||
| join(baseDir, '..', 'json', 'i18n', `${lang}.json`), | ||
| join(baseDir, '..', 'json', 'i18n', 'en.json'), | ||
| ]; | ||
|
|
||
| for (const dictPath of paths) { | ||
| const [error, jsonString] = tryCatch(readFileSync, dictPath, 'utf8'); | ||
|
|
||
| if (!error && jsonString) { | ||
| return JSON.parse(jsonString); | ||
| } | ||
| } | ||
|
|
||
| return {}; | ||
| }; |
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,31 @@ | ||
| import {test, stub} from 'supertape'; | ||
| import {readTranslations} from './i18n.js'; | ||
|
|
||
| test('cloudcmd: server: i18n: readTranslations: success', (t) => { | ||
| const readFileSync = stub().returns(JSON.stringify({ | ||
| hello: 'world', | ||
| })); | ||
|
|
||
| const result = readTranslations('pl', '/base', {readFileSync}); | ||
| const expected = {hello: 'world'}; | ||
|
|
||
| t.deepEqual(result, expected); | ||
| t.end(); | ||
| }); | ||
|
|
||
| test('cloudcmd: server: i18n: readTranslations: fallback on error', (t) => { | ||
| let called = false; | ||
| const readFileSync = stub(() => { | ||
| if (!called) { | ||
| called = true; | ||
| throw Error('ENOENT: no such file or directory'); | ||
| } | ||
| return JSON.stringify({hello: 'fallback'}); | ||
| }); | ||
|
|
||
| const result = readTranslations('pl', '/base', {readFileSync}); | ||
| const expected = {hello: 'fallback'}; | ||
|
|
||
| t.deepEqual(result, expected); | ||
| t.end(); | ||
| }); | ||
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} from '@cloudcmd/test-e2e'; | ||
|
|
||
| test('should inject i18n into index.html during bootstrap', async ({page}) => { | ||
| const i18nPack = await page.evaluate(() => globalThis.i18n); | ||
|
|
||
| const isObject = typeof i18nPack === 'object' && i18nPack !== null; | ||
| if (!isObject) | ||
| throw Error('i18n is not an object'); | ||
| }); | ||
|
|
||
| test('should use injected i18n translations on client-side', async ({page}) => { | ||
| await page.evaluate(() => { | ||
| globalThis.i18n = { | ||
| 'F2 - Rename': 'F2 - Zmień nazwę', | ||
| }; | ||
| }); | ||
|
|
||
| const translation = await page.evaluate(() => { | ||
| return globalThis.i18n['F2 - Rename']; | ||
| }); | ||
|
|
||
| if (translation !== 'F2 - Zmień nazwę') | ||
| throw Error('Client-side translation fallback failed'); | ||
| }); |
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.