Skip to content

Commit f765d1c

Browse files
committed
[fix] enable Legacy Test Script based on Jest & Puppeteer
[fix] GitHub actions configuration (fix #14)
1 parent 5250823 commit f765d1c

File tree

8 files changed

+68
-43
lines changed

8 files changed

+68
-43
lines changed

.github/workflows/main.yml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
jobs:
77
Build-and-Publish:
88
runs-on: ubuntu-latest
9+
permissions:
10+
contents: write
11+
id-token: write
912
steps:
1013
- uses: actions/checkout@v4
1114

@@ -20,14 +23,10 @@ jobs:
2023
- uses: browser-actions/setup-chrome@latest
2124

2225
- name: Install Dependencies
23-
run: |
24-
pnpm i --frozen-lockfile
25-
pnpm set-chrome
26+
run: pnpm i --frozen-lockfile
2627

2728
- name: Build & Publish
28-
run: |
29-
cd test/ && pnpm i && cd ..
30-
npm publish --access public --provenance
29+
run: npm publish --access public --provenance
3130
env:
3231
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
3332

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ package-lock.json
33
dist/
44
.parcel-cache/
55
docs/
6+
.env
67
.vscode/
7-
test/yarn.lock
8+
yarn.lock

Contributing.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
## Local development
44

55
```shell
6+
npm i pnpm -g
7+
68
git clone https://github.com/EasyWebApp/cell-router.git ~/Desktop/cell-router
79
cd ~/Desktop/cell-router
810

9-
npm install
10-
npm run set-chrome
11+
pnpm i
1112
npm start
1213
```

ReadMe.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
## Demo
1111

12-
https://web-cell.dev/WebCell-scaffold/
12+
https://web-cell.dev/cell-router/preview/
1313

1414
## Feature
1515

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"@parcel/transformer-typescript-tsc": "~2.13.0",
4040
"@parcel/transformer-typescript-types": "~2.13.0",
4141
"@types/jest": "^29.5.14",
42+
"dotenv": "^16.4.5",
4243
"element-internals-polyfill": "^1.3.12",
4344
"fs-match": "^1.7.2",
4445
"husky": "^9.1.7",
@@ -58,11 +59,11 @@
5859
},
5960
"scripts": {
6061
"prepare": "husky",
61-
"set-chrome": "app-find chrome -c",
62+
"install": "app-find chrome msedge firefox -c",
6263
"preview": "cd test/ && rimraf ../.parcel-cache/ dist/ && parcel --open",
6364
"pack-preview": "cd test/ && rimraf ../.parcel-cache/ dist/ && parcel build --public-url=. --dist-dir=../docs/preview/",
6465
"pack-dist": "rimraf dist/ && parcel build source/index.ts",
65-
"test": "lint-staged && npm run pack-dist && npm run pack-preview",
66+
"test": "lint-staged && npm run pack-preview && jest --forceExit",
6667
"pack-docs": "rimraf docs/ && typedoc source/",
6768
"build": "npm run pack-dist && npm run pack-docs",
6869
"help": "npm run pack-docs && web-server docs/ -o",
@@ -75,7 +76,8 @@
7576
"tabWidth": 4
7677
},
7778
"jest": {
78-
"preset": "ts-jest"
79+
"preset": "ts-jest",
80+
"testTimeout": 10000
7981
},
8082
"lint-staged": {
8183
"*.{html,md,json,ts,tsx}": "prettier --write"

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/browser.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
1+
import 'dotenv/config';
2+
13
import { WebServer } from 'koapache';
2-
import Puppeteer, { Browser, Page } from 'puppeteer-core';
4+
import { launch, Browser, Page } from 'puppeteer-core';
35

4-
const { npm_config_chrome } = process.env;
6+
const { CI, chrome, msedge, firefox } = process.env;
57

68
var server: string, browser: Browser, page: Page;
79

810
export async function bootServer() {
911
if (server) return server;
1012

1113
const { address, port } = await new WebServer({
12-
staticPath: 'test/dist/'
14+
staticPath: 'docs/preview/'
1315
}).workerHost();
1416

1517
return (server = `http://${address}:${port}/`);
1618
}
1719

1820
export async function getPage(path: string) {
19-
browser ||= await Puppeteer.launch({
20-
executablePath: npm_config_chrome,
21+
browser ||= await launch({
22+
browser: chrome || msedge ? 'chrome' : 'firefox',
23+
executablePath: chrome || msedge || firefox,
24+
headless: !!CI,
2125
slowMo: 200
2226
});
2327
page ||= (await browser.pages())[0];
@@ -33,11 +37,10 @@ export async function expectPage(
3337
title: string,
3438
path: string
3539
) {
36-
expect(
37-
await page.$eval(selector, tag => [
38-
tag.textContent,
39-
document.title,
40-
window.location.hash
41-
])
42-
).toStrictEqual(expect.arrayContaining([content, title, path]));
40+
const result = await page.$eval(selector, tag => [
41+
tag.textContent,
42+
document.title,
43+
window.location.hash
44+
]);
45+
expect(result).toEqual([content, title, path]);
4346
}

test/index.spec.ts

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,31 @@ describe('Top router', () => {
1313
it('should render Route components', async () => {
1414
expect(await page.$eval('body', tag => tag.innerHTML.trim())).toBe(
1515
'<nav><a href="#list/1">List page</a>' +
16-
'<a href="#detail/2?edit=true">Detail page</a></nav>' +
17-
'<main class="router"><cell-route path="#list/:id" start-class="start" end-class="end"></cell-route>' +
18-
'<cell-route path="#detail/:id" start-class="start" end-class="end"></cell-route></main>'
16+
'<a href="#detail/2?edit=true">Detail page</a>' +
17+
'<a href="#async/3?edit=true">Async page</a></nav>' +
18+
'<main class="router">' +
19+
'<cell-route path="#"><div>Home Page</div></cell-route>' +
20+
'<cell-route path="#list/:id"></cell-route>' +
21+
'<cell-route path="#detail/:id"></cell-route>' +
22+
'<cell-route path="#async/:id"></cell-route>' +
23+
'</main>'
1924
);
2025
});
2126

2227
it('should turn to a page after clicking a link', async () => {
23-
await page.click('nav a:first-child');
28+
await page.click('nav a:nth-child(1)');
2429

2530
await expectPage(
26-
'cell-route:first-of-type',
31+
'cell-route:nth-child(2)',
2732
'Path: #list/1' + 'Data: {"id":"1"}',
2833
'List page',
2934
'#list/1'
3035
);
3136

32-
await page.click('nav a:last-child');
37+
await page.click('nav a:nth-child(2)');
3338

3439
await expectPage(
35-
'cell-route:last-of-type',
40+
'cell-route:nth-child(3)',
3641
'Path: #detail/2?edit=true' + 'Data: {"id":"2","edit":true}',
3742
'Detail page',
3843
'#detail/2?edit=true'
@@ -43,17 +48,17 @@ describe('Top router', () => {
4348
await page.goBack();
4449

4550
await expectPage(
46-
'cell-route:first-of-type',
47-
'Path: /#list/1' + 'Data: {"id":"1"}',
51+
'cell-route:nth-child(2)',
52+
'Path: #list/1' + 'Data: {"id":"1"}',
4853
'List page',
4954
'#list/1'
5055
);
5156

5257
await page.goForward();
5358

5459
await expectPage(
55-
'cell-route:last-of-type',
56-
'Path: /#detail/2?edit=true' + 'Data: {"id":"2","edit":true}',
60+
'cell-route:nth-child(3)',
61+
'Path: #detail/2?edit=true' + 'Data: {"id":"2","edit":true}',
5762
'Detail page',
5863
'#detail/2?edit=true'
5964
);
@@ -63,34 +68,45 @@ describe('Top router', () => {
6368
await page.reload();
6469

6570
await expectPage(
66-
'cell-route:last-of-type',
67-
'Path: /#detail/2?edit=true' + 'Data: {"id":"2","edit":true}',
71+
'cell-route:nth-child(3)',
72+
'Path: #detail/2?edit=true' + 'Data: {"id":"2","edit":true}',
6873
'Detail page',
6974
'#detail/2?edit=true'
7075
);
7176

7277
await page.goBack();
7378

7479
await expectPage(
75-
'cell-route:first-of-type',
76-
'Path: /#list/1' + 'Data: {"id":"1"}',
80+
'cell-route:nth-child(2)',
81+
'Path: #list/1' + 'Data: {"id":"1"}',
7782
'List page',
7883
'#list/1'
7984
);
8085

8186
await page.goBack();
8287

83-
await expectPage('cell-route', '', 'Cell Router', '');
88+
await expectPage('cell-route', 'Home Page', 'Cell Router', '');
8489
});
8590

8691
it('should render a page based on Changed Hash', async () => {
8792
await page.evaluate(() => (location.hash = '#list/1'));
8893

8994
await expectPage(
90-
'cell-route:first-of-type',
91-
'Path: /#list/1' + 'Data: {"id":"1"}',
95+
'cell-route:nth-child(2)',
96+
'Path: #list/1' + 'Data: {"id":"1"}',
9297
'List page',
9398
'#list/1'
9499
);
95100
});
101+
102+
it('should load an Async Page component', async () => {
103+
await page.click('nav a:nth-child(3)');
104+
105+
await expectPage(
106+
'cell-route:nth-child(4) h1',
107+
'Async',
108+
'Async page',
109+
'#async/3?edit=true'
110+
);
111+
});
96112
});

0 commit comments

Comments
 (0)