1
- import React from 'react' ;
2
- import { AppProvider } from '@edx/frontend-platform/react' ;
3
- import { initializeMockApp } from '@edx/frontend-platform' ;
4
- import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth' ;
5
- import { IntlProvider } from '@edx/frontend-platform/i18n' ;
6
- import { render , fireEvent , waitFor } from '@testing-library/react' ;
7
- import MockAdapter from 'axios-mock-adapter' ;
8
- import type { Store } from 'redux' ;
9
-
10
- import { ToastProvider } from '../../generic/toast-context' ;
1
+ import {
2
+ fireEvent ,
3
+ render as baseRender ,
4
+ screen ,
5
+ waitFor ,
6
+ initializeMocks ,
7
+ } from '../../testUtils' ;
8
+ import { LibraryProvider } from '../common/context' ;
11
9
import { getClipboardUrl } from '../../generic/data/api' ;
12
10
import { ContentHit } from '../../search-manager' ;
13
- import initializeStore from '../../store' ;
14
11
import ComponentCard from './ComponentCard' ;
15
12
16
- let store : Store ;
17
- let axiosMock : MockAdapter ;
18
-
19
13
const contentHit : ContentHit = {
20
14
id : '1' ,
21
15
usageKey : 'lb:org1:demolib:html:a1fa8bdd-dc67-4976-9bf5-0ea75a9bca3d' ,
22
16
type : 'library_block' ,
23
17
blockId : 'a1fa8bdd-dc67-4976-9bf5-0ea75a9bca3d' ,
24
- contextKey : 'lb :org1:Demo_Course' ,
18
+ contextKey : 'lib :org1:Demo_Course' ,
25
19
org : 'org1' ,
26
20
breadcrumbs : [ { displayName : 'Demo Lib' } ] ,
27
21
displayName : 'Text Display Name' ,
@@ -47,87 +41,63 @@ const clipboardBroadcastChannelMock = {
47
41
48
42
( global as any ) . BroadcastChannel = jest . fn ( ( ) => clipboardBroadcastChannelMock ) ;
49
43
50
- const RootWrapper = ( ) => (
51
- < AppProvider store = { store } >
52
- < IntlProvider locale = "en" >
53
- < ToastProvider >
54
- < ComponentCard
55
- contentHit = { contentHit }
56
- blockTypeDisplayName = "text"
57
- />
58
- </ ToastProvider >
59
- </ IntlProvider >
60
- </ AppProvider >
61
- ) ;
44
+ const libraryId = 'lib:org1:Demo_Course' ;
45
+ const render = ( ) => baseRender ( < ComponentCard contentHit = { contentHit } blockTypeDisplayName = "text" /> , {
46
+ extraWrapper : ( { children } ) => < LibraryProvider libraryId = { libraryId } > { children } </ LibraryProvider > ,
47
+ } ) ;
62
48
63
49
describe ( '<ComponentCard />' , ( ) => {
64
- beforeEach ( ( ) => {
65
- initializeMockApp ( {
66
- authenticatedUser : {
67
- userId : 3 ,
68
- username : 'abc123' ,
69
- administrator : true ,
70
- roles : [ ] ,
71
- } ,
72
- } ) ;
73
- store = initializeStore ( ) ;
74
-
75
- axiosMock = new MockAdapter ( getAuthenticatedHttpClient ( ) ) ;
76
- } ) ;
77
-
78
- afterEach ( ( ) => {
79
- jest . clearAllMocks ( ) ;
80
- axiosMock . restore ( ) ;
81
- } ) ;
82
-
83
50
it ( 'should render the card with title and description' , ( ) => {
84
- const { getByText } = render ( < RootWrapper /> ) ;
51
+ initializeMocks ( ) ;
52
+ render ( ) ;
85
53
86
- expect ( getByText ( 'Text Display Formated Name' ) ) . toBeInTheDocument ( ) ;
87
- expect ( getByText ( 'This is a text: ID=1' ) ) . toBeInTheDocument ( ) ;
54
+ expect ( screen . getByText ( 'Text Display Formated Name' ) ) . toBeInTheDocument ( ) ;
55
+ expect ( screen . getByText ( 'This is a text: ID=1' ) ) . toBeInTheDocument ( ) ;
88
56
} ) ;
89
57
90
58
it ( 'should call the updateClipboard function when the copy button is clicked' , async ( ) => {
59
+ const { axiosMock, mockShowToast } = initializeMocks ( ) ;
91
60
axiosMock . onPost ( getClipboardUrl ( ) ) . reply ( 200 , { } ) ;
92
- const { getByRole , getByTestId , getByText } = render ( < RootWrapper /> ) ;
61
+ render ( ) ;
93
62
94
63
// Open menu
95
- expect ( getByTestId ( 'component-card-menu-toggle' ) ) . toBeInTheDocument ( ) ;
96
- fireEvent . click ( getByTestId ( 'component-card-menu-toggle' ) ) ;
64
+ expect ( screen . getByTestId ( 'component-card-menu-toggle' ) ) . toBeInTheDocument ( ) ;
65
+ fireEvent . click ( screen . getByTestId ( 'component-card-menu-toggle' ) ) ;
97
66
98
67
// Click copy to clipboard
99
- expect ( getByRole ( 'button' , { name : 'Copy to clipboard' } ) ) . toBeInTheDocument ( ) ;
100
- fireEvent . click ( getByRole ( 'button' , { name : 'Copy to clipboard' } ) ) ;
68
+ expect ( screen . getByRole ( 'button' , { name : 'Copy to clipboard' } ) ) . toBeInTheDocument ( ) ;
69
+ fireEvent . click ( screen . getByRole ( 'button' , { name : 'Copy to clipboard' } ) ) ;
101
70
102
71
expect ( axiosMock . history . post . length ) . toBe ( 1 ) ;
103
72
expect ( axiosMock . history . post [ 0 ] . data ) . toBe (
104
73
JSON . stringify ( { usage_key : contentHit . usageKey } ) ,
105
74
) ;
106
75
107
76
await waitFor ( ( ) => {
108
- expect ( getByText ( 'Component copied to clipboard' ) ) . toBeInTheDocument ( ) ;
77
+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Component copied to clipboard' ) ;
109
78
} ) ;
110
79
} ) ;
111
80
112
81
it ( 'should show error message if the api call fails' , async ( ) => {
82
+ const { axiosMock, mockShowToast } = initializeMocks ( ) ;
113
83
axiosMock . onPost ( getClipboardUrl ( ) ) . reply ( 400 ) ;
114
- const { getByRole , getByTestId , getByText } = render ( < RootWrapper /> ) ;
84
+ render ( ) ;
115
85
116
86
// Open menu
117
- expect ( getByTestId ( 'component-card-menu-toggle' ) ) . toBeInTheDocument ( ) ;
118
- fireEvent . click ( getByTestId ( 'component-card-menu-toggle' ) ) ;
87
+ expect ( screen . getByTestId ( 'component-card-menu-toggle' ) ) . toBeInTheDocument ( ) ;
88
+ fireEvent . click ( screen . getByTestId ( 'component-card-menu-toggle' ) ) ;
119
89
120
90
// Click copy to clipboard
121
- expect ( getByRole ( 'button' , { name : 'Copy to clipboard' } ) ) . toBeInTheDocument ( ) ;
122
- fireEvent . click ( getByRole ( 'button' , { name : 'Copy to clipboard' } ) ) ;
91
+ expect ( screen . getByRole ( 'button' , { name : 'Copy to clipboard' } ) ) . toBeInTheDocument ( ) ;
92
+ fireEvent . click ( screen . getByRole ( 'button' , { name : 'Copy to clipboard' } ) ) ;
123
93
124
94
expect ( axiosMock . history . post . length ) . toBe ( 1 ) ;
125
95
expect ( axiosMock . history . post [ 0 ] . data ) . toBe (
126
96
JSON . stringify ( { usage_key : contentHit . usageKey } ) ,
127
97
) ;
128
98
129
99
await waitFor ( ( ) => {
130
- expect ( getByText ( 'Failed to copy component to clipboard' ) ) . toBeInTheDocument ( ) ;
100
+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Failed to copy component to clipboard' ) ;
131
101
} ) ;
132
102
} ) ;
133
103
} ) ;
0 commit comments