Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
wandyezj committed Jun 12, 2024
1 parent d4759f4 commit b865c67
Show file tree
Hide file tree
Showing 12 changed files with 311 additions and 253 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ jobs:
steps:

# Setup
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
# Match Development Node Version
node-version: '20.x'
Expand Down Expand Up @@ -78,7 +78,7 @@ jobs:
if: inputs.test
run: npm run playwright-test

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
if: inputs.test
with:
name: playwright-report
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ jobs:
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v1
uses: actions/deploy-pages@v4
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# website

Simple Website Template

[![main CI](https://github.com/wandyezj/website/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/wandyezj/website/actions/workflows/main.yml)
[![Pages](https://github.com/wandyezj/website/actions/workflows/pages.yml/badge.svg?branch=main)](https://github.com/wandyezj/website/actions/workflows/pages.yml)
376 changes: 191 additions & 185 deletions package-lock.json

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,20 @@
"homepage": "https://github.com/wandyezj/website#readme",
"dependencies": {},
"devDependencies": {
"@playwright/test": "^1.41.2",
"@types/node": "^18.7.17",
"playwright": "^1.41.2",
"@typescript-eslint/eslint-plugin": "^7.0.2",
"@typescript-eslint/parser": "^7.0.2",
"eslint": "^8.57.0",
"@playwright/test": "^1.44.1",
"@types/node": "^20.14.2",
"@typescript-eslint/eslint-plugin": "^7.13.0",
"@typescript-eslint/parser": "^7.13.0",
"eslint": "^8.56.0",
"cspell": "8.4.1",
"prettier": "^3.2.5",
"prettier": "^3.3.2",
"clean-webpack-plugin": "^4.0.0",
"copy-webpack-plugin": "^12.0.2",
"html-webpack-plugin": "^5.5.0",
"html-webpack-plugin": "^5.6.0",
"ts-loader": "^9.5.1",
"typescript": "^5.3.3",
"webpack": "^5.90.3",
"typescript": "^5.4.5",
"webpack": "^5.92.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.1"
"webpack-dev-server": "^5.0.4"
}
}
7 changes: 7 additions & 0 deletions tests/distConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import path from "path";
import { getRootDirectory } from "./getRootDirectory";

export const localDistPath = path.resolve(getRootDirectory(), "dist");

export const rootUrlLocal = "https://localhost:3000";
export const rootUrlProduction = "https://wandyezj.github.io/website";
16 changes: 16 additions & 0 deletions tests/getLocalDistData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as fs from "fs";
import path from "path";
import { localDistPath } from "./distConstants";

export function getLocalDistData(subpath: string) {
/**
* Local generated file from build
*/
const localDistDataPath = path.resolve(localDistPath, subpath);
if (!fs.existsSync(localDistDataPath)) {
throw new Error(`cannot find localDistDataPath ${localDistDataPath}`);
}

const localDistData = fs.readFileSync(localDistDataPath);
return localDistData;
}
17 changes: 0 additions & 17 deletions tests/getLocalDistIndexData.ts

This file was deleted.

43 changes: 43 additions & 0 deletions tests/getSource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Route to use for testing
*/
export enum Source {
/**
* (default)
* Use local dist.
* Most robust for testing in a pipeline.
*/
Dist,

/**
* Use local server.
* Most useful for development.
*/
Localhost,

/**
* Use production url
*/
Production,
}

const sourceDefault = Source.Dist;
const sourceOverride: Source | undefined = undefined;

export function getSource() {
if (sourceOverride !== undefined) {
return sourceOverride;
}

const testMode = process.env["TEST_MODE"];

if (testMode === "dist") {
return Source.Dist;
}

if (testMode === "localhost") {
return Source.Localhost;
}

return sourceDefault;
}
6 changes: 4 additions & 2 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { test, expect } from "@playwright/test";
import { mainPageTitle, navigateToMainPage } from "./navigateToMainPage";

import { navigateToPage } from "./navigateToPage";
const mainPageTitle = "Website";

test("navigates to correct page title", async ({ browser }) => {
const page = await navigateToMainPage(browser);
const page = await navigateToPage(browser, "index.html", mainPageTitle);
await expect(page).toHaveTitle(mainPageTitle);
});
34 changes: 0 additions & 34 deletions tests/navigateToMainPage.ts

This file was deleted.

33 changes: 33 additions & 0 deletions tests/navigateToPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { expect, Page, Browser } from "@playwright/test";
import { getLocalDistData } from "./getLocalDistData";
import { getSource, Source } from "./getSource";
import { rootUrlLocal, rootUrlProduction } from "./distConstants";

export async function navigateToPage(browser: Browser, subpath: string, title: string): Promise<Page> {
// Create a separate browser context for each test
const context = await browser.newContext();
const page = await context.newPage();
const useSource = getSource();
const rootUrl = useSource === Source.Localhost ? rootUrlLocal : rootUrlProduction;
const pageUrl = rootUrl + "/" + subpath;

// redirect to local data
if (useSource === Source.Dist) {
// interceptor to replace content of the page
page.route(`${rootUrl}/*`, (route, request) => {
const url = request.url();
const subpath = url.substring(rootUrl.length + 1);
const body = getLocalDistData(subpath);
route.fulfill({
body,
});
});
}

await page.goto(pageUrl);

// Check that the page is the right one
await expect(page).toHaveTitle(title);

return page;
}

0 comments on commit b865c67

Please sign in to comment.