-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathComponentDetails.test.tsx
More file actions
113 lines (102 loc) · 3.95 KB
/
ComponentDetails.test.tsx
File metadata and controls
113 lines (102 loc) · 3.95 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { getConfig } from '@edx/frontend-platform';
import {
initializeMocks,
render as baseRender,
screen,
fireEvent,
findByDeepTextContent,
} from '@src/testUtils';
import { mockFetchIndexDocuments, mockContentSearchConfig } from '@src/search-manager/data/api.mock';
import {
mockContentLibrary,
mockGetEntityLinks,
mockLibraryBlockCreationEntry,
mockLibraryBlockDraftHistory,
mockLibraryBlockMetadata,
mockLibraryBlockPublishHistory,
mockLibraryBlockPublishHistoryEntries,
mockXBlockAssets,
mockXBlockOLX,
} from '../data/api.mocks';
import { LibraryProvider } from '../common/context/LibraryContext';
import { SidebarBodyItemId, SidebarProvider } from '../common/context/SidebarContext';
import ComponentDetails from './ComponentDetails';
mockContentSearchConfig.applyMock();
mockContentLibrary.applyMock();
mockLibraryBlockMetadata.applyMock();
mockLibraryBlockCreationEntry.applyMock();
mockLibraryBlockDraftHistory.applyMock();
mockLibraryBlockPublishHistory.applyMock();
mockLibraryBlockPublishHistoryEntries.applyMock();
mockXBlockAssets.applyMock();
mockXBlockOLX.applyMock();
mockGetEntityLinks.applyMock();
mockFetchIndexDocuments.applyMock();
const {
libraryId,
} = mockContentLibrary;
const render = (usageKey: string) =>
baseRender(<ComponentDetails />, {
path: `/library/${libraryId}/components/${usageKey}`,
params: { libraryId, selectedItemId: usageKey },
extraWrapper: ({ children }) => (
<LibraryProvider libraryId={libraryId}>
<SidebarProvider
initialSidebarItemInfo={{
id: usageKey,
type: SidebarBodyItemId.ComponentInfo,
}}
>
{children}
</SidebarProvider>
</LibraryProvider>
),
});
describe('<ComponentDetails />', () => {
beforeEach(() => {
initializeMocks();
});
it('should render the component details loading', async () => {
render(mockLibraryBlockMetadata.usageKeyThatNeverLoads);
expect(await screen.findByText('Loading...')).toBeInTheDocument();
});
it('should render the component details error', async () => {
render(mockLibraryBlockMetadata.usageKeyError404);
// Metadata and history queries fail silently; the section renders empty without crashing
expect(await screen.findByText('Component History')).toBeInTheDocument();
expect(screen.queryByText(/Mocked request failed/)).not.toBeInTheDocument();
});
it('should render the component usage', async () => {
render(mockLibraryBlockMetadata.usageKeyPublished);
expect(await screen.findByText('Component Usage')).toBeInTheDocument();
const course1 = await screen.findByText('Course 1');
expect(course1).toBeInTheDocument();
fireEvent.click(screen.getByText('Course 1'));
const course2 = screen.getByText('Course 2');
expect(course2).toBeInTheDocument();
fireEvent.click(screen.getByText('Course 2'));
const links = screen.getAllByRole('link');
// There are 2 instances in the Unit 1, but only one is shown
expect(links).toHaveLength(3);
expect(links[0]).toHaveTextContent('Unit 1');
expect(links[0]).toHaveAttribute(
'href',
`${getConfig().STUDIO_BASE_URL}/container/block-v1:org+course1+run+type@vertical+block@verticalId1`,
);
expect(links[1]).toHaveTextContent('Unit 2');
expect(links[1]).toHaveAttribute(
'href',
`${getConfig().STUDIO_BASE_URL}/container/block-v1:org+course1+run+type@vertical+block@verticalId2`,
);
expect(links[2]).toHaveTextContent('Problem Bank 3');
expect(links[2]).toHaveAttribute(
'href',
`${getConfig().STUDIO_BASE_URL}/container/block-v1:org+course2+run+type@itembank+block@itembankId3`,
);
});
it('should render the component history', async () => {
render(mockLibraryBlockMetadata.usageKeyPublished);
// usageKeyPublished matches usageKeyEmpty in mockLibraryBlockCreationEntry (TEST2 key)
expect(await findByDeepTextContent(/Author created.*Introduction to Testing 2/i)).toBeInTheDocument();
});
});