-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathTextbook.test.jsx
More file actions
94 lines (80 loc) · 3.14 KB
/
Textbook.test.jsx
File metadata and controls
94 lines (80 loc) · 3.14 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// @ts-check
import userEvent from '@testing-library/user-event';
import { CourseAuthoringProvider } from '@src/CourseAuthoringContext';
import { initializeMocks, render, waitFor } from '../testUtils';
import { RequestStatus } from '../data/constants';
import { executeThunk } from '../utils';
import { getTextbooksApiUrl } from './data/api';
import { fetchTextbooksQuery } from './data/thunk';
import { textbooksMock } from './__mocks__';
import { Textbooks } from '.';
import messages from './messages';
let axiosMock;
let store;
const courseId = 'course-v1:org+101+101';
const emptyTextbooksMock = { textbooks: [] };
const renderComponent = () =>
render(
<CourseAuthoringProvider courseId={courseId}>
<Textbooks />
</CourseAuthoringProvider>,
);
describe('<Textbooks />', () => {
beforeEach(async () => {
const mocks = initializeMocks();
store = mocks.reduxStore;
axiosMock = mocks.axiosMock;
axiosMock
.onGet(getTextbooksApiUrl(courseId))
.reply(200, textbooksMock);
await executeThunk(fetchTextbooksQuery(courseId), store.dispatch);
});
it('renders Textbooks component correctly', async () => {
const {
getByText,
getByRole,
getAllByTestId,
queryByTestId,
} = renderComponent();
await waitFor(() => {
expect(getByText(messages.headingTitle.defaultMessage)).toBeInTheDocument();
expect(getByText(messages.breadcrumbContent.defaultMessage)).toBeInTheDocument();
expect(getByText(messages.breadcrumbPagesAndResources.defaultMessage)).toBeInTheDocument();
expect(getByRole('button', { name: messages.newTextbookButton.defaultMessage })).toBeInTheDocument();
expect(getAllByTestId('textbook-card')).toHaveLength(2);
expect(queryByTestId('textbooks-empty-placeholder')).not.toBeInTheDocument();
});
});
it('renders textbooks form when "New textbooks" button is clicked', async () => {
const user = userEvent.setup();
const { getByTestId, getByRole } = renderComponent();
await waitFor(async () => {
const newTextbookButton = getByRole('button', { name: messages.newTextbookButton.defaultMessage });
await user.click(newTextbookButton);
expect(getByTestId('textbook-form')).toBeInTheDocument();
});
});
it('renders Textbooks component with empty placeholder correctly', async () => {
axiosMock
.onGet(getTextbooksApiUrl(courseId))
.reply(200, emptyTextbooksMock);
const { getByTestId, queryAllByTestId } = renderComponent();
await waitFor(() => {
expect(getByTestId('textbooks-empty-placeholder')).toBeInTheDocument();
expect(queryAllByTestId('textbook-card')).toHaveLength(0);
});
});
it('displays an alert and sets status to FAILED when API responds with 403', async () => {
axiosMock
.onGet(getTextbooksApiUrl(courseId))
.reply(403);
await executeThunk(fetchTextbooksQuery(courseId), store.dispatch);
const { getByTestId } = renderComponent();
await waitFor(() => {
expect(getByTestId('connectionErrorAlert')).toBeInTheDocument();
expect(store.getState().textbooks.loadingStatus).toBe(
RequestStatus.FAILED,
);
});
});
});