1
1
import React from 'react' ;
2
- import { render } from '@testing-library/react' ;
3
2
import { when } from 'jest-when' ;
4
- import { MemoryRouter , useParams } from 'react-router-dom' ;
5
- import { Provider } from 'react-redux' ;
6
-
7
- import { QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
3
+ import { useParams } from 'react-router-dom' ;
8
4
9
5
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth' ;
10
- import { IntlProvider } from '@edx/frontend-platform/i18n' ;
11
6
import { getConfig } from '@edx/frontend-platform' ;
12
7
13
- import App from 'App' ;
14
-
15
8
import fakeData from 'data/services/lms/fakeData' ;
16
9
import { loadState } from 'data/services/lms/fakeData/dataStates' ;
17
10
import { paths } from 'data/services/lms/urls' ;
18
11
19
12
import { stepNames , stepRoutes } from 'constants/index' ;
20
- import { progressKeys } from 'constants/mockData' ;
21
13
22
- import { createStore } from 'data/store' ;
14
+ import {
15
+ courseId ,
16
+ xblockId ,
17
+ baseUrl ,
18
+ config ,
19
+ getProgressKeys ,
20
+ stepOrders ,
21
+ } from './constants' ;
22
+ import {
23
+ mockQuerySelector ,
24
+ post ,
25
+ pageDataUrl ,
26
+ loadApp ,
27
+ mockPageData ,
28
+ } from './utils' ;
23
29
24
30
jest . mock ( '@edx/frontend-platform/auth' , ( ) => ( {
25
31
...jest . requireActual ( '@edx/frontend-platform/auth' ) ,
@@ -42,137 +48,65 @@ jest.mock('axios', () => ({
42
48
...jest . requireActual ( 'axios' ) ,
43
49
get : jest . fn ( ) . mockResolvedValue ( { data : 'fake file data' } ) ,
44
50
} ) ) ;
45
- jest . mock ( 'components/HotjarSurvey' , ( ) => 'HotjarSurvey ' ) ;
51
+ jest . mock ( 'components/HotjarSurvey' , ( ) => 'hot-jar-survey ' ) ;
46
52
47
53
jest . unmock ( 'react' ) ;
48
54
jest . unmock ( '@edx/paragon' ) ;
49
55
50
- jest . spyOn ( document , 'querySelector' ) . mockImplementation ( ( selector ) => {
51
- if ( selector === 'html' ) {
52
- return {
53
- scrollTo : jest . fn ( ) ,
54
- } ;
55
- }
56
- return selector ;
57
- } ) ;
56
+ mockQuerySelector ( ) ;
58
57
59
- const post = jest . fn ( ) ;
60
- const lmsBaseUrl = 'test-base-url' ;
61
- const courseId = 'test-course-id' ;
62
- const xblockId = 'test-xblock-id' ;
63
- const baseUrl = `${ lmsBaseUrl } /courses/${ courseId } /xblock/${ xblockId } /handler` ;
64
- const config = {
65
- LMS_BASE_URL : lmsBaseUrl ,
66
- } ;
67
58
when ( getConfig ) . calledWith ( ) . mockReturnValue ( config ) ;
68
-
69
59
when ( getAuthenticatedHttpClient ) . calledWith ( ) . mockReturnValue ( { post } ) ;
70
60
when ( useParams ) . calledWith ( ) . mockReturnValue ( { courseId, xblockId } ) ;
71
61
72
- const renderApp = ( route ) => {
73
- const store = createStore ( false ) ;
74
- const queryClient = new QueryClient ( {
75
- queries : { retry : false } ,
76
- } ) ;
77
- const location = `/${ route } /${ courseId } /${ xblockId } /` ;
78
- return (
79
- < IntlProvider locale = "en" >
80
- < Provider store = { store } >
81
- < QueryClientProvider client = { queryClient } >
82
- < MemoryRouter initialEntries = { [ location ] } >
83
- < App />
84
- </ MemoryRouter >
85
- </ QueryClientProvider >
86
- </ Provider >
87
- </ IntlProvider >
88
- ) ;
89
- } ;
90
-
91
- const pageDataUrl = ( view = undefined ) => {
92
- const url = `${ baseUrl } /get_learner_data/` ;
93
- return view ? `${ url } ${ view } ` : url ;
94
- } ;
95
-
96
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
97
- const loadApp = async ( progressKey , step , hasSubmitted = false ) => {
98
- const app = renderApp ( stepRoutes [ step ] ) ;
99
- return render ( app ) ;
100
- } ;
101
-
102
- const mockPageData = ( url , { body, response } ) => {
103
- when ( post ) . calledWith ( `${ baseUrl } ${ paths . oraConfig } ` , { } )
104
- . mockResolvedValue ( { data : fakeData . oraConfig . assessmentText } ) ;
105
- if ( body ) {
106
- when ( post ) . calledWith ( url , expect . anything ( ) ) . mockResolvedValue ( { data : response } ) ;
107
- } else {
108
- when ( post ) . calledWith ( url ) . mockResolvedValue ( { data : response } ) ;
109
- }
110
- } ;
111
-
112
62
let el ;
113
63
114
64
describe ( 'Integration smoke tests' , ( ) => {
115
65
beforeEach ( ( ) => {
116
66
jest . clearAllMocks ( ) ;
117
67
} ) ;
118
- const testModalView = ( { step, keys } ) => {
119
- it . each ( keys ) ( 'renders %s progress state' , async ( progressKey ) => {
120
- const state = loadState ( { view : stepRoutes [ step ] , progressKey } ) ;
121
- mockPageData ( pageDataUrl ( step ) , { body : { } , response : state } ) ;
122
- el = await loadApp ( progressKey , step ) ;
123
- await el . findAllByText ( 'Create response' ) ;
124
- } ) ;
125
- } ;
126
- describe ( 'xblock view' , ( ) => {
127
- const xblockProgressKeys = Object . values ( progressKeys ) ;
128
- it . each ( xblockProgressKeys ) ( 'renders %s progress state' , async ( progressKey ) => {
129
- const state = loadState ( {
130
- view : stepNames . xblock ,
131
- progressKey,
68
+ describe . each ( Object . keys ( stepOrders ) ) ( 'For step order %s' , ( stepOrder ) => {
69
+ const oraConfig = { ...fakeData . oraConfig . assessmentText } ;
70
+ oraConfig . assessment_steps . order = stepOrders [ stepOrder ] ;
71
+ when ( post ) . calledWith ( `${ baseUrl } ${ paths . oraConfig } ` , { } )
72
+ . mockResolvedValue ( { data : oraConfig } ) ;
73
+ const testModalView = ( { step } ) => {
74
+ const keys = getProgressKeys ( stepOrder , step ) ;
75
+ if ( keys . length === 0 ) { return ; }
76
+ it . each ( keys ) ( 'renders %s progress state' , async ( progressKey ) => {
77
+ const state = loadState ( { view : stepRoutes [ step ] , progressKey } ) ;
78
+ mockPageData ( pageDataUrl ( step ) , { body : { } , response : state } ) ;
79
+ el = await loadApp ( progressKey , step ) ;
80
+ await el . findAllByText ( 'Create response' ) ;
81
+ } ) ;
82
+ } ;
83
+ describe ( 'xblock view' , ( ) => {
84
+ const keys = getProgressKeys ( stepOrder , stepNames . xblock ) ;
85
+ it . each ( keys ) ( 'renders %s progress state' , async ( progressKey ) => {
86
+ const state = loadState ( {
87
+ view : stepNames . xblock ,
88
+ progressKey,
89
+ } ) ;
90
+ mockPageData ( pageDataUrl ( ) , { body : { } , response : state } ) ;
91
+ el = await loadApp ( progressKey , stepNames . xblock ) ;
92
+ const { title } = fakeData . oraConfig . assessmentText ;
93
+ await el . findByText ( title ) ;
132
94
} ) ;
133
- mockPageData ( pageDataUrl ( ) , { body : { } , response : state } ) ;
134
- el = await loadApp ( progressKey , stepNames . xblock ) ;
135
- await el . findByText ( 'Open Response Assessment' ) ;
136
95
} ) ;
137
- } ) ;
138
- describe ( 'submission view' , ( ) => {
139
- const submissionProgressKeys = [
140
- progressKeys . submissionUnsaved ,
141
- progressKeys . submissionSaved ,
142
- progressKeys . studentTraining ,
143
- progressKeys . studentTrainingPartial ,
144
- progressKeys . selfAssessment ,
145
- progressKeys . peerAssessment ,
146
- progressKeys . peerAssessmentPartial ,
147
- progressKeys . peerAssessmentWaiting ,
148
- progressKeys . staffAfterPeer ,
149
- progressKeys . graded ,
150
- ] ;
151
- testModalView ( { step : stepNames . submission , keys : submissionProgressKeys } ) ;
152
- } ) ;
153
- describe ( 'studentTraining view' , ( ) => {
154
- const trainingProgressKeys = [
155
- progressKeys . studentTraining ,
156
- progressKeys . studentTrainingPartial ,
157
- ] ;
158
- testModalView ( { step : stepNames . studentTraining , keys : trainingProgressKeys } ) ;
159
- } ) ;
160
- describe ( 'self assessment view' , ( ) => {
161
- const selfProgressKeys = [ progressKeys . selfAssessment ] ;
162
- testModalView ( { step : stepNames . self , keys : selfProgressKeys } ) ;
163
- } ) ;
164
- describe ( 'peer assessment view' , ( ) => {
165
- const peerProgressKeys = [
166
- progressKeys . peerAssessment ,
167
- progressKeys . peerAssessmentPartial ,
168
- progressKeys . peerAssessmentWaiting ,
169
- progressKeys . staffAfterPeer ,
170
- progressKeys . graded ,
171
- ] ;
172
- testModalView ( { step : stepNames . peer , keys : peerProgressKeys } ) ;
173
- } ) ;
174
- describe ( 'graded view' , ( ) => {
175
- const gradedProgressKeys = [ progressKeys . graded ] ;
176
- testModalView ( { step : stepNames . done , keys : gradedProgressKeys } ) ;
96
+ describe ( 'submission view' , ( ) => {
97
+ testModalView ( { step : stepNames . submission } ) ;
98
+ } ) ;
99
+ describe ( 'studentTraining view' , ( ) => {
100
+ testModalView ( { step : stepNames . studentTraining } ) ;
101
+ } ) ;
102
+ describe ( 'self assessment view' , ( ) => {
103
+ testModalView ( { step : stepNames . self } ) ;
104
+ } ) ;
105
+ describe ( 'peer assessment view' , ( ) => {
106
+ testModalView ( { step : stepNames . peer } ) ;
107
+ } ) ;
108
+ describe ( 'graded view' , ( ) => {
109
+ testModalView ( { step : stepNames . done } ) ;
110
+ } ) ;
177
111
} ) ;
178
112
} ) ;
0 commit comments