|
| 1 | +import { describe, it, expect, vi, suite, beforeEach } from "vitest"; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { render, screen } from '@testing-library/react' |
| 4 | +import * as twd from '../../'; |
| 5 | +import { TWDSidebar } from "../../ui/TWDSidebar"; |
| 6 | +import { clearTests } from "../../twdRegistry"; |
| 7 | + |
| 8 | +describe("TWDSidebar", () => { |
| 9 | + beforeEach(() => { |
| 10 | + // Clear all registered tests before each test case |
| 11 | + clearTests(); |
| 12 | + vi.clearAllMocks(); |
| 13 | + }); |
| 14 | + |
| 15 | + describe("component" , () => { |
| 16 | + it("should render TWDSidebar component closed", async () => { |
| 17 | + render(<TWDSidebar open={false} />); |
| 18 | + const sidebarElement = screen.getByText("TWD"); |
| 19 | + expect(sidebarElement).toBeInTheDocument(); |
| 20 | + }); |
| 21 | + |
| 22 | + it("should render TWDSidebar component open", async () => { |
| 23 | + render(<TWDSidebar open={true} />); |
| 24 | + const sidebarElement = screen.getByText("Run All"); |
| 25 | + expect(sidebarElement).toBeInTheDocument(); |
| 26 | + }); |
| 27 | + |
| 28 | + it("should open the sidebar when clicking the closed sidebar", async () => { |
| 29 | + const user = userEvent.setup(); |
| 30 | + render(<TWDSidebar open={false} />); |
| 31 | + const closedSidebarElement = screen.getByText("TWD"); |
| 32 | + expect(closedSidebarElement).toBeInTheDocument(); |
| 33 | + // Simulate a click event |
| 34 | + await user.click(closedSidebarElement); |
| 35 | + const openSidebarElement = screen.getByText("Run All"); |
| 36 | + expect(openSidebarElement).toBeInTheDocument(); |
| 37 | + // simulate a click event to close the sidebar |
| 38 | + const closeButton = screen.getByText("✖"); |
| 39 | + await user.click(closeButton); |
| 40 | + expect(screen.getByText("TWD")).toBeInTheDocument(); |
| 41 | + // Run all not visible |
| 42 | + expect(screen.queryByText("Run All")).not.toBeInTheDocument(); |
| 43 | + }); |
| 44 | + |
| 45 | + it('execute test when clicking the run button', async () => { |
| 46 | + const firstTest = vi.fn(); |
| 47 | + const secondTest = vi.fn(); |
| 48 | + twd.describe('Group 1', () => { |
| 49 | + twd.it('Test 1.1', firstTest); |
| 50 | + twd.itSkip('Test 1.2', secondTest); |
| 51 | + }); |
| 52 | + const user = userEvent.setup() |
| 53 | + render(<TWDSidebar open={true} />); |
| 54 | + const runAllButton = screen.getByText("Run All"); |
| 55 | + expect(runAllButton).toBeInTheDocument(); |
| 56 | + // Simulate a click event |
| 57 | + await user.click(runAllButton); |
| 58 | + expect(firstTest).toHaveBeenCalled(); |
| 59 | + expect(secondTest).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('skip test when there is a test with only', async () => { |
| 63 | + const firstTest = vi.fn(); |
| 64 | + const secondTest = vi.fn(); |
| 65 | + twd.describe('Group test only', () => { |
| 66 | + twd.it('Test no only', firstTest); |
| 67 | + twd.itOnly('Test only', secondTest); |
| 68 | + }); |
| 69 | + const user = userEvent.setup() |
| 70 | + render(<TWDSidebar open={true} />); |
| 71 | + const runAllButton = screen.getByText("Run All"); |
| 72 | + expect(runAllButton).toBeInTheDocument(); |
| 73 | + // Simulate a click event |
| 74 | + await user.click(runAllButton); |
| 75 | + expect(firstTest).not.toHaveBeenCalled(); |
| 76 | + expect(secondTest).toHaveBeenCalled(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should handle throw exceptions in tests', async () => { |
| 80 | + const errorTest = vi.fn().mockRejectedValue(new Error("Test error")); |
| 81 | + twd.describe('Group test error', () => { |
| 82 | + twd.it('Test error', errorTest); |
| 83 | + }); |
| 84 | + const user = userEvent.setup() |
| 85 | + render(<TWDSidebar open={true} />); |
| 86 | + const runAllButton = screen.getByText("Run All"); |
| 87 | + expect(runAllButton).toBeInTheDocument(); |
| 88 | + // Simulate a click event |
| 89 | + await user.click(runAllButton); |
| 90 | + expect(errorTest).toHaveBeenCalled(); |
| 91 | + const errorLog = screen.getByText(/Test failed: Test error/); |
| 92 | + expect(errorLog).toBeInTheDocument(); |
| 93 | + }); |
| 94 | + }); |
| 95 | +}); |
0 commit comments