-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathTextbook.test.tsx
More file actions
74 lines (60 loc) · 2.44 KB
/
Textbook.test.tsx
File metadata and controls
74 lines (60 loc) · 2.44 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
import userEvent from '@testing-library/user-event';
import { CourseAuthoringProvider } from '@src/CourseAuthoringContext';
import {
initializeMocks,
render,
screen,
} from '@src/testUtils';
import { getTextbooksApiUrl } from './data/api';
import { textbooksMock } from './__mocks__';
import { Textbooks } from '.';
import messages from './messages';
let axiosMock;
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();
axiosMock = mocks.axiosMock;
axiosMock
.onGet(getTextbooksApiUrl(courseId))
.reply(200, textbooksMock);
});
it('renders Textbooks component correctly', async () => {
renderComponent();
expect(await screen.findByText(messages.headingTitle.defaultMessage)).toBeInTheDocument();
expect(screen.getByText(messages.breadcrumbContent.defaultMessage)).toBeInTheDocument();
expect(screen.getByText(messages.breadcrumbPagesAndResources.defaultMessage)).toBeInTheDocument();
expect(screen.getByRole('button', { name: messages.newTextbookButton.defaultMessage })).toBeInTheDocument();
expect(screen.getAllByTestId('textbook-card')).toHaveLength(2);
expect(screen.queryByTestId('textbooks-empty-placeholder')).not.toBeInTheDocument();
});
it('renders textbooks form when "New textbooks" button is clicked', async () => {
const user = userEvent.setup();
renderComponent();
const newTextbookButton = await screen.findByRole('button', { name: messages.newTextbookButton.defaultMessage });
await user.click(newTextbookButton);
expect(screen.getByTestId('textbook-form')).toBeInTheDocument();
});
it('renders Textbooks component with empty placeholder correctly', async () => {
axiosMock
.onGet(getTextbooksApiUrl(courseId))
.reply(200, emptyTextbooksMock);
renderComponent();
expect(await screen.findByTestId('textbooks-empty-placeholder')).toBeInTheDocument();
expect(screen.queryAllByTestId('textbook-card')).toHaveLength(0);
});
it('displays an alert when API responds with 403', async () => {
axiosMock
.onGet(getTextbooksApiUrl(courseId))
.reply(403);
renderComponent();
expect(await screen.findByTestId('connectionErrorAlert')).toBeInTheDocument();
});
});