@@ -111,16 +111,16 @@ describe("Toast", () => {
@@ -129,39 +129,42 @@ describe("Toast", () => {
});
it("toast should popup to the screen after click", () => {
- const { getByText: text, getByTestId: id } = render(
);
+ render(
);
- expect(text("Error")).toBeInTheDocument();
+ const errorBtn = screen.getByText("Error button");
+
+ expect(errorBtn).toBeInTheDocument();
act(() => {
- click(text("Error"));
+ click(errorBtn);
});
- expect(id("error")).toHaveTextContent("This is error");
+ expect(screen.getByTestId("error-toast")).toHaveTextContent(
+ "This is error",
+ );
});
it("should be hideToastd after clicking close button", () => {
- const {
- getByText: text,
- getByTestId: getId,
- queryByTestId: queryId,
- } = render(
);
+ render(
);
+ const errorBtn = screen.getByText("Error button");
- expect(text("Error")).toBeInTheDocument();
+ expect(errorBtn).toBeInTheDocument();
// add first
act(() => {
- click(text("Error"));
+ click(errorBtn);
});
- expect(getId("error")).toHaveTextContent("This is error");
+ expect(screen.getByTestId("error-toast")).toHaveTextContent(
+ "This is error",
+ );
// let hideToast now
act(() => {
- click(getId("error-close"));
+ click(screen.getByTestId("error-close"));
});
- expect(queryId("error")).not.toBeInTheDocument();
+ expect(screen.queryByTestId("error-toast")).not.toBeInTheDocument();
});
test("Toast renders with no a11y violations", async () => {
diff --git a/src/toast/__tests__/ToastState.test.ts b/src/toast/__tests__/ToastState.test.ts
index 0911c0de6..1adffde66 100644
--- a/src/toast/__tests__/ToastState.test.ts
+++ b/src/toast/__tests__/ToastState.test.ts
@@ -2,6 +2,7 @@ import { wait } from "reakit-test-utils";
import { renderHook, act } from "@testing-library/react-hooks";
import { useToastState } from "..";
+import { cleanup } from "@testing-library/react-hooks";
beforeEach(() => {
jest.useFakeTimers();
@@ -11,6 +12,7 @@ beforeEach(() => {
});
afterEach(() => {
+ cleanup();
(window.requestAnimationFrame as any).mockRestore();
});
@@ -18,17 +20,17 @@ describe("ToastState", () => {
it("should render correctly", () => {
const state = renderHook(() => useToastState({})).result;
- expect(state.current).toMatchInlineSnapshot(`
- Object {
- "hideToast": [Function],
- "isToastVisible": [Function],
- "removeToast": [Function],
- "showToast": [Function],
- "sortedToasts": Object {},
- "toasts": Object {},
- "toggleToast": [Function],
- }
- `);
+ state.current.showToast({ content: "hello world" });
+ state.current.showToast({ content: "hello world 2" });
+ state.current.showToast({
+ content: "hello world 3",
+ autoDismiss: true,
+ timeout: 5000,
+ type: "warning",
+ placement: "top-right",
+ });
+
+ expect(state.current).toMatchSnapshot();
});
it("should add a new toast", () => {
@@ -39,55 +41,57 @@ describe("ToastState", () => {
act(() => {
result.current.showToast({ type: "primary", content: "Hello world" });
});
- expect(Object.values(result.current.toasts)).toMatchObject([
- {
- autoDismiss: undefined,
- content: "Hello world",
- isVisible: true,
- placement: "bottom-center",
- timeout: undefined,
- type: "primary",
- },
- ]);
+
+ expect(result.current.toasts).toMatchInlineSnapshot(`
+ Object {
+ "toast-1": Object {
+ "autoDismiss": undefined,
+ "content": "Hello world",
+ "id": "toast-1",
+ "isVisible": true,
+ "placement": "bottom-center",
+ "timeout": undefined,
+ "type": "primary",
+ },
+ }
+ `);
});
it("should toggle toast", () => {
const { result } = renderHook(() => useToastState({}));
-
+ const toastId = "toast-1";
expect(result.current.toasts).toStrictEqual({});
act(() => {
result.current.showToast({ type: "primary", content: "Hello world" });
});
- const id = Object.values(result.current.toasts)[0].id;
-
act(() => {
- result.current.toggleToast({ id, isVisible: false });
+ result.current.toggleToast({ id: toastId, isVisible: false });
+ });
+
+ expect(result.current.toasts[toastId]).toMatchObject({
+ 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.showToast({ 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.removeToast(id);
+ result.current.removeToast("toast-1");
});
expect(result.current.toasts).toStrictEqual({});
});
it("should hide toast", () => {
const { result } = renderHook(() => useToastState({ animationTimeout: 5 }));
- let id = "";
expect(result.current.toasts).toStrictEqual({});
@@ -95,21 +99,24 @@ describe("ToastState", () => {
result.current.showToast({ type: "primary", content: "Hello world" });
});
expect(Object.values(result.current.toasts)).toHaveLength(1);
- id = Object.values(result.current.toasts)[0].id;
act(() => {
- result.current.hideToast(id);
+ result.current.hideToast("toast-1");
});
- expect(Object.values(result.current.toasts)).toMatchObject([
- {
- autoDismiss: undefined,
- content: "Hello world",
- isVisible: false,
- placement: "bottom-center",
- timeout: undefined,
- type: "primary",
- },
- ]);
+
+ expect(result.current.toasts).toMatchInlineSnapshot(`
+ Object {
+ "toast-1": Object {
+ "autoDismiss": undefined,
+ "content": "Hello world",
+ "id": "toast-1",
+ "isVisible": false,
+ "placement": "bottom-center",
+ "timeout": undefined,
+ "type": "primary",
+ },
+ }
+ `);
// Wait for animation timeout and after that toast should be removed
wait(
@@ -153,19 +160,6 @@ describe("ToastState", () => {
});
});
- const sortedToasts = result.current.sortedToasts;
- expect(sortedToasts["top-center"]).toHaveLength(1);
- expect(sortedToasts["bottom-center"]).toHaveLength(1);
- expect(sortedToasts["bottom-left"]).toHaveLength(2);
- expect(sortedToasts["top-right"]).toHaveLength(1);
-
- expect(sortedToasts["bottom-left"][0]).toMatchObject({
- content: "Hello world 3",
- placement: "bottom-left",
- });
- expect(sortedToasts["bottom-left"][1]).toMatchObject({
- content: "Hello world 4",
- placement: "bottom-left",
- });
+ expect(result.current.sortedToasts).toMatchSnapshot();
});
});
diff --git a/src/toast/__tests__/__snapshots__/ToastState.test.ts.snap b/src/toast/__tests__/__snapshots__/ToastState.test.ts.snap
new file mode 100644
index 000000000..0b5a0f438
--- /dev/null
+++ b/src/toast/__tests__/__snapshots__/ToastState.test.ts.snap
@@ -0,0 +1,131 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`ToastState should render correctly 1`] = `
+Object {
+ "hideToast": [Function],
+ "isToastVisible": [Function],
+ "removeToast": [Function],
+ "showToast": [Function],
+ "sortedToasts": Object {
+ "bottom-center": Array [
+ Object {
+ "autoDismiss": undefined,
+ "content": "hello world",
+ "id": "toast-1",
+ "isVisible": true,
+ "placement": "bottom-center",
+ "timeout": undefined,
+ "type": "",
+ },
+ Object {
+ "autoDismiss": undefined,
+ "content": "hello world 2",
+ "id": "toast-2",
+ "isVisible": true,
+ "placement": "bottom-center",
+ "timeout": undefined,
+ "type": "",
+ },
+ ],
+ "top-right": Array [
+ Object {
+ "autoDismiss": true,
+ "content": "hello world 3",
+ "id": "toast-3",
+ "isVisible": true,
+ "placement": "top-right",
+ "timeout": 5000,
+ "type": "warning",
+ },
+ ],
+ },
+ "toasts": Object {
+ "toast-1": Object {
+ "autoDismiss": undefined,
+ "content": "hello world",
+ "id": "toast-1",
+ "isVisible": true,
+ "placement": "bottom-center",
+ "timeout": undefined,
+ "type": "",
+ },
+ "toast-2": Object {
+ "autoDismiss": undefined,
+ "content": "hello world 2",
+ "id": "toast-2",
+ "isVisible": true,
+ "placement": "bottom-center",
+ "timeout": undefined,
+ "type": "",
+ },
+ "toast-3": Object {
+ "autoDismiss": true,
+ "content": "hello world 3",
+ "id": "toast-3",
+ "isVisible": true,
+ "placement": "top-right",
+ "timeout": 5000,
+ "type": "warning",
+ },
+ },
+ "toggleToast": [Function],
+}
+`;
+
+exports[`ToastState should test getToastToRender 1`] = `
+Object {
+ "bottom-center": Array [
+ Object {
+ "autoDismiss": undefined,
+ "content": "Hello world 2",
+ "id": "toast-2",
+ "isVisible": true,
+ "placement": "bottom-center",
+ "timeout": undefined,
+ "type": "primary",
+ },
+ ],
+ "bottom-left": Array [
+ Object {
+ "autoDismiss": undefined,
+ "content": "Hello world 3",
+ "id": "toast-3",
+ "isVisible": true,
+ "placement": "bottom-left",
+ "timeout": undefined,
+ "type": "primary",
+ },
+ Object {
+ "autoDismiss": undefined,
+ "content": "Hello world 4",
+ "id": "toast-4",
+ "isVisible": true,
+ "placement": "bottom-left",
+ "timeout": undefined,
+ "type": "primary",
+ },
+ ],
+ "top-center": Array [
+ Object {
+ "autoDismiss": undefined,
+ "content": "Hello world 1",
+ "id": "toast-1",
+ "isVisible": true,
+ "placement": "top-center",
+ "timeout": undefined,
+ "type": "primary",
+ },
+ ],
+ "top-right": Array [
+ Object {
+ "autoDismiss": undefined,
+ "content": "Hello world 5",
+ "id": "toast-5",
+ "isVisible": true,
+ "placement": "top-right",
+ "timeout": undefined,
+ "type": "primary",
+ },
+ ],
+}
+`;
diff --git a/src/utils/LiveAnnouncer.test.tsx b/src/utils/LiveAnnouncer.test.tsx
index e2e6b6fb3..9e8538ac4 100644
--- a/src/utils/LiveAnnouncer.test.tsx
+++ b/src/utils/LiveAnnouncer.test.tsx
@@ -16,42 +16,7 @@ describe("LiveAnnouncer", () => {
it("should render correctly", () => {
announce("Hello world", "assertive", 0);
- expect(document.body).toMatchInlineSnapshot(`
-
-
-
-
- Hello world
-
-
-
-
-
- `);
+ expect(document.body).toMatchSnapshot();
destroyAnnouncer();
@@ -59,6 +24,9 @@ describe("LiveAnnouncer", () => {
});
test("LiveRegionAnnouncer", () => {
+ const assertiveMessage = "Hello Assertive";
+ const politeMessage = "Hello Polite";
+
const Announcer = () => {
const ref = React.useRef();
@@ -67,14 +35,14 @@ describe("LiveAnnouncer", () => {