Skip to content

Commit

Permalink
move i18n to fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
sidvishnoi committed Sep 12, 2024
1 parent c30cce0 commit 5c80e56
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 52 deletions.
3 changes: 1 addition & 2 deletions tests/e2e/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test, expect } from './fixtures/base';
import { fillPopup } from './pages/popup';
import { i18n } from './helpers';

test.beforeEach(async ({ popup }) => {
await popup.reload();
Expand Down Expand Up @@ -68,7 +67,7 @@ test.describe('should fail to connect if:', () => {
).toEqual({ connected: false });
});

test('public key not added', async ({ popup }) => {
test('public key not added', async ({ popup, i18n }) => {
const { CONNECT_WALLET_ADDRESS_URL } = process.env;
expect(CONNECT_WALLET_ADDRESS_URL).toBeDefined();

Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/connect.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/// <reference types="chrome"/>
import { test, expect } from './fixtures/base';
import { connectWallet, disconnectWallet } from './pages/popup';
import { i18n } from './helpers';

test.beforeEach(async ({ popup }) => {
await popup.reload();
Expand All @@ -11,6 +10,7 @@ test('connects with correct details provided', async ({
persistentContext,
background,
popup,
i18n,
}) => {
const {
CONNECT_KEY_ID,
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e/fixtures/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import {
getBackground,
getExtensionId,
loadContext,
BrowserIntl,
type Background,
} from './helpers';
import { openPopup, type Popup } from '../pages/popup';

type BaseScopeWorker = {
persistentContext: BrowserContext;
background: Background;
i18n: BrowserIntl;
/**
* IMPORTANT: This is created once per test file. Mutating/closing could
* impact other tests in same file.
Expand Down Expand Up @@ -40,6 +42,14 @@ export const test = base.extend<{ page: Page }, BaseScopeWorker>({
{ scope: 'worker' },
],

i18n: [
async ({ browserName }, use) => {
const i18n = new BrowserIntl(browserName);
await use(i18n);
},
{ scope: 'worker' },
],

popup: [
async ({ background, persistentContext, browserName, channel }, use) => {
const extensionId = getExtensionId(browserName, background);
Expand Down
59 changes: 59 additions & 0 deletions tests/e2e/fixtures/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Buffer } from 'node:buffer';
import net from 'node:net';
import path from 'node:path';
import { readFileSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import {
chromium,
Expand All @@ -12,6 +13,7 @@ import {
type Worker,
} from '@playwright/test';
import { DIST_DIR, ROOT_DIR } from '../../../esbuild/config';
import type { TranslationKeys } from '../../../src/shared/helpers';

export type BrowserInfo = { browserName: string; channel: string | undefined };
export type Background = Worker;
Expand Down Expand Up @@ -264,3 +266,60 @@ export async function loadKeysToExtension(
throw new Error('Could not load keys to extension');
}
}

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

/**
* Replacement of browser.i18n.getMessage related APIs
*/
export class BrowserIntl {
private cache = new Map<string, TranslationData>();
private lang = 'en';
private pathToExtension: string;

constructor(browserName: string) {
this.pathToExtension = getPathToExtension(browserName);
}

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

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

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}`);
}

let result = msg.message;
if (!msg.placeholders) return result;

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]);
}
return result;
}
}
49 changes: 0 additions & 49 deletions tests/e2e/helpers.ts

This file was deleted.

0 comments on commit 5c80e56

Please sign in to comment.