-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup jest testing framework for react
Setup Github CI job for Jest, update node version in workflow Move to react-testing-library, add test for StudentDropper
- Loading branch information
1 parent
8716c11
commit da4c74e
Showing
14 changed files
with
19,122 additions
and
7,117 deletions.
There are no files selected for viewing
This file contains 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,4 +1,4 @@ | ||
{ | ||
"presets": ["@babel/preset-react", "@babel/preset-typescript"], | ||
"plugins": ["transform-class-properties", "lodash", "@babel/plugin-transform-runtime"] | ||
"plugins": ["transform-class-properties", "lodash", "@babel/plugin-transform-runtime", "dynamic-import-node"] | ||
} |
This file contains 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 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,4 @@ | ||
// babel config for jest | ||
module.exports = { | ||
presets: ["@babel/preset-env", ["@babel/preset-react", { runtime: "automatic" }]] | ||
}; |
This file contains 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 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 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,10 @@ | ||
import React from "react"; | ||
import { render } from "@testing-library/react"; | ||
import LoadingSpinner from "../components/LoadingSpinner"; | ||
|
||
describe("LoadingSpinner", () => { | ||
it("should render correctly", () => { | ||
const component = render(<LoadingSpinner />); | ||
expect(component.asFragment()).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains 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,85 @@ | ||
import React from "react"; | ||
import { fireEvent, render } from "@testing-library/react"; | ||
import Modal, { ModalCloser } from "../components/Modal"; | ||
import { act } from "react-dom/test-utils"; | ||
|
||
/** | ||
* Returns a test Modal component. | ||
*/ | ||
const getTestModal = (handleCloseModal: React.MouseEventHandler<Element>, className = ""): React.ReactElement => { | ||
if (className === "") { | ||
return ( | ||
<Modal closeModal={handleCloseModal}> | ||
<div>Hello World</div> | ||
</Modal> | ||
); | ||
} else { | ||
return ( | ||
<Modal closeModal={handleCloseModal} className={className}> | ||
<div>Hello World</div> | ||
</Modal> | ||
); | ||
} | ||
}; | ||
|
||
describe("Modal", () => { | ||
it("should render correctly", () => { | ||
const handleCloseModal = jest.fn(); | ||
const component = render(getTestModal(handleCloseModal)); | ||
expect(component.asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
it("should render correctly with className", () => { | ||
const handleCloseModal = jest.fn(); | ||
const component = render(getTestModal(handleCloseModal, "test-modal")); | ||
expect(component.asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
describe("should close correctly", () => { | ||
test("when the close button is clicked", () => { | ||
const handleCloseModal = jest.fn(); | ||
const component = render(getTestModal(handleCloseModal)); | ||
|
||
// find and click the close button | ||
const closeButton = component.getByRole("button", { name: /close/i }); | ||
act(() => { | ||
fireEvent.click(closeButton); | ||
}); | ||
|
||
expect(handleCloseModal).toHaveBeenCalled(); | ||
}); | ||
|
||
test("when the overlay is clicked", () => { | ||
const handleCloseModal = jest.fn(); | ||
const component = render(getTestModal(handleCloseModal)); | ||
|
||
// find and click the overlay | ||
const overlay = component.container.querySelector(".modal-overlay")!; | ||
act(() => { | ||
fireEvent.click(overlay); | ||
}); | ||
|
||
expect(handleCloseModal).toHaveBeenCalled(); | ||
}); | ||
|
||
test("when an external modal closer is clicked", () => { | ||
// create a modal | ||
const handleCloseModal = jest.fn(); | ||
const component = render( | ||
<Modal closeModal={handleCloseModal} className="test-modal"> | ||
<div>Hello World</div> | ||
<ModalCloser>Close</ModalCloser> | ||
</Modal> | ||
); | ||
expect(component.asFragment()).toMatchSnapshot(); | ||
|
||
// find and click the external modal closer | ||
const closer = component.container.querySelector(".modal-close")!; | ||
act(() => { | ||
fireEvent.click(closer); | ||
}); | ||
|
||
expect(handleCloseModal).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
csm_web/frontend/src/tests/__snapshots__/LoadingSpinner.test.tsx.snap
This file contains 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,46 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`LoadingSpinner should render correctly 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
class="sk-fading-circle " | ||
> | ||
<div | ||
class="sk-circle1 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle2 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle3 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle4 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle5 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle6 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle7 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle8 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle9 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle10 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle11 sk-circle" | ||
/> | ||
<div | ||
class="sk-circle12 sk-circle" | ||
/> | ||
</div> | ||
</DocumentFragment> | ||
`; |
99 changes: 99 additions & 0 deletions
99
csm_web/frontend/src/tests/__snapshots__/Modal.test.tsx.snap
This file contains 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,99 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Modal should close correctly when an external modal closer is clicked 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
class="modal-overlay" | ||
/> | ||
<div | ||
class="modal test-modal" | ||
> | ||
<div | ||
class="modal-contents" | ||
> | ||
<div | ||
class="modal-close-x" | ||
> | ||
<button | ||
aria-label="close" | ||
class="inline-plus-sign" | ||
> | ||
<span> | ||
× | ||
</span> | ||
</button> | ||
</div> | ||
<div> | ||
Hello World | ||
</div> | ||
<div | ||
class="modal-close" | ||
> | ||
Close | ||
</div> | ||
</div> | ||
</div> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`Modal should render correctly 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
class="modal-overlay" | ||
/> | ||
<div | ||
class="modal " | ||
> | ||
<div | ||
class="modal-contents" | ||
> | ||
<div | ||
class="modal-close-x" | ||
> | ||
<button | ||
aria-label="close" | ||
class="inline-plus-sign" | ||
> | ||
<span> | ||
× | ||
</span> | ||
</button> | ||
</div> | ||
<div> | ||
Hello World | ||
</div> | ||
</div> | ||
</div> | ||
</DocumentFragment> | ||
`; | ||
|
||
exports[`Modal should render correctly with className 1`] = ` | ||
<DocumentFragment> | ||
<div | ||
class="modal-overlay" | ||
/> | ||
<div | ||
class="modal test-modal" | ||
> | ||
<div | ||
class="modal-contents" | ||
> | ||
<div | ||
class="modal-close-x" | ||
> | ||
<button | ||
aria-label="close" | ||
class="inline-plus-sign" | ||
> | ||
<span> | ||
× | ||
</span> | ||
</button> | ||
</div> | ||
<div> | ||
Hello World | ||
</div> | ||
</div> | ||
</div> | ||
</DocumentFragment> | ||
`; |
Oops, something went wrong.