UNPKG

2.22 kBJavaScriptView Raw
1import React from 'react';
2import { render, fireEvent, cleanup, wait } from '@testing-library/react';
3import Feedback from '..';
4
5global.document.createRange = () => ({
6 setStart: () => {},
7 setEnd: () => {},
8 commonAncestorContainer: {
9 nodeName: 'BODY',
10 ownerDocument: document,
11 },
12});
13
14afterEach(cleanup);
15
16describe('Feedback', () => {
17 test('should show form on click', () => {
18 const { getByTestId, getByText } = render(
19 <Feedback appName="Test Space" />
20 );
21
22 fireEvent.click(getByText('Give Feedback'));
23
24 // eslint-disable-next-line unicorn/prefer-query-selector
25 expect(getByTestId('feedback-form')).toBeDefined();
26 });
27
28 test('should show modal form on click', () => {
29 const { getByTestId, getByText } = render(
30 <Feedback appName="Test Space" modal />
31 );
32
33 fireEvent.click(getByText('Give Feedback'));
34
35 // eslint-disable-next-line unicorn/prefer-query-selector
36 expect(getByTestId('feedbackModal')).toBeDefined();
37 });
38
39 test('should call onClose in form', async () => {
40 const onClose = jest.fn();
41
42 const { getByTestId, getByText } = render(
43 <Feedback appName="Test Space" formProps={{ onClose }} />
44 );
45
46 fireEvent.click(getByText('Give Feedback'));
47
48 // eslint-disable-next-line unicorn/prefer-query-selector
49 expect(getByTestId('feedback-form')).toBeDefined();
50
51 fireEvent.keyDown(getByText('Close'), {
52 key: 'Enter',
53 keyCode: 13,
54 });
55
56 await wait(() => {
57 expect(onClose).toHaveBeenCalledTimes(1);
58 });
59
60 fireEvent.click(getByText('Give Feedback'));
61
62 fireEvent.click(getByText('Close'));
63
64 await wait(() => {
65 expect(onClose).toHaveBeenCalledTimes(2);
66 });
67 });
68
69 test('should call onClose in modal', async () => {
70 const onClose = jest.fn();
71
72 const { getByTestId, getByText } = render(
73 <Feedback appName="Test Space" formProps={{ onClose }} modal />
74 );
75
76 fireEvent.click(getByText('Give Feedback'));
77
78 // eslint-disable-next-line unicorn/prefer-query-selector
79 expect(getByTestId('feedbackModal')).toBeDefined();
80
81 fireEvent.click(getByText('Close'));
82
83 await wait(() => {
84 expect(onClose).toHaveBeenCalledTimes(1);
85 });
86 });
87});