1
- import React from 'react' ;
2
- import { Helmet } from 'react-helmet' ;
3
- import { shallow } from '@edx/react-unit-test-utils' ;
1
+ import { render , screen , waitFor } from '@testing-library/react' ;
4
2
5
- import { useIntl } from '@edx/frontend-platform/i18n' ;
3
+ import { IntlProvider } from '@edx/frontend-platform/i18n' ;
6
4
import { getConfig } from '@edx/frontend-platform' ;
7
5
8
6
import { RequestKeys } from 'data/constants/requests' ;
9
7
import { reduxHooks } from 'hooks' ;
10
- import Dashboard from 'containers/Dashboard' ;
11
- import LearnerDashboardHeader from 'containers/LearnerDashboardHeader' ;
12
- import AppWrapper from 'containers/WidgetContainers/AppWrapper' ;
13
8
import { App } from './App' ;
14
9
import messages from './messages' ;
15
10
16
- jest . mock ( '@edx/frontend-component-footer' , ( ) => ( { FooterSlot : 'FooterSlot' } ) ) ;
17
-
18
- jest . mock ( 'containers/Dashboard' , ( ) => 'Dashboard' ) ;
19
- jest . mock ( 'containers/LearnerDashboardHeader' , ( ) => 'LearnerDashboardHeader' ) ;
20
- jest . mock ( 'containers/WidgetContainers/AppWrapper' , ( ) => 'AppWrapper' ) ;
11
+ jest . mock ( '@edx/frontend-component-footer' , ( ) => ( {
12
+ FooterSlot : jest . fn ( ( ) => < div > FooterSlot</ div > ) ,
13
+ } ) ) ;
14
+ jest . mock ( 'containers/Dashboard' , ( ) => jest . fn ( ( ) => < div > Dashboard</ div > ) ) ;
15
+ jest . mock ( 'containers/LearnerDashboardHeader' , ( ) => jest . fn ( ( ) => < div > LearnerDashboardHeader</ div > ) ) ;
16
+ jest . mock ( 'containers/WidgetContainers/AppWrapper' , ( ) => jest . fn ( ( { children } ) => < div className = "AppWrapper" > { children } </ div > ) ) ;
21
17
jest . mock ( 'data/redux' , ( ) => ( {
22
18
selectors : 'redux.selectors' ,
23
19
actions : 'redux.actions' ,
@@ -36,113 +32,102 @@ jest.mock('@edx/frontend-platform', () => ({
36
32
getConfig : jest . fn ( ( ) => ( { } ) ) ,
37
33
} ) ) ;
38
34
35
+ jest . unmock ( '@openedx/paragon' ) ;
36
+ jest . unmock ( '@edx/frontend-platform/i18n' ) ;
37
+ jest . unmock ( 'react' ) ;
38
+
39
39
const loadData = jest . fn ( ) ;
40
40
reduxHooks . useLoadData . mockReturnValue ( loadData ) ;
41
41
42
- let el ;
43
-
44
- const supportEmail = 'test-support-url' ;
42
+ const supportEmail = '[email protected] ' ;
45
43
reduxHooks . usePlatformSettingsData . mockReturnValue ( { supportEmail } ) ;
46
44
47
45
describe ( 'App router component' , ( ) => {
48
- const { formatMessage } = useIntl ( ) ;
49
46
describe ( 'component' , ( ) => {
50
47
const runBasicTests = ( ) => {
51
- test ( 'snapshot' , ( ) => { expect ( el . snapshot ) . toMatchSnapshot ( ) ; } ) ;
52
- it ( 'displays title in helmet component' , ( ) => {
53
- const control = el . instance
54
- . findByType ( Helmet ) [ 0 ]
55
- . findByType ( 'title' ) [ 0 ] ;
56
- expect ( control . children [ 0 ] . el ) . toEqual ( formatMessage ( messages . pageTitle ) ) ;
48
+ it ( 'displays title in helmet component' , async ( ) => {
49
+ await waitFor ( ( ) => expect ( document . title ) . toEqual ( messages . pageTitle . defaultMessage ) ) ;
57
50
} ) ;
58
51
it ( 'displays learner dashboard header' , ( ) => {
59
- expect ( el . instance . findByType ( LearnerDashboardHeader ) . length ) . toEqual ( 1 ) ;
52
+ const learnerDashboardHeader = screen . getByText ( 'LearnerDashboardHeader' ) ;
53
+ expect ( learnerDashboardHeader ) . toBeInTheDocument ( ) ;
60
54
} ) ;
61
55
it ( 'wraps the header and main components in an AppWrapper widget container' , ( ) => {
62
- const container = el . instance . findByType ( AppWrapper ) [ 0 ] ;
63
- expect ( container . children [ 0 ] . type ) . toEqual ( 'LearnerDashboardHeader' ) ;
64
- expect ( container . children [ 1 ] . type ) . toEqual ( 'main' ) ;
56
+ const appWrapper = screen . getByText ( 'LearnerDashboardHeader' ) . parentElement ;
57
+ expect ( appWrapper ) . toHaveClass ( 'AppWrapper' ) ;
58
+ expect ( appWrapper . children [ 1 ] . id ) . toEqual ( 'main' ) ;
59
+ } ) ;
60
+ it ( 'displays footer slot' , ( ) => {
61
+ const footerSlot = screen . getByText ( 'FooterSlot' ) ;
62
+ expect ( footerSlot ) . toBeInTheDocument ( ) ;
65
63
} ) ;
66
64
} ;
67
65
describe ( 'no network failure' , ( ) => {
68
- beforeAll ( ( ) => {
66
+ beforeEach ( ( ) => {
67
+ jest . clearAllMocks ( ) ;
69
68
reduxHooks . useRequestIsFailed . mockReturnValue ( false ) ;
70
69
getConfig . mockReturnValue ( { } ) ;
71
- el = shallow ( < App /> ) ;
70
+ render ( < IntlProvider locale = "en" > < App /> </ IntlProvider > ) ;
72
71
} ) ;
73
72
runBasicTests ( ) ;
74
73
it ( 'loads dashboard' , ( ) => {
75
- const main = el . instance . findByType ( 'main' ) [ 0 ] ;
76
- expect ( main . children . length ) . toEqual ( 1 ) ;
77
- const dashboard = main . children [ 0 ] . el ;
78
- expect ( dashboard . type ) . toEqual ( 'Dashboard' ) ;
79
- expect ( dashboard ) . toEqual ( shallow ( < Dashboard /> ) ) ;
74
+ const dashboard = screen . getByText ( 'Dashboard' ) ;
75
+ expect ( dashboard ) . toBeInTheDocument ( ) ;
80
76
} ) ;
81
77
} ) ;
82
78
describe ( 'no network failure with optimizely url' , ( ) => {
83
- beforeAll ( ( ) => {
79
+ beforeEach ( ( ) => {
80
+ jest . clearAllMocks ( ) ;
84
81
reduxHooks . useRequestIsFailed . mockReturnValue ( false ) ;
85
82
getConfig . mockReturnValue ( { OPTIMIZELY_URL : 'fake.url' } ) ;
86
- el = shallow ( < App /> ) ;
83
+ render ( < IntlProvider locale = "en" > < App /> </ IntlProvider > ) ;
87
84
} ) ;
88
85
runBasicTests ( ) ;
89
86
it ( 'loads dashboard' , ( ) => {
90
- const main = el . instance . findByType ( 'main' ) [ 0 ] ;
91
- expect ( main . children . length ) . toEqual ( 1 ) ;
92
- const dashboard = main . children [ 0 ] . el ;
93
- expect ( dashboard . type ) . toEqual ( 'Dashboard' ) ;
94
- expect ( dashboard ) . toEqual ( shallow ( < Dashboard /> ) ) ;
87
+ const dashboard = screen . getByText ( 'Dashboard' ) ;
88
+ expect ( dashboard ) . toBeInTheDocument ( ) ;
95
89
} ) ;
96
90
} ) ;
97
91
describe ( 'no network failure with optimizely project id' , ( ) => {
98
- beforeAll ( ( ) => {
92
+ beforeEach ( ( ) => {
93
+ jest . clearAllMocks ( ) ;
99
94
reduxHooks . useRequestIsFailed . mockReturnValue ( false ) ;
100
95
getConfig . mockReturnValue ( { OPTIMIZELY_PROJECT_ID : 'fakeId' } ) ;
101
- el = shallow ( < App /> ) ;
96
+ render ( < IntlProvider locale = "en" > < App /> </ IntlProvider > ) ;
102
97
} ) ;
103
98
runBasicTests ( ) ;
104
99
it ( 'loads dashboard' , ( ) => {
105
- const main = el . instance . findByType ( 'main' ) [ 0 ] ;
106
- expect ( main . children . length ) . toEqual ( 1 ) ;
107
- const dashboard = main . children [ 0 ] . el ;
108
- expect ( dashboard . type ) . toEqual ( 'Dashboard' ) ;
109
- expect ( dashboard ) . toEqual ( shallow ( < Dashboard /> ) ) ;
100
+ const dashboard = screen . getByText ( 'Dashboard' ) ;
101
+ expect ( dashboard ) . toBeInTheDocument ( ) ;
110
102
} ) ;
111
103
} ) ;
112
104
describe ( 'initialize failure' , ( ) => {
113
- beforeAll ( ( ) => {
105
+ beforeEach ( ( ) => {
106
+ jest . clearAllMocks ( ) ;
114
107
reduxHooks . useRequestIsFailed . mockImplementation ( ( key ) => key === RequestKeys . initialize ) ;
115
108
getConfig . mockReturnValue ( { } ) ;
116
- el = shallow ( < App /> ) ;
109
+ render ( < IntlProvider locale = "en" messages = { messages } > < App /> </ IntlProvider > ) ;
117
110
} ) ;
118
111
runBasicTests ( ) ;
119
112
it ( 'loads error page' , ( ) => {
120
- const main = el . instance . findByType ( 'main' ) [ 0 ] ;
121
- expect ( main . children . length ) . toEqual ( 1 ) ;
122
- const alert = main . children [ 0 ] ;
123
- expect ( alert . type ) . toEqual ( 'Alert' ) ;
124
- expect ( alert . children . length ) . toEqual ( 1 ) ;
125
- const errorPage = alert . children [ 0 ] ;
126
- expect ( errorPage . type ) . toEqual ( 'ErrorPage' ) ;
127
- expect ( errorPage . props . message ) . toEqual ( formatMessage ( messages . errorMessage , { supportEmail } ) ) ;
113
+ const alert = screen . getByRole ( 'alert' ) ;
114
+ expect ( alert ) . toBeInTheDocument ( ) ;
115
+ const errorPage = screen . getByText ( 'ErrorPage' ) ;
116
+ expect ( errorPage ) . toBeInTheDocument ( ) ;
128
117
} ) ;
129
118
} ) ;
130
119
describe ( 'refresh failure' , ( ) => {
131
- beforeAll ( ( ) => {
120
+ beforeEach ( ( ) => {
132
121
reduxHooks . useRequestIsFailed . mockImplementation ( ( key ) => key === RequestKeys . refreshList ) ;
133
122
getConfig . mockReturnValue ( { } ) ;
134
- el = shallow ( < App /> ) ;
123
+ render ( < IntlProvider locale = "en" > < App /> </ IntlProvider > ) ;
135
124
} ) ;
136
125
runBasicTests ( ) ;
137
126
it ( 'loads error page' , ( ) => {
138
- const main = el . instance . findByType ( 'main' ) [ 0 ] ;
139
- expect ( main . children . length ) . toEqual ( 1 ) ;
140
- const alert = main . children [ 0 ] ;
141
- expect ( alert . type ) . toEqual ( 'Alert' ) ;
142
- expect ( alert . children . length ) . toEqual ( 1 ) ;
143
- const errorPage = alert . children [ 0 ] ;
144
- expect ( errorPage . type ) . toEqual ( 'ErrorPage' ) ;
145
- expect ( errorPage . props . message ) . toEqual ( formatMessage ( messages . errorMessage , { supportEmail } ) ) ;
127
+ const alert = screen . getByRole ( 'alert' ) ;
128
+ expect ( alert ) . toBeInTheDocument ( ) ;
129
+ const errorPage = screen . getByText ( 'ErrorPage' ) ;
130
+ expect ( errorPage ) . toBeInTheDocument ( ) ;
146
131
} ) ;
147
132
} ) ;
148
133
} ) ;
0 commit comments