-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckbox.test.tsx
More file actions
70 lines (59 loc) · 2.57 KB
/
Checkbox.test.tsx
File metadata and controls
70 lines (59 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { Checkbox } from '../';
import { render, fireEvent } from '@testing-library/react';
describe('Checkbox component', () => {
it('Checkbox disabled and not checked should render correctly', () => {
const button = render(<Checkbox disabled={true} checked={false} />);
expect(button).toMatchSnapshot();
});
it('Checkbox enabled and not checked should render correctly', () => {
const button = render(<Checkbox checked={false} />);
expect(button).toMatchSnapshot();
});
it('Checkbox checked should render correctly', () => {
const button = render(<Checkbox checked={true} />);
expect(button).toMatchSnapshot();
});
it('Checkbox disabled and checked should render correctly', () => {
const button = render(<Checkbox disabled={true} checked={true} />);
expect(button).toMatchSnapshot();
});
it('Checkbox enabled and indetermiante should render correctly', () => {
const button = render(<Checkbox checked={true} indeterminate={true} />);
expect(button).toMatchSnapshot();
});
it('Checkbox disabled and indetermiante should render correctly', () => {
const button = render(<Checkbox disabled={true} checked={true} indeterminate={true} />);
expect(button).toMatchSnapshot();
});
it('calls onClick when label is clicked and not disabled', () => {
const handleClick = vi.fn();
const { container } = render(<Checkbox onClick={handleClick as any} />);
const label = container.querySelector('label');
fireEvent.click(label!);
expect(handleClick).toHaveBeenCalled();
});
it('does not call onClick when disabled', () => {
const handleClick = vi.fn();
const { container } = render(<Checkbox disabled={true} onClick={handleClick as any} />);
const label = container.querySelector('label');
fireEvent.click(label!);
expect(handleClick).not.toHaveBeenCalled();
});
it('prevents default on inner div click', () => {
const { container } = render(<Checkbox />);
const div = container.querySelector('div');
const event = new MouseEvent('click', { bubbles: true, cancelable: true });
event.preventDefault = vi.fn();
fireEvent(div!, event);
expect(event.preventDefault).toHaveBeenCalled();
});
it('triggers onKeyDown handlers without errors', () => {
const { container } = render(<Checkbox />);
const label = container.querySelector('label');
const div = container.querySelector('div');
fireEvent.keyDown(label!, { key: 'Enter' });
fireEvent.keyDown(div!, { key: 'Enter' });
});
});