Skip to content

Commit 9676e27

Browse files
committed
test: add jest and ci workflow
1 parent 48556f8 commit 9676e27

7 files changed

Lines changed: 8926 additions & 5 deletions

File tree

.github/workflows/test.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
build:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-node@v3
15+
with:
16+
node-version: '20'
17+
- run: npm ci
18+
- run: npm run lint
19+
- run: npm test
20+
- run: npm run build

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
.idea
22
web-ext-artifacts
3+
node_modules
4+
coverage

__tests__/utils.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const { TextEncoder, TextDecoder } = require('util');
2+
global.TextEncoder = TextEncoder;
3+
global.TextDecoder = TextDecoder;
4+
const { JSDOM } = require('jsdom');
5+
const { replaceI18nPlaceholders } = require('../utils/utils');
6+
7+
describe('replaceI18nPlaceholders', () => {
8+
let dom;
9+
beforeEach(() => {
10+
dom = new JSDOM(`<!DOCTYPE html><html><head><title data-i18n="title"></title></head><body><span data-i18n="hello"></span><p>__MSG_world__</p></body></html>`);
11+
global.document = dom.window.document;
12+
global.Node = dom.window.Node;
13+
global.browser = {
14+
i18n: {
15+
getMessage: (key) => ({ title: 'Title', hello: 'Hello', world: 'World' }[key])
16+
}
17+
};
18+
});
19+
afterEach(() => {
20+
dom.window.close();
21+
delete global.document;
22+
delete global.Node;
23+
delete global.browser;
24+
});
25+
test('replaces placeholders in elements and text nodes', () => {
26+
replaceI18nPlaceholders();
27+
expect(dom.window.document.querySelector('span').textContent).toBe('Hello');
28+
expect(dom.window.document.querySelector('p').textContent).toBe('World');
29+
expect(dom.window.document.title).toBe('Title');
30+
});
31+
});

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
};

0 commit comments

Comments
 (0)