-
Notifications
You must be signed in to change notification settings - Fork 904
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
89c44a5
Add initial test config
bryphe-coder 37cedc7
Get tests running
bryphe-coder 87cc778
Add test step to CI
bryphe-coder 93424d6
Initial test coverage
bryphe-coder 2ba0a8b
Upload coverage to codecov
bryphe-coder 1d05fa1
Generate coverage files
bryphe-coder 6bc94fc
Fix file path
bryphe-coder cad1398
Ignore next.config.js
bryphe-coder d3465d7
Formatting
bryphe-coder 707f455
Use default 'maxWorkers' setting
bryphe-coder 7b50658
Remove page component; inline navbar / footer rendering
bryphe-coder 64f8108
Formatting
bryphe-coder dc6f16b
Add tests back for Navbar / Footer
bryphe-coder a633886
Merge branch 'main' into bryphe/chore/jest-tests
bryphe-coder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,4 +16,5 @@ yarn-error.log | |
|
||
# Front-end ignore | ||
.next/ | ||
site/.next/ | ||
site/.next/ | ||
coverage/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,4 +12,5 @@ yarn-error.log | |
|
||
# Front-end ignore | ||
.next/ | ||
site/.next/ | ||
site/.next/ | ||
coverage/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
], | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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") | ||
}) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) => { | ||
bryphe-coder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
render(<Icon />) | ||
}) | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 awhen
?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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
andthens
ie: