Add some simple tests
All checks were successful
Run tests / test (pull_request) Successful in 4m12s

This commit is contained in:
Torjus Håkestad 2023-12-07 22:43:55 +01:00
parent 2e96de56e6
commit 8f9a8cd655
8 changed files with 4637 additions and 24 deletions

View File

@ -4,4 +4,8 @@ module.exports = {
parser: "@typescript-eslint/parser", parser: "@typescript-eslint/parser",
plugins: ["@typescript-eslint"], plugins: ["@typescript-eslint"],
root: true, root: true,
ignorePatterns: ["jest.config.js", "dist"],
overrides: [{
files: ["src/**.js"],
}]
}; };

View File

@ -0,0 +1,31 @@
name: Run tests
on:
push:
paths:
- 'src/**.ts'
- 'src/**.tsx'
pull_request:
paths:
- 'src/**.ts'
- 'src/**.tsx'
jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 5
env:
steps:
- uses: actions/checkout@v4
- name: Set up node
uses: https://github.com/actions/setup-node@v4
with:
node-version: 21
cache: npm
- name: deps
run: npm install
- name: Test
run: npm run test

5
jest.config.js Normal file
View File

@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'jsdom',
};

4540
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -7,18 +7,25 @@
"start": "parcel src/index.html", "start": "parcel src/index.html",
"build": "parcel build src/index.html", "build": "parcel build src/index.html",
"type-check": "tsc --noEmit", "type-check": "tsc --noEmit",
"test": "echo \"Error: no test specified\" && exit 1" "test": "jest"
}, },
"keywords": [], "keywords": [],
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@jest/globals": "^29.7.0",
"@testing-library/dom": "^9.3.3",
"@testing-library/react": "^14.1.2",
"@types/testing-library__jest-dom": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^6.13.2", "@typescript-eslint/eslint-plugin": "^6.13.2",
"@typescript-eslint/parser": "^6.13.2", "@typescript-eslint/parser": "^6.13.2",
"eslint": "^8.55.0", "eslint": "^8.55.0",
"jest": "^29.7.0",
"jest-environment-jsdom": "^29.7.0",
"parcel": "^2.10.3", "parcel": "^2.10.3",
"prettier": "^3.1.0", "prettier": "^3.1.0",
"process": "^0.11.10", "process": "^0.11.10",
"ts-jest": "^29.1.1",
"ts-loader": "^9.5.1", "ts-loader": "^9.5.1",
"typescript": "^5.3.2" "typescript": "^5.3.2"
}, },

View File

@ -0,0 +1,51 @@
import React from "react";
import "@testing-library/jest-dom";
import "@testing-library/jest-dom/jest-globals";
import { describe, it, expect } from "@jest/globals";
import { render, screen } from "@testing-library/react";
import { Menu } from "../menu";
describe("<Menu />", () => {
it("renders ok", () => {
const cb = () => {};
render(
<Menu
items={[{ streamKey: "streamer", viewCount: 10 }]}
selectedItem={null}
selectCallback={cb}
/>,
);
});
it("has item in list", () => {
const cb = () => {};
render(
<Menu
items={[{ streamKey: "streamer", viewCount: 10 }]}
selectedItem={null}
selectCallback={cb}
/>,
);
const streamerElement = screen.getByText("streamer");
expect(streamerElement).toBeInTheDocument();
});
it("updates selected when clicked", () => {
let selected: string | null = null;
const cb = (name: string | null) => {
selected = name;
};
render(
<Menu
items={[{ streamKey: "streamer", viewCount: 10 }]}
selectedItem={null}
selectCallback={cb}
/>,
);
const streamerElement = screen.getByText("streamer");
expect(streamerElement).toBeInTheDocument();
expect(selected).toBeNull();
streamerElement.click();
expect(selected).toStrictEqual("streamer");
});
});

View File

@ -14,34 +14,31 @@ export function Menu({ items, selectedItem, selectCallback }: MenuProps) {
const menuitems = () => { const menuitems = () => {
return ( return (
<> <>
{items.map((value, idx) => { {items.map((value) => {
const key = `${value.streamKey}_${value.viewCount}`
if (selectedItem == value.streamKey) { if (selectedItem == value.streamKey) {
return ( return (
<> <li key={key} className="menu-item menu-selected">
<li key={idx} className="menu-item menu-selected"> <a className="menu-link">
<a href={"#" + value} className="menu-link">
{value.streamKey} {value.streamKey}
</a> </a>
<p className="menu-viewcount">{value.viewCount}</p> <p className="menu-viewcount">{value.viewCount}</p>
</li> </li>
</>
); );
} else { } else {
return ( return (
<>
<li <li
key={idx} key={value.streamKey}
onClick={() => { onClick={() => {
selectCallback(value.streamKey); selectCallback(value.streamKey);
}} }}
className="menu-item" className="menu-item"
> >
<a href={"#" + value.streamKey} className="menu-link"> <a className="menu-link">
{value.streamKey} {value.streamKey}
</a> </a>
<p className="menu-viewcount">{value.viewCount}</p> <p className="menu-viewcount">{value.viewCount}</p>
</li> </li>
</>
); );
} }
})} })}
@ -56,8 +53,7 @@ export function Menu({ items, selectedItem, selectCallback }: MenuProps) {
onClick={() => { onClick={() => {
selectCallback(null); selectCallback(null);
}} }}
href="#" href="#">
>
{title} {title}
</a> </a>
<ul className="menu-list">{menuitems()}</ul> <ul className="menu-list">{menuitems()}</ul>

View File

@ -7,7 +7,8 @@
"lib": ["dom", "esnext"], "lib": ["dom", "esnext"],
"strict": true, "strict": true,
"sourceMap": true, "sourceMap": true,
"target": "esnext" "target": "esnext",
"types": ["node", "jest", "@testing-library/jest-dom"]
}, },
"exclude": ["node_modules"], "exclude": ["node_modules"],
"include": ["src/**/*.ts", "src/**/*.tsx"] "include": ["src/**/*.ts", "src/**/*.tsx"]