Browse Source

Partial tests for DelayedButton

Kirk Trombley 3 years ago
parent
commit
eb982c3256

+ 1 - 1
client/src/components/util/DelayedButton.jsx

@@ -25,7 +25,7 @@ const useCountdown = (seconds, onEnd) => {
   return [remaining, () => setPaused(!paused)];
 };
 
-export const CountdownButton = ({
+const CountdownButton = ({
   onCancelled,
   onEnd,
   formatter,

+ 34 - 0
client/src/components/util/__tests__/DelayedButton.test.jsx

@@ -0,0 +1,34 @@
+import { render, fireEvent, waitFor } from "@testing-library/react";
+import DelayedButton from "../DelayedButton";
+
+global.setInterval = jest.fn();
+
+describe("DelayedButton", () => {
+  beforeEach(() => {
+    jest.resetAllMocks();
+  });
+
+  it("renders and matches snapshot", () => {
+    const { container } = render(<DelayedButton>Test</DelayedButton>);
+    expect(container.firstChild).toMatchSnapshot();
+  });
+
+  it("switches to a delay button when clicked", async () => {
+    // TODO
+    // let timerAdvance;
+    // setInterval.mockImplementation(fn => {
+    //   timerAdvance = fn;
+    // });
+    const onEnd = jest.fn();
+    const { container, getByText, queryByText } = render(
+      <DelayedButton onEnd={onEnd}>Test</DelayedButton>
+    );
+    fireEvent.click(getByText("Test"));
+    await waitFor(() => expect(queryByText("Test")).not.toBeInTheDocument());
+    expect(container.firstChild).toMatchSnapshot();
+    // TODO
+    // timerAdvance();
+    // timerAdvance();
+    // expect(container.firstChild).toMatchSnapshot();
+  });
+});

+ 17 - 0
client/src/components/util/__tests__/__snapshots__/DelayedButton.test.jsx.snap

@@ -0,0 +1,17 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`DelayedButton renders and matches snapshot 1`] = `
+<button
+  type="button"
+>
+  Test
+</button>
+`;
+
+exports[`DelayedButton switches to a delay button when clicked 1`] = `
+<button
+  type="button"
+>
+  3
+</button>
+`;