-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_frontend.ts
228 lines (188 loc) · 6.11 KB
/
test_frontend.ts
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import React from 'react';
import { render, fireEvent, waitFor, screen } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store';
import { BrowserRouter as Router } from 'react-router-dom';
import App from './App';
import * as actions from './actions';
import NewComponent from './NewComponent'; // Import the new component
// Mock the axios module
jest.mock('axios');
const mockStore = configureStore([]);
const setup = async (user) => {
const store = mockStore({
user,
});
render(
<Provider store={store}>
<Router>
<App />
</Router>
</Provider>
);
fireEvent.change(screen.getByLabelText(/username/i), {
target: { value: user.username },
});
fireEvent.change(screen.getByLabelText(/password/i), {
target: { value: user.password },
});
fireEvent.click(screen.getByText(/login/i));
await waitFor(() => screen.getByText(`/${user.role} dashboard/i`));
};
test('renders the login page', () => {
render(
<Provider store={mockStore({})}>
<Router>
<App />
</Router>
</Provider>
);
expect(screen.getByText(/login/i)).toBeInTheDocument();
});
test('navigates to the teacher dashboard', async () => {
actions.createUser = jest.fn().mockResolvedValueOnce({
data: {
username: 'teacher1',
password: 'password',
role: 'teacher',
},
});
await setup({ username: 'teacher1', password: 'password', role: 'teacher' });
expect(screen.getByText(/teacher dashboard/i)).toBeInTheDocument();
// Add more assertions here
});
test('navigates to the student dashboard', async () => {
test('renders the new component', () => {
render(
<Provider store={mockStore({})}>
<Router>
<NewComponent />
</Router>
</Provider>
);
expect(screen.getByText(/new component/i)).toBeInTheDocument();
});
test('navigates to the admin dashboard', async () => {
actions.createUser = jest.fn().mockResolvedValueOnce({
data: {
username: 'admin1',
password: 'password',
role: 'admin',
},
});
await setup({ username: 'admin1', password: 'password', role: 'admin' });
expect(screen.getByText(/admin dashboard/i)).toBeInTheDocument();
// Add more assertions here
});
test('displays an error message for unsuccessful registration', async () => {
test('navigates to the new component', async () => {
actions.createResource = jest.fn().mockResolvedValueOnce({
data: {
resource: 'newresource',
},
});
await setup({ resource: 'newresource' });
expect(screen.getByText(/new component/i)).toBeInTheDocument();
// Add more assertions here
});
actions.createUser = jest.fn().mockRejectedValueOnce({
response: {
data: {
message: 'Username already exists',
},
},
});
await setup({ username: 'existinguser', password: 'password' });
expect(screen.getByText(/username already exists/i)).toBeInTheDocument();
});
test('navigates to the parent dashboard', async () => {
actions.createUser = jest.fn().mockResolvedValueOnce({
data: {
username: 'parent1',
password: 'password',
role: 'parent',
},
});
await setup({ username: 'parent1', password: 'password', role: 'parent' });
expect(screen.getByText(/parent dashboard/i)).toBeInTheDocument();
// Add more assertions here
});
await setup({ username: 'student1', password: 'password', role: 'student' });
expect(screen.getByText(/student dashboard/i)).toBeInTheDocument();
// Add more assertions here
test('displays an error message for unsuccessful resource creation', async () => {
actions.createResource = jest.fn().mockRejectedValueOnce({
response: {
data: {
message: 'Resource already exists',
},
},
});
await setup({ resource: 'existingresource' });
expect(screen.getByText(/resource already exists/i)).toBeInTheDocument();
});
});
test('displays an error message for unsuccessful login', async () => {
actions.createUser = jest.fn().mockRejectedValueOnce({
response: {
data: {
message: 'Invalid username or password',
},
},
});
await setup({ username: 'wronguser', password: 'wrongpass' });
expect(screen.getByText(/invalid username or password/i)).toBeInTheDocument();
});
test('displays an error message for unsuccessful course creation', async () => {
test('displays an error message for unsuccessful resource update', async () => {
actions.updateResource = jest.fn().mockRejectedValueOnce({
response: {
data: {
message: 'Resource update failed',
},
},
});
await setup({ resource: 'existingresource' });
expect(screen.getByText(/resource update failed/i)).toBeInTheDocument();
});
actions.createCourse = jest.fn().mockRejectedValueOnce({
response: {
data: {
message: 'Course already exists',
},
},
});
await setup({ username: 'teacher1', password: 'password', role: 'teacher' });
fireEvent.change(screen.getByLabelText(/course name/i), {
target: { value: 'existingcourse' },
});
fireEvent.click(screen.getByText(/create course/i));
expect(screen.getByText(/course already exists/i)).toBeInTheDocument();
});
test('displays an error message for unsuccessful assignment creation', async () => {
actions.createAssignment = jest.fn().mockRejectedValueOnce({
response: {
data: {
message: 'Assignment already exists',
},
},
});
await setup({ username: 'teacher1', password: 'password', role: 'teacher' });
fireEvent.change(screen.getByLabelText(/assignment name/i), {
target: { value: 'existingassignment' },
});
fireEvent.click(screen.getByText(/create assignment/i));
expect(screen.getByText(/assignment already exists/i)).toBeInTheDocument();
});
test('displays an error message for unsuccessful resource deletion', async () => {
actions.deleteResource = jest.fn().mockRejectedValueOnce({
response: {
data: {
message: 'Resource deletion failed',
},
},
});
await setup({ resource: 'existingresource' });
expect(screen.getByText(/resource deletion failed/i)).toBeInTheDocument();
});