-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: jest testing setup * test: fixed jest typescript definitions * fix: test script * chore(deps): move deps to dev * test: added tests for accordion * test: added husky precommit test * test: axe container test * chore(infra): added github action test * test: added tests for number input * fix: a11y aria-valuetext issue with NumberInput * test: added tests for Slider * test: added tests for progress * test: added tests for link * test: added tests for breadcrumb * test: added tests for pagination * test: added tests for toast * chore(deps): remove unused raf
- Loading branch information
1 parent
76de250
commit 57ba567
Showing
5 changed files
with
300 additions
and
10 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
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,118 @@ | ||
import * as React from "react"; | ||
import { axe } from "jest-axe"; | ||
import { render } from "reakit-test-utils"; | ||
|
||
import { useToast, ToastProvider } from ".."; | ||
|
||
const Demo = () => { | ||
const { show } = useToast(); | ||
|
||
return ( | ||
<div> | ||
<button | ||
onClick={() => { | ||
show({ | ||
type: "error", | ||
content: "This is error", | ||
}); | ||
}} | ||
> | ||
Error | ||
</button> | ||
<button | ||
onClick={() => { | ||
show({ type: "success", content: "This is success" }); | ||
}} | ||
> | ||
Success | ||
</button> | ||
<button | ||
onClick={() => { | ||
show({ type: "warning", content: "This is warning" }); | ||
}} | ||
> | ||
Warning | ||
</button> | ||
<button | ||
onClick={() => { | ||
show({ | ||
content: () => ( | ||
<p style={{ fontFamily: "Impact", color: "black" }}> | ||
This is Custom | ||
</p> | ||
), | ||
}); | ||
}} | ||
> | ||
Custom | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
const ToastComp: React.FC = () => { | ||
return ( | ||
<ToastProvider | ||
placement="bottom-center" | ||
toastTypes={{ | ||
error: ({ remove, content, id }) => { | ||
return ( | ||
<div className="toast" style={{ backgroundColor: "#f02c2d" }}> | ||
{content} <button onClick={() => remove(id)}>x</button> | ||
</div> | ||
); | ||
}, | ||
success: ({ remove, content, id }) => { | ||
return ( | ||
<div className="toast" style={{ backgroundColor: "#01c24e" }}> | ||
{content} <button onClick={() => remove(id)}>x</button> | ||
</div> | ||
); | ||
}, | ||
warning: ({ remove, content, id }) => { | ||
return ( | ||
<div className="toast" style={{ backgroundColor: "#ef5013" }}> | ||
{content} <button onClick={() => remove(id)}>x</button> | ||
</div> | ||
); | ||
}, | ||
}} | ||
> | ||
<Demo /> | ||
</ToastProvider> | ||
); | ||
}; | ||
|
||
describe("Toast", () => { | ||
it("should render correctly", () => { | ||
const { baseElement } = render(<ToastComp />); | ||
|
||
expect(baseElement).toMatchInlineSnapshot(` | ||
<body> | ||
<div> | ||
<div> | ||
<button> | ||
Error | ||
</button> | ||
<button> | ||
Success | ||
</button> | ||
<button> | ||
Warning | ||
</button> | ||
<button> | ||
Custom | ||
</button> | ||
</div> | ||
</div> | ||
</body> | ||
`); | ||
}); | ||
|
||
test("Toast renders with no a11y violations", async () => { | ||
const { container } = render(<ToastComp />); | ||
const results = await axe(container); | ||
|
||
expect(results).toHaveNoViolations(); | ||
}); | ||
}); |
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,174 @@ | ||
import { wait } from "reakit-test-utils"; | ||
import { renderHook, act } from "@testing-library/react-hooks"; | ||
|
||
import { useToastState } from ".."; | ||
|
||
beforeEach(() => { | ||
jest.useFakeTimers(); | ||
jest | ||
.spyOn(window, "requestAnimationFrame") | ||
.mockImplementation((cb: any) => cb()); | ||
}); | ||
|
||
afterEach(() => { | ||
(window.requestAnimationFrame as any).mockRestore(); | ||
}); | ||
|
||
describe("ToastState", () => { | ||
it("should render correctly", () => { | ||
const state = renderHook(() => useToastState({})).result; | ||
|
||
expect(state.current).toMatchInlineSnapshot(` | ||
Object { | ||
"getToastToRender": [Function], | ||
"hide": [Function], | ||
"remove": [Function], | ||
"setToasts": [Function], | ||
"show": [Function], | ||
"toasts": Object {}, | ||
"toggle": [Function], | ||
} | ||
`); | ||
}); | ||
|
||
it("should add a new toast", () => { | ||
const { result } = renderHook(() => useToastState({})); | ||
|
||
expect(result.current.toasts).toStrictEqual({}); | ||
|
||
act(() => { | ||
result.current.show({ type: "primary", content: "Hello world" }); | ||
}); | ||
expect(Object.values(result.current.toasts)).toMatchObject([ | ||
{ | ||
autoDismiss: undefined, | ||
content: "Hello world", | ||
isVisible: true, | ||
placement: undefined, | ||
timeout: undefined, | ||
type: "primary", | ||
}, | ||
]); | ||
}); | ||
|
||
it("should toggle toast", () => { | ||
const { result } = renderHook(() => useToastState({})); | ||
|
||
expect(result.current.toasts).toStrictEqual({}); | ||
|
||
act(() => { | ||
result.current.show({ type: "primary", content: "Hello world" }); | ||
}); | ||
|
||
const id = Object.values(result.current.toasts)[0].id; | ||
|
||
act(() => { | ||
result.current.toggle({ id, isVisible: false }); | ||
}); | ||
expect(result.current.toasts[id]).toMatchObject({ isVisible: false }); | ||
}); | ||
|
||
it("should remove toast", () => { | ||
const { result } = renderHook(() => useToastState({})); | ||
|
||
expect(result.current.toasts).toStrictEqual({}); | ||
|
||
act(() => { | ||
result.current.show({ type: "primary", content: "Hello world" }); | ||
}); | ||
expect(Object.values(result.current.toasts)).toHaveLength(1); | ||
const id = Object.values(result.current.toasts)[0].id; | ||
|
||
act(() => { | ||
result.current.remove(id); | ||
}); | ||
expect(result.current.toasts).toStrictEqual({}); | ||
}); | ||
|
||
it("should hide toast", () => { | ||
const { result } = renderHook(() => useToastState({ animationTimeout: 5 })); | ||
let id = ""; | ||
|
||
expect(result.current.toasts).toStrictEqual({}); | ||
|
||
act(() => { | ||
result.current.show({ type: "primary", content: "Hello world" }); | ||
}); | ||
expect(Object.values(result.current.toasts)).toHaveLength(1); | ||
id = Object.values(result.current.toasts)[0].id; | ||
|
||
act(() => { | ||
result.current.hide(id); | ||
}); | ||
expect(Object.values(result.current.toasts)).toMatchObject([ | ||
{ | ||
autoDismiss: undefined, | ||
content: "Hello world", | ||
isVisible: false, | ||
placement: undefined, | ||
timeout: undefined, | ||
type: "primary", | ||
}, | ||
]); | ||
|
||
// Wait for animation timeout and after that toast should be removed | ||
wait( | ||
() => { | ||
expect(result.current.toasts).toStrictEqual({}); | ||
}, | ||
{ timeout: 5 }, | ||
); | ||
}); | ||
|
||
it("should test getToastToRender", () => { | ||
const { result } = renderHook(() => useToastState({})); | ||
|
||
expect(result.current.toasts).toStrictEqual({}); | ||
|
||
act(() => { | ||
result.current.show({ | ||
type: "primary", | ||
placement: "top-center", | ||
content: "Hello world 1", | ||
}); | ||
result.current.show({ | ||
type: "primary", | ||
placement: "bottom-center", | ||
content: "Hello world 2", | ||
}); | ||
result.current.show({ | ||
type: "primary", | ||
placement: "bottom-left", | ||
content: "Hello world 3", | ||
}); | ||
result.current.show({ | ||
type: "primary", | ||
placement: "bottom-left", | ||
content: "Hello world 4", | ||
}); | ||
result.current.show({ | ||
type: "primary", | ||
placement: "top-right", | ||
content: "Hello world 5", | ||
}); | ||
}); | ||
|
||
const allPositions: any[] = []; | ||
result.current.getToastToRender("bottom-center", (pos, toastList) => { | ||
allPositions.push(toastList); | ||
}); | ||
expect(allPositions[0]).toHaveLength(1); | ||
expect(allPositions[1]).toHaveLength(1); | ||
expect(allPositions[2]).toHaveLength(2); | ||
expect(allPositions[3]).toHaveLength(1); | ||
|
||
expect(allPositions[2][0]).toMatchObject({ | ||
content: "Hello world 3", | ||
placement: "bottom-left", | ||
}); | ||
expect(allPositions[2][1]).toMatchObject({ | ||
content: "Hello world 4", | ||
placement: "bottom-left", | ||
}); | ||
}); | ||
}); |
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