|
| 1 | +import { fireEvent, render } from '@testing-library/react' |
| 2 | +import React from 'react' |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
| 4 | +import Welcome from '../../src/components/Welcome.js' |
| 5 | + |
| 6 | +describe('Welcome Component', () => { |
| 7 | + it('renders welcome content', () => { |
| 8 | + const onClose = vi.fn() |
| 9 | + const { getByRole, getByText } = render(<Welcome onClose={onClose} />) |
| 10 | + |
| 11 | + expect(getByText('npx hyperparam')).toBeDefined() |
| 12 | + expect(getByText('Got it')).toBeDefined() |
| 13 | + const button = getByRole('button') |
| 14 | + expect(button).toBeDefined() |
| 15 | + expect(button.textContent).toBe('Got it') |
| 16 | + }) |
| 17 | + |
| 18 | + it('calls onClose when button is clicked', () => { |
| 19 | + const onClose = vi.fn() |
| 20 | + const { getByRole } = render(<Welcome onClose={onClose} />) |
| 21 | + |
| 22 | + fireEvent.click(getByRole('button')) |
| 23 | + expect(onClose).toHaveBeenCalledTimes(1) |
| 24 | + }) |
| 25 | + |
| 26 | + it('calls onClose when clicking outside the popup', () => { |
| 27 | + const onClose = vi.fn() |
| 28 | + const { container } = render(<Welcome onClose={onClose} />) |
| 29 | + |
| 30 | + // Find the backdrop element |
| 31 | + const backdropElement = container.querySelector('.welcome') |
| 32 | + expect(backdropElement).toBeDefined() |
| 33 | + |
| 34 | + if (backdropElement) { |
| 35 | + fireEvent.click(backdropElement) |
| 36 | + expect(onClose).toHaveBeenCalledTimes(1) |
| 37 | + } |
| 38 | + }) |
| 39 | + |
| 40 | + it('does not call onClose when clicking inside the popup', () => { |
| 41 | + const onClose = vi.fn() |
| 42 | + const { getByText } = render(<Welcome onClose={onClose} />) |
| 43 | + |
| 44 | + // Find and click on an element inside the popup content |
| 45 | + const paragraphElement = getByText('Supported file types include Parquet, CSV, JSON, Markdown, and Text.') |
| 46 | + fireEvent.click(paragraphElement) |
| 47 | + |
| 48 | + expect(onClose).not.toHaveBeenCalled() |
| 49 | + }) |
| 50 | + |
| 51 | + it('calls onClose when pressing Escape key', () => { |
| 52 | + const onClose = vi.fn() |
| 53 | + render(<Welcome onClose={onClose} />) |
| 54 | + |
| 55 | + // Simulate pressing the Escape key |
| 56 | + fireEvent.keyDown(window, { key: 'Escape' }) |
| 57 | + expect(onClose).toHaveBeenCalledTimes(1) |
| 58 | + }) |
| 59 | + |
| 60 | + it('does not call onClose when pressing other keys', () => { |
| 61 | + const onClose = vi.fn() |
| 62 | + render(<Welcome onClose={onClose} />) |
| 63 | + |
| 64 | + // Simulate pressing a different key |
| 65 | + fireEvent.keyDown(window, { key: 'Enter' }) |
| 66 | + expect(onClose).not.toHaveBeenCalled() |
| 67 | + }) |
| 68 | +}) |
0 commit comments