forked from alan2207/bulletproof-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscussion.test.tsx
132 lines (92 loc) · 3.27 KB
/
Discussion.test.tsx
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { useParams as useMockParams } from 'react-router-dom';
import {
render,
screen,
userEvent,
waitFor,
createDiscussion,
createUser,
within,
} from '@/test/test-utils';
import { Discussion } from '../Discussion';
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'), // keep the rest of the exports intact
useParams: jest.fn(),
}));
const renderDiscussion = async () => {
const fakeUser = await createUser();
const fakeDiscussion = await createDiscussion({ teamId: fakeUser.teamId });
(useMockParams as jest.Mock).mockImplementation(() => ({
discussionId: fakeDiscussion.id,
}));
const utils = await render(<Discussion />, {
user: fakeUser,
});
await screen.findByText(fakeDiscussion.title);
return {
...utils,
fakeUser,
fakeDiscussion,
};
};
test('should render discussion', async () => {
const { fakeDiscussion } = await renderDiscussion();
expect(screen.getByText(fakeDiscussion.body)).toBeInTheDocument();
});
test('should update discussion', async () => {
const { fakeDiscussion } = await renderDiscussion();
const titleUpdate = '-Updated';
const bodyUpdate = '-Updated';
userEvent.click(screen.getByRole('button', { name: /update discussion/i }));
const drawer = screen.getByRole('dialog', {
name: /update discussion/i,
});
const titleField = within(drawer).getByText(/title/i);
const bodyField = within(drawer).getByText(/body/i);
userEvent.type(titleField, titleUpdate);
userEvent.type(bodyField, bodyUpdate);
const submitButton = within(drawer).getByRole('button', {
name: /submit/i,
});
userEvent.click(submitButton);
await waitFor(() => expect(drawer).not.toBeInTheDocument());
const newTitle = `${fakeDiscussion.title}${titleUpdate}`;
const newBody = `${fakeDiscussion.body}${bodyUpdate}`;
expect(screen.getByText(newTitle)).toBeInTheDocument();
expect(screen.getByText(newBody)).toBeInTheDocument();
});
test('should create and delete a comment on the discussion', async () => {
await renderDiscussion();
const comment = 'Hello World';
userEvent.click(screen.getByRole('button', { name: /create comment/i }));
const drawer = screen.getByRole('dialog', {
name: /create comment/i,
});
const bodyField = within(drawer).getByText(/body/i);
userEvent.type(bodyField, comment);
const submitButton = within(drawer).getByRole('button', {
name: /submit/i,
});
userEvent.click(submitButton);
await waitFor(() => expect(drawer).not.toBeInTheDocument());
const commentsList = screen.getByRole('list', {
name: 'comments',
});
const commentElements = within(commentsList).getAllByRole('listitem');
const commentElement = commentElements[0];
expect(commentElement).toBeInTheDocument();
const deleteCommentButton = within(commentElement).getByRole('button', {
name: /delete comment/i,
exact: false,
});
userEvent.click(deleteCommentButton);
const confirmationDialog = screen.getByRole('dialog', {
name: /delete comment/i,
});
const confirmationDeleteButton = within(confirmationDialog).getByRole('button', {
name: /delete/i,
});
userEvent.click(confirmationDeleteButton);
await screen.findByText(/comment deleted/i);
expect(within(commentsList).queryByText(comment)).not.toBeInTheDocument();
});