|
| 1 | +/* |
| 2 | +Copyright 2023 New Vector Ltd |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { describe, expect, test, vi } from "vitest"; |
| 18 | +import { render, configure } from "@testing-library/react"; |
| 19 | + |
| 20 | +import { Toast } from "../src/Toast"; |
| 21 | +import { withFakeTimers } from "./utils/test"; |
| 22 | + |
| 23 | +configure({ |
| 24 | + defaultHidden: true, |
| 25 | +}); |
| 26 | + |
| 27 | +describe("Toast", () => { |
| 28 | + test("renders", () => { |
| 29 | + const { queryByRole } = render( |
| 30 | + <Toast open={false} onDismiss={() => {}}> |
| 31 | + Hello world! |
| 32 | + </Toast>, |
| 33 | + ); |
| 34 | + expect(queryByRole("dialog")).toBe(null); |
| 35 | + const { getByRole } = render( |
| 36 | + <Toast open={true} onDismiss={() => {}}> |
| 37 | + Hello world! |
| 38 | + </Toast>, |
| 39 | + ); |
| 40 | + expect(getByRole("dialog")).toMatchSnapshot(); |
| 41 | + }); |
| 42 | + |
| 43 | + test("dismisses itself after the specified timeout", () => { |
| 44 | + withFakeTimers(() => { |
| 45 | + const onDismiss = vi.fn(); |
| 46 | + render( |
| 47 | + <Toast open={true} onDismiss={onDismiss} autoDismiss={2000}> |
| 48 | + Hello world! |
| 49 | + </Toast>, |
| 50 | + ); |
| 51 | + vi.advanceTimersByTime(2000); |
| 52 | + expect(onDismiss).toHaveBeenCalled(); |
| 53 | + }); |
| 54 | + }); |
| 55 | +}); |
0 commit comments