Skip to content

chore: Add initial jest tests + code coverage #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jan 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion .github/workflows/coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ jobs:
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./gotests.coverage
flags: ${{ matrix.os }}
flags: unittest-go-${{ matrix.os }}
fail_ci_if_error: true

test-js:
Expand All @@ -161,3 +161,12 @@ jobs:
- run: yarn install

- run: yarn build

- run: yarn test:coverage

- uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage/lcov.info
flags: unittest-js
fail_ci_if_error: true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ yarn-error.log

# Front-end ignore
.next/
site/.next/
site/.next/
coverage/
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ yarn-error.log

# Front-end ignore
.next/
site/.next/
site/.next/
coverage/
26 changes: 26 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
projects: [
{
coverageReporters: ["text", "lcov"],
displayName: "test",
preset: "ts-jest",
roots: ["<rootDir>/site"],
transform: {
"^.+\\.tsx?$": "ts-jest",
},
testEnvironment: "jsdom",
testRegex: "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
testPathIgnorePatterns: ["/node_modules/", "/__tests__/fakes"],
moduleDirectories: ["node_modules", "<rootDir>"],
},
],
collectCoverageFrom: [
"<rootDir>/site/**/*.js",
"<rootDir>/site/**/*.ts",
"<rootDir>/site/**/*.tsx",
"!<rootDir>/site/**/*.stories.tsx",
"!<rootDir>/site/.next/**/*.*",
"!<rootDir>/site/next-env.d.ts",
"!<rootDir>/site/next.config.js",
],
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,26 @@
"build:dev": "next build site",
"dev": "next dev site",
"format:check": "prettier --check '**/*.{css,html,js,json,jsx,md,ts,tsx,yaml,yml}'",
"format:write": "prettier --write '**/*.{css,html,js,json,jsx,md,ts,tsx,yaml,yml}'"
"format:write": "prettier --write '**/*.{css,html,js,json,jsx,md,ts,tsx,yaml,yml}'",
"test": "jest --selectProjects test",
"test:coverage": "jest --selectProjects test --collectCoverage"
},
"devDependencies": {
"@material-ui/core": "4.9.4",
"@material-ui/icons": "4.5.1",
"@material-ui/lab": "4.0.0-alpha.42",
"@testing-library/react": "12.1.2",
"@types/jest": "27.4.0",
"@types/node": "14.18.4",
"@types/react": "17.0.38",
"@types/react-dom": "17.0.11",
"@types/superagent": "4.1.14",
"jest": "27.4.7",
"next": "12.0.7",
"prettier": "2.5.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"ts-jest": "27.1.2",
"ts-loader": "9.2.6",
"typescript": "4.5.4"
}
Expand Down
58 changes: 58 additions & 0 deletions site/components/Button/SplitButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { fireEvent, render, screen } from "@testing-library/react"
import React from "react"
import { SplitButton, SplitButtonProps } from "./SplitButton"

namespace Helpers {
export type SplitButtonOptions = "a" | "b" | "c"

// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
export const callback = (selectedOption: SplitButtonOptions): void => {}

export const options: SplitButtonProps<SplitButtonOptions>["options"] = [
{
label: "test a",
value: "a",
},
{
label: "test b",
value: "b",
},
{
label: "test c",
value: "c",
},
]
}

describe("SplitButton", () => {
describe("onClick", () => {
it("is called when primary action is clicked", () => {
// Given
const mockedAndSpyedCallback = jest.fn(Helpers.callback)

// When
render(<SplitButton onClick={mockedAndSpyedCallback} options={Helpers.options} />)
fireEvent.click(screen.getByText("test a"))

// Then
expect(mockedAndSpyedCallback.mock.calls.length).toBe(1)
expect(mockedAndSpyedCallback.mock.calls[0][0]).toBe("a")
})

it("is called when clicking option in pop-up", () => {
// Given
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we confident all tests will follow this pattern? What if there are subsequent given after a when?

I'm hesitant to say we should have these comments in all tests, because I'm not sure how else you'd write the logic anyways.

Copy link
Contributor Author

@bryphe-coder bryphe-coder Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a structure we've been using on the front-end (I believe @vapurrmaid introduced it) - in fact one of my first PRs had me add this to tests: https://github.com/coder/m/pull/9720/files#r697678791

Some tests have an extra given / when / then - @vapurrmaid would know some examples of that. Might be nice to have this documented in a front-end code standards page on Notion (our current standards are pretty empty)

Copy link
Contributor

@greyscaled greyscaled Jan 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://en.wikipedia.org/wiki/Given-When-Then#:~:text=Given%2DWhen%2DThen%20(GWT,to%20write%20down%20test%20cases.&text=When%20describes%20actions%20taken%20by,part%20of%20behavior%2Ddriven%20development.

^ This is a fairly standard pattern.

You're welcome to follow up whens and thens

ie:

// Given - all your givens

// When - thing

// Then - outcome

// When - other thing

// Then - other outcome

const mockedAndSpyedCallback = jest.fn(Helpers.callback)

// When
render(<SplitButton onClick={mockedAndSpyedCallback} options={Helpers.options} />)
const buttons = screen.getAllByRole("button")
const dropdownButton = buttons[1]
fireEvent.click(dropdownButton)
fireEvent.click(screen.getByText("test c"))

// Then
expect(mockedAndSpyedCallback.mock.calls.length).toBe(1)
expect(mockedAndSpyedCallback.mock.calls[0][0]).toBe("c")
})
})
})
14 changes: 14 additions & 0 deletions site/components/EmptyState/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { screen } from "@testing-library/react"
import { render } from "../../test_helpers"
import React from "react"
import { EmptyState, EmptyStateProps } from "./index"

describe("EmptyState", () => {
it("renders (smoke test)", async () => {
// When
render(<EmptyState message="Hello, world" />)

// Then
await screen.findByText("Hello, world")
})
})
22 changes: 22 additions & 0 deletions site/components/Icons/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from "react"
import { SvgIcon } from "@material-ui/core"
import { render } from "./../../test_helpers"

import * as Icons from "./index"

const getAllIcons = (): [string, typeof SvgIcon][] => {
let k: keyof typeof Icons
let ret: [string, typeof SvgIcon][] = []
for (k in Icons) {
ret.push([k, Icons[k]])
}
return ret
}

describe("Icons", () => {
const allIcons = getAllIcons()

it.each(allIcons)(`rendering icon %p`, (_name, Icon) => {
render(<Icon />)
})
})
51 changes: 0 additions & 51 deletions site/components/Navbar/NavMenuEntry.tsx

This file was deleted.

15 changes: 15 additions & 0 deletions site/components/Navbar/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { screen } from "@testing-library/react"

import { render } from "../../test_helpers"
import { Navbar } from "./index"

describe("Navbar", () => {
it("renders content", async () => {
// When
render(<Navbar />)

// Then
await screen.findAllByText("Coder", { exact: false })
})
})
2 changes: 1 addition & 1 deletion site/components/Navbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const Navbar: React.FC = () => {
</Link>
</div>
<div className={styles.fullWidth}>
<div className={styles.title}>Hello, World - Coder v2</div>
<div className={styles.title}>Coder v2</div>
</div>
<div className={styles.fixed}>
<List>
Expand Down
15 changes: 15 additions & 0 deletions site/components/Page/Footer.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from "react"
import { screen } from "@testing-library/react"

import { render } from "../../test_helpers"
import { Footer } from "./Footer"

describe("Footer", () => {
it("renders content", async () => {
// When
render(<Footer />)

// Then
await screen.findByText("Copyright", { exact: false })
})
})
51 changes: 1 addition & 50 deletions site/components/Page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1 @@
import React from "react"
import { makeStyles } from "@material-ui/core/styles"

import { Footer } from "./Footer"
import { Navbar } from "../Navbar"

export const Page: React.FC<{ children: React.ReactNode }> = ({ children }) => {
// TODO: More interesting styling here!

const styles = useStyles()

const header = (
<div className={styles.header}>
<Navbar />
</div>
)

const footer = (
<div className={styles.footer}>
<Footer />
</div>
)

const body = <div className={styles.body}> {children}</div>

return (
<div className={styles.root}>
{header}
{body}
{footer}
</div>
)
}

const useStyles = makeStyles((theme) => ({
root: {
display: "flex",
flexDirection: "column",
},
header: {
flex: 0,
},
body: {
height: "100%",
flex: 1,
},
footer: {
flex: 0,
},
}))
export * from "./Footer"
1 change: 0 additions & 1 deletion site/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require("path")

module.exports = {
env: {},
Expand Down
Loading