Skip to content

Commit

Permalink
fix: update tests for compatibility with new metadata components
Browse files Browse the repository at this point in the history
  • Loading branch information
enellis authored and birtles committed Nov 27, 2024
1 parent f178c14 commit f22a32d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/content/popup/Metadata/ShogiInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function ShogiInfo({ meta }: Props) {
</span>
<span lang="ja">{serializeShogi(meta)}</span>
<span class="tp-px-1.5">=</span>
<span>{move}</span>
<span id="shogi-move">{move}</span>
</div>
);
}
4 changes: 0 additions & 4 deletions src/content/popup/lang-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,3 @@ export function getLangTag() {
}
return langTag;
}

export function clearLangTagCache() {
langTag = null;
}
26 changes: 11 additions & 15 deletions src/content/popup/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@ import { afterEach, describe, expect, it, vi } from 'vitest';

let locale: 'en' | 'ja' | 'zh_CN' = 'en';

vi.mock('webextension-polyfill', async () => {
vi.mock('../../common/i18n', async () => {
const { mockGetMessage } = await import('./mock-get-message');
return {
default: {
i18n: {
getMessage: (id: string, replacements?: string | Array<string>) =>
useLocale: () => {
return {
t: (id: string, replacements?: string | Array<string>) =>
mockGetMessage(locale, id, replacements),
},
langTag: locale,
};
},
};
});

import { ShogiMeta } from '../shogi';
import { clearLangTagCache } from './lang-tag';

import { renderMetadata } from './metadata';

function setLocale(localeToSet: 'en' | 'ja' | 'zh_CN') {
locale = localeToSet;
clearLangTagCache();
}

describe('renderShogiInfo', () => {
afterEach(() => {
setLocale('en');
locale = 'en';
});

it('renders a shogi move with a piece and destination', () => {
Expand Down Expand Up @@ -198,18 +193,19 @@ function getShogiMove(

const prevLocale = locale;
if (localeToUse) {
setLocale(localeToUse);
locale = localeToUse;
}

let result =
renderMetadata(params)?.querySelector('.value')?.textContent ?? undefined;
renderMetadata(params)?.querySelector('#shogi-move')?.textContent ??
undefined;

// Drop any zero-width spaces since we only add them for Safari's sake and
// they're not expected to affect the visual result
result = result?.replace(/\u200b/g, '');

if (prevLocale !== localeToUse) {
setLocale(prevLocale);
locale = prevLocale;
}

return result;
Expand Down

0 comments on commit f22a32d

Please sign in to comment.