Skip to content

Commit

Permalink
export i18n API mock
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Sep 12, 2024
1 parent 40b4c80 commit 2b63bdf
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
4 changes: 2 additions & 2 deletions tests/e2e/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { test, expect } from './fixtures/base';
import { fillPopup } from './pages/popup';
import { getMessage } from './helpers';
import { i18n } from './helpers';

test.beforeEach(async ({ popup }) => {
await popup.reload();
Expand Down Expand Up @@ -80,7 +80,7 @@ test.describe('should fail to connect if:', () => {
await connectButton.click();

await expect(popup.locator('p.text-error')).toHaveText(
getMessage('connectWallet_error_invalidClient'),
i18n.getMessage('connectWallet_error_invalidClient'),
);
});
});
4 changes: 2 additions & 2 deletions tests/e2e/connect.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference types="chrome"/>
import { test, expect } from './fixtures/base';
import { connectWallet, disconnectWallet } from './pages/popup';
import { getMessage } from './helpers';
import { i18n } from './helpers';

test.beforeEach(async ({ popup }) => {
await popup.reload();
Expand Down Expand Up @@ -45,7 +45,7 @@ test('connects with correct details provided', async ({
await expect(settingsLink).toBeVisible();

await expect(popup.locator('h3')).toHaveText(
getMessage('siteNotMonetized_state_text'),
i18n.getMessage('siteNotMonetized_state_text'),
);

const storage = await background.evaluate(() => {
Expand Down
7 changes: 3 additions & 4 deletions tests/e2e/fixtures/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import {
} from '@playwright/test';
import { DIST_DIR, ROOT_DIR } from '../../../esbuild/config';

export { ROOT_DIR, SRC_DIR, DIST_DIR } from '../../../esbuild/config';

export type BrowserInfo = { browserName: string; channel: string | undefined };
export type Background = Worker;

export const testDir = path.join(ROOT_DIR, 'tests', 'e2e');
export const BUILD_DIR = DIST_DIR;

// https://playwright.dev/docs/auth#basic-shared-account-in-all-tests
export const authFile = path.join(
Expand Down Expand Up @@ -186,9 +185,9 @@ export async function loadContext(
function getPathToExtension(browserName: string) {
let pathToExtension: string;
if (browserName === 'chromium') {
pathToExtension = path.join(DIST_DIR, 'chrome');
pathToExtension = path.join(BUILD_DIR, 'chrome');
} else if (browserName === 'firefox') {
pathToExtension = path.join(DIST_DIR, 'firefox');
pathToExtension = path.join(BUILD_DIR, 'firefox');
} else {
throw new Error('Unknown browser: ' + browserName);
}
Expand Down
62 changes: 30 additions & 32 deletions tests/e2e/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
import path from 'node:path';
import { readFileSync } from 'node:fs';
import { SRC_DIR } from '../../esbuild/config';
import { BUILD_DIR } from './fixtures/helpers';
import { TranslationKeys } from '../../src/shared/helpers';

type TranslationData = Record<
TranslationKeys,
{ message: string; placeholders?: Record<string, { content: string }> }
>;

const MESSAGES = {
_cache: new Map<string, TranslationData>(),
get(lang: string) {
const cached = this._cache.get(lang);
// Replacement of browser.i18n.getMessage related APIs
export const i18n = new (class BrowserIntl {
private cache = new Map<string, TranslationData>();
private lang = 'en';

private get(lang: string) {
const cached = this.cache.get(lang);
if (cached) return cached;

const filePath = path.join(SRC_DIR, '_locales', lang, 'messages.json');
const filePath = path.join(BUILD_DIR, '_locales', lang, 'messages.json');
const data = JSON.parse(readFileSync(filePath, 'utf8')) as TranslationData;
this._cache.set(lang, data);
this.cache.set(lang, data);
return data;
},
};

// Replacement of browser.i18n.getMessage
export function getMessage(
key: TranslationKeys,
substitutions?: string | string[],
language = 'en',
) {
const msg = MESSAGES.get(language)[key] || MESSAGES.get('en')[key];
if (typeof msg === 'undefined') {
throw new Error(`Message not found: ${key}`);
}

let result = msg.message;
if (!msg.placeholders) return result;
getMessage(key: TranslationKeys, substitutions?: string | string[]) {
const msg = this.get(this.lang)[key] || this.get('en')[key];
if (typeof msg === 'undefined') {
throw new Error(`Message not found: ${key}`);
}

if (!substitutions) {
throw new Error('Missing substitutions');
}
let result = msg.message;
if (!msg.placeholders) return result;

if (typeof substitutions === 'string') {
substitutions = [substitutions];
}
if (!substitutions) {
throw new Error('Missing substitutions');
}

if (typeof substitutions === 'string') {
substitutions = [substitutions];
}

for (const [key, { content }] of Object.entries(msg.placeholders)) {
const idx = Number(content.replace('$', ''));
result = result.replaceAll(`$${key.toUpperCase()}$`, substitutions[idx]);
for (const [key, { content }] of Object.entries(msg.placeholders)) {
const idx = Number(content.replace('$', ''));
result = result.replaceAll(`$${key.toUpperCase()}$`, substitutions[idx]);
}
return result;
}
return result;
}
})();

0 comments on commit 2b63bdf

Please sign in to comment.