|
| 1 | +import { describe, it, expect, vi } from 'vitest'; |
| 2 | +import { render, fireEvent } from '@solidjs/testing-library'; |
| 3 | +import FeedbackModal, { FEEDBACK_TAGS } from '../../src/components/FeedbackModal'; |
| 4 | + |
| 5 | +describe('FeedbackModal', () => { |
| 6 | + it('does not render when closed', () => { |
| 7 | + const { container } = render(() => ( |
| 8 | + <FeedbackModal open={false} onClose={vi.fn()} onSubmit={vi.fn()} /> |
| 9 | + )); |
| 10 | + expect(container.querySelector('.modal-backdrop')).toBeNull(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('renders when open', () => { |
| 14 | + const { container } = render(() => ( |
| 15 | + <FeedbackModal open={true} onClose={vi.fn()} onSubmit={vi.fn()} /> |
| 16 | + )); |
| 17 | + expect(container.querySelector('.modal-backdrop')).not.toBeNull(); |
| 18 | + expect(container.querySelector('.modal')).not.toBeNull(); |
| 19 | + }); |
| 20 | + |
| 21 | + it('renders all feedback tags', () => { |
| 22 | + const { container } = render(() => ( |
| 23 | + <FeedbackModal open={true} onClose={vi.fn()} onSubmit={vi.fn()} /> |
| 24 | + )); |
| 25 | + const tags = container.querySelectorAll('.feedback-tag'); |
| 26 | + expect(tags.length).toBe(FEEDBACK_TAGS.length); |
| 27 | + for (let i = 0; i < FEEDBACK_TAGS.length; i++) { |
| 28 | + expect(tags[i]!.textContent).toBe(FEEDBACK_TAGS[i]); |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + it('toggles tag selection on click', () => { |
| 33 | + const { container } = render(() => ( |
| 34 | + <FeedbackModal open={true} onClose={vi.fn()} onSubmit={vi.fn()} /> |
| 35 | + )); |
| 36 | + const tag = container.querySelector('.feedback-tag') as HTMLElement; |
| 37 | + expect(tag.classList.contains('feedback-tag--selected')).toBe(false); |
| 38 | + fireEvent.click(tag); |
| 39 | + expect(tag.classList.contains('feedback-tag--selected')).toBe(true); |
| 40 | + fireEvent.click(tag); |
| 41 | + expect(tag.classList.contains('feedback-tag--selected')).toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it('calls onSubmit with selected tags and details', () => { |
| 45 | + const onSubmit = vi.fn(); |
| 46 | + const { container } = render(() => ( |
| 47 | + <FeedbackModal open={true} onClose={vi.fn()} onSubmit={onSubmit} /> |
| 48 | + )); |
| 49 | + const tags = container.querySelectorAll('.feedback-tag'); |
| 50 | + fireEvent.click(tags[0]!); |
| 51 | + fireEvent.click(tags[2]!); |
| 52 | + |
| 53 | + const textarea = container.querySelector('.feedback-modal__textarea') as HTMLTextAreaElement; |
| 54 | + fireEvent.input(textarea, { target: { value: 'test details' } }); |
| 55 | + |
| 56 | + const submitBtn = container.querySelector('.btn--primary') as HTMLElement; |
| 57 | + fireEvent.click(submitBtn); |
| 58 | + |
| 59 | + expect(onSubmit).toHaveBeenCalledWith( |
| 60 | + [FEEDBACK_TAGS[0], FEEDBACK_TAGS[2]], |
| 61 | + 'test details', |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('calls onClose when close button is clicked', () => { |
| 66 | + const onClose = vi.fn(); |
| 67 | + const { container } = render(() => ( |
| 68 | + <FeedbackModal open={true} onClose={onClose} onSubmit={vi.fn()} /> |
| 69 | + )); |
| 70 | + const closeBtn = container.querySelector('.modal__close') as HTMLElement; |
| 71 | + fireEvent.click(closeBtn); |
| 72 | + expect(onClose).toHaveBeenCalled(); |
| 73 | + }); |
| 74 | + |
| 75 | + it('calls onClose when clicking backdrop', () => { |
| 76 | + const onClose = vi.fn(); |
| 77 | + const { container } = render(() => ( |
| 78 | + <FeedbackModal open={true} onClose={onClose} onSubmit={vi.fn()} /> |
| 79 | + )); |
| 80 | + const backdrop = container.querySelector('.modal-backdrop') as HTMLElement; |
| 81 | + fireEvent.click(backdrop); |
| 82 | + expect(onClose).toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not call onClose when clicking modal content', () => { |
| 86 | + const onClose = vi.fn(); |
| 87 | + const { container } = render(() => ( |
| 88 | + <FeedbackModal open={true} onClose={onClose} onSubmit={vi.fn()} /> |
| 89 | + )); |
| 90 | + const modal = container.querySelector('.modal') as HTMLElement; |
| 91 | + fireEvent.click(modal); |
| 92 | + expect(onClose).not.toHaveBeenCalled(); |
| 93 | + }); |
| 94 | + |
| 95 | + it('calls onClose on Escape key', () => { |
| 96 | + const onClose = vi.fn(); |
| 97 | + render(() => <FeedbackModal open={true} onClose={onClose} onSubmit={vi.fn()} />); |
| 98 | + fireEvent.keyDown(document, { key: 'Escape' }); |
| 99 | + expect(onClose).toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + |
| 102 | + it('resets state after submit', () => { |
| 103 | + const onSubmit = vi.fn(); |
| 104 | + const { container } = render(() => ( |
| 105 | + <FeedbackModal open={true} onClose={vi.fn()} onSubmit={onSubmit} /> |
| 106 | + )); |
| 107 | + const tag = container.querySelector('.feedback-tag') as HTMLElement; |
| 108 | + fireEvent.click(tag); |
| 109 | + expect(tag.classList.contains('feedback-tag--selected')).toBe(true); |
| 110 | + |
| 111 | + const submitBtn = container.querySelector('.btn--primary') as HTMLElement; |
| 112 | + fireEvent.click(submitBtn); |
| 113 | + |
| 114 | + // After submit, tags should be deselected |
| 115 | + expect(tag.classList.contains('feedback-tag--selected')).toBe(false); |
| 116 | + }); |
| 117 | + |
| 118 | + it('renders textarea with placeholder', () => { |
| 119 | + const { container } = render(() => ( |
| 120 | + <FeedbackModal open={true} onClose={vi.fn()} onSubmit={vi.fn()} /> |
| 121 | + )); |
| 122 | + const textarea = container.querySelector('.feedback-modal__textarea') as HTMLTextAreaElement; |
| 123 | + expect(textarea).not.toBeNull(); |
| 124 | + expect(textarea.placeholder).toBe('Share details (optional)'); |
| 125 | + }); |
| 126 | +}); |
0 commit comments