UNPKG

3.65 kBJavaScriptView Raw
1import React from 'react';
2import { render, screen } from '@testing-library/react';
3import user from '@testing-library/user-event';
4import '@testing-library/jest-dom';
5import { testForCustomClass, testForCustomTag } from '../testUtils';
6import { Alert } from '..';
7
8describe('Alert', () => {
9 beforeEach(() => {
10 jest.resetModules();
11 jest.useFakeTimers();
12 });
13
14 it('should render children', () => {
15 render(<Alert>Yo!</Alert>);
16 expect(screen.getByText('Yo!')).toBeInTheDocument();
17 });
18
19 it('should render additional classes', () => {
20 testForCustomClass(Alert);
21 });
22
23 it('should render custom tag', () => {
24 testForCustomTag(Alert);
25 });
26
27 it('should pass close className down', () => {
28 const noop = () => {};
29 render(
30 <Alert toggle={noop} closeClassName="test-class-name">
31 Yo!
32 </Alert>,
33 );
34
35 expect(screen.getByLabelText('Close')).toHaveClass('test-class-name');
36 });
37
38 it('should pass other props down', () => {
39 render(<Alert data-testprop="testvalue">Yo!</Alert>);
40 expect(screen.getByText('Yo!')).toHaveAttribute(
41 'data-testprop',
42 'testvalue',
43 );
44 });
45
46 it('should have "success" as default color', () => {
47 render(<Alert>Yo!</Alert>);
48 expect(screen.getByText('Yo!')).toHaveClass('alert-success');
49 });
50
51 it('should accept color prop', () => {
52 render(<Alert color="warning">Yo!</Alert>);
53 expect(screen.getByText('Yo!')).toHaveClass('alert-warning');
54 });
55
56 it('should use a div tag by default', () => {
57 render(<Alert>Yo!</Alert>);
58 expect(screen.getByText('Yo!').tagName.toLowerCase()).toEqual('div');
59 });
60
61 it('should be non dismissible by default', () => {
62 render(<Alert>Yo!</Alert>);
63
64 expect(screen.queryByLabelText('Close')).toBe(null);
65 expect(screen.getByText('Yo!')).not.toHaveClass('alert-dismissible');
66 });
67
68 it('should show dismiss button if passed toggle', () => {
69 render(
70 <Alert color="danger" toggle={() => {}}>
71 Yo!
72 </Alert>,
73 );
74
75 expect(screen.getByLabelText('Close')).toBeInTheDocument();
76 expect(screen.getByText('Yo!')).toHaveClass('alert-dismissible');
77 });
78
79 it('should be empty if not isOpen', () => {
80 render(<Alert isOpen={false}>Yo!</Alert>);
81 expect(screen.queryByText('Yo!')).toBe(null);
82 });
83
84 it('should be dismissible', () => {
85 const mockFn = jest.fn();
86 render(
87 <Alert color="danger" toggle={mockFn}>
88 Yo!
89 </Alert>,
90 );
91 screen.getByText('Yo!');
92
93 user.click(screen.getByLabelText('Close'));
94 expect(mockFn).toHaveBeenCalled();
95 });
96
97 it('should render close button with custom aria-label', () => {
98 render(
99 <Alert toggle={() => {}} closeAriaLabel="oseclay">
100 Yo!
101 </Alert>,
102 );
103
104 expect(screen.getByLabelText('oseclay')).toBeInTheDocument();
105 });
106
107 it('should have default transitionTimeouts', () => {
108 render(<Alert>Hello</Alert>);
109
110 expect(screen.getByText(/hello/i)).not.toHaveClass('show');
111
112 jest.advanceTimersByTime(150);
113 expect(screen.getByText(/hello/i)).toHaveClass('show');
114 });
115
116 it('should have support configurable transitionTimeouts', () => {
117 render(
118 <Alert
119 transition={{
120 timeout: 0,
121 appear: false,
122 enter: false,
123 exit: false,
124 }}
125 >
126 Hello
127 </Alert>,
128 );
129
130 expect(screen.getByText(/hello/i)).toHaveClass('show');
131 });
132
133 it('works with strict mode', () => {
134 const spy = jest.spyOn(console, 'error');
135 render(
136 <React.StrictMode>
137 <Alert>Hello</Alert>
138 </React.StrictMode>,
139 );
140 expect(spy).not.toHaveBeenCalled();
141 });
142});