1
1
import React from 'react' ;
2
- import ReactDOM from 'react-dom' ;
3
- import TestUtils from 'react-addons-test-utils' ;
4
- import assert from 'assert' ;
5
2
import sinon from 'sinon' ;
3
+ import { mount } from 'enzyme' ;
4
+ import { expect } from '../../../../util/configuredChai' ;
6
5
7
6
import Pairing from '@cdo/apps/code-studio/components/pairing/Pairing.jsx' ;
8
7
9
8
describe ( 'Pairing component' , function ( ) {
10
- var div ;
11
- var component ;
12
- var server ;
13
-
14
- function render ( ajaxUrl ) {
15
- component = ReactDOM . render ( React . createElement ( Pairing , { source : ajaxUrl } ) , div ) ;
16
- }
17
-
18
- function numberOfStudents ( ) {
19
- return TestUtils . scryRenderedDOMComponentsWithClass ( component , 'student' ) . length ;
20
- }
21
-
22
- function numberOfSelectedStudents ( ) {
23
- return TestUtils . scryRenderedDOMComponentsWithClass ( component , 'selected' ) . length ;
24
- }
25
-
26
- function sectionSelect ( ) {
27
- return TestUtils . findRenderedDOMComponentWithTag ( component , 'select' ) ;
28
- }
29
-
30
- function addPartnersButtonRendered ( ) {
31
- return TestUtils . scryRenderedDOMComponentsWithClass ( component , 'addPartners' ) . length ;
32
- }
33
-
34
- function addPartnersButton ( ) {
35
- return TestUtils . findRenderedDOMComponentWithClass ( component , 'addPartners' ) ;
9
+ function createDomElement ( ) {
10
+ return mount ( React . createElement ( Pairing , { source : '/pairings' } ) ) ;
36
11
}
37
12
38
- function setupFakeAjax ( url , response ) {
39
- server = sinon . fakeServer . create ( ) ;
40
-
41
- server . respondWith ( "GET" , url , [
13
+ function setupFakeAjax ( response ) {
14
+ var server = sinon . fakeServer . create ( ) ;
15
+ server . respondWith ( "GET" , '/pairings' , [
42
16
200 , { "Content-Type" : "application/json" } , JSON . stringify ( response )
43
17
] ) ;
18
+ return server ;
44
19
}
45
20
46
- function teardownFakeAjax ( ) {
21
+ function teardownFakeAjax ( server ) {
47
22
server . restore ( ) ;
48
23
}
49
24
25
+ function verifyStartingValues ( component , student = 0 , select = 0 , stop = 0 ) {
26
+ expect ( component . find ( 'Pairing' ) . length ) . to . equal ( 1 ) ;
27
+ expect ( component . find ( '.selected' ) . length ) . to . equal ( 0 ) ;
28
+ expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 0 ) ;
29
+ expect ( component . find ( '.student' ) . length ) . to . equal ( student ) ;
30
+ expect ( component . find ( 'select' ) . length ) . to . equal ( select ) ;
31
+ expect ( component . find ( '.stop' ) . length ) . to . equal ( stop ) ;
32
+ }
33
+
50
34
describe ( 'for student in multiple sections' , function ( ) {
51
- var ajaxUrl = '/pairings' ;
35
+ var component ;
36
+ var server ;
52
37
var ajaxState = {
53
38
sections : [ {
54
39
id : 1 ,
@@ -60,149 +45,119 @@ describe('Pairing component', function () {
60
45
} ;
61
46
62
47
beforeEach ( function ( ) {
63
- div = document . createElement ( "div" ) ;
64
-
65
- setupFakeAjax ( ajaxUrl , ajaxState ) ;
66
-
67
- render ( ajaxUrl ) ;
48
+ server = setupFakeAjax ( ajaxState ) ;
49
+ component = createDomElement ( ) ;
50
+ component . update ( ) ;
68
51
server . respond ( ) ;
69
52
} ) ;
70
53
71
54
afterEach ( function ( ) {
72
- teardownFakeAjax ( ) ;
73
-
74
- if ( div ) {
75
- ReactDOM . unmountComponentAtNode ( div ) ;
76
- component = null ;
77
- }
78
- } ) ;
79
-
80
- it ( 'should render a section dropdown' , function ( ) {
81
- assert ( sectionSelect ( ) ) ;
82
- } ) ;
83
-
84
- it ( 'should not render a list of students' , function ( ) {
85
- assert . equal ( 0 , numberOfStudents ( ) ) ;
55
+ teardownFakeAjax ( server ) ;
56
+ component = null ;
86
57
} ) ;
87
58
88
59
it ( 'should change the section and render a list of students when a section with students is selected' , function ( ) {
60
+ verifyStartingValues ( component , 0 , 1 ) ;
61
+
89
62
// choose first section
90
- TestUtils . Simulate . change ( sectionSelect ( ) , { target : { value : "1" } } ) ;
91
- assert . equal ( "1" , sectionSelect ( ) . value ) ;
92
- assert . equal ( 2 , numberOfStudents ( ) ) ;
63
+ component . find ( 'select' ) . simulate ( 'change' , { target : { value : '1' } } ) ;
64
+ expect ( component . find ( 'select' ) . props ( ) . value ) . to . equal ( 1 ) ;
65
+ expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
93
66
94
67
// choose second section
95
- TestUtils . Simulate . change ( sectionSelect ( ) , { target : { value : "15" } } ) ;
96
- assert . equal ( "15" , sectionSelect ( ) . value ) ;
97
- assert . equal ( 0 , numberOfStudents ( ) ) ;
68
+ component . find ( 'select' ) . simulate ( 'change' , { target : { value : '15' } } ) ;
69
+ expect ( component . find ( 'select' ) . props ( ) . value ) . to . equal ( 15 ) ;
70
+ expect ( component . find ( '.student' ) . length ) . to . equal ( 0 ) ;
98
71
} ) ;
99
72
} ) ;
100
73
101
74
describe ( 'before ajax response is received' , function ( ) {
75
+ var component ;
102
76
beforeEach ( function ( ) {
103
- div = document . createElement ( "div" ) ;
104
- component = ReactDOM . render ( React . createElement ( Pairing , { } ) , div ) ;
77
+ component = mount ( React . createElement ( Pairing , { } ) ) ;
78
+ component . update ( ) ;
105
79
} ) ;
106
80
107
81
afterEach ( function ( ) {
108
- if ( div ) {
109
- ReactDOM . unmountComponentAtNode ( div ) ;
110
- component = null ;
111
- }
82
+ component = null ;
112
83
} ) ;
113
84
114
85
it ( 'should not render a section dropdown' , function ( ) {
115
- assert . equal ( 0 , TestUtils . scryRenderedDOMComponentsWithTag ( component , 'select' ) . length ) ;
86
+ verifyStartingValues ( component ) ;
116
87
} ) ;
117
88
} ) ;
118
89
119
90
describe ( 'for student in one section' , function ( ) {
120
- var ajaxUrl = '/pairings' ;
91
+ var component ;
92
+ var server ;
121
93
var ajaxState = {
122
- sections : [ { id : 1 , name : "A section" , students : [ { id : 11 , name : "First student" } , { id : 12 , name : "Second Student" } ] } ] ,
94
+ sections : [ {
95
+ id : 1 ,
96
+ name : "A section" ,
97
+ students : [ { id : 11 , name : "First student" } , { id : 12 , name : "Second Student" } ] } ] ,
123
98
pairings : [ ]
124
99
} ;
125
100
126
101
beforeEach ( function ( ) {
127
- div = document . createElement ( "div" ) ;
128
-
129
- setupFakeAjax ( ajaxUrl , ajaxState ) ;
130
-
131
- render ( ajaxUrl ) ;
102
+ server = setupFakeAjax ( ajaxState ) ;
103
+ component = createDomElement ( ) ;
104
+ component . update ( ) ;
132
105
server . respond ( ) ;
133
106
} ) ;
134
107
135
108
afterEach ( function ( ) {
136
- teardownFakeAjax ( ) ;
137
-
138
- if ( div ) {
139
- ReactDOM . unmountComponentAtNode ( div ) ;
140
- component = null ;
141
- }
142
- } ) ;
143
-
144
- it ( 'should not render a section dropdown' , function ( ) {
145
- assert . equal ( 0 , TestUtils . scryRenderedDOMComponentsWithTag ( component , 'select' ) . length ) ;
109
+ teardownFakeAjax ( server ) ;
110
+ component = null ;
146
111
} ) ;
147
112
113
+ it ( 'should recall two students are selected when two students are clicked' , function ( ) {
114
+ verifyStartingValues ( component , 2 ) ;
148
115
149
- it ( 'should render a list of students' , function ( ) {
150
- assert . equal ( 2 , numberOfStudents ( ) ) ;
151
- assert . equal ( 0 , numberOfSelectedStudents ( ) ) ;
116
+ // click on both students to select
117
+ component . find ( '.student' ) . first ( ) . simulate ( 'click' ) ;
118
+ component . find ( '.student' ) . last ( ) . simulate ( 'click' ) ;
119
+ expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
120
+ expect ( component . find ( '.selected' ) . length ) . to . equal ( 2 ) ;
121
+ expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 1 ) ;
152
122
} ) ;
153
123
154
- it ( 'should select a student when clicking on it' , function ( ) {
155
- assert . equal ( 2 , numberOfStudents ( ) ) ;
156
- assert . equal ( 0 , numberOfSelectedStudents ( ) ) ;
157
- assert ( ! addPartnersButtonRendered ( ) ) ;
124
+ it ( 'should stop displaying addPartners when student is unclicked' , function ( ) {
125
+ verifyStartingValues ( component , 2 ) ;
158
126
159
127
// click on first student to select
160
- TestUtils . Simulate . click ( TestUtils . scryRenderedDOMComponentsWithClass ( component , 'student' ) [ 0 ] ) ;
161
- assert . equal ( 2 , numberOfStudents ( ) ) ;
162
- assert . equal ( 1 , numberOfSelectedStudents ( ) ) ;
163
- assert ( addPartnersButtonRendered ( ) ) ;
164
-
165
- // click on second student to select
166
- TestUtils . Simulate . click ( TestUtils . scryRenderedDOMComponentsWithClass ( component , 'student' ) [ 1 ] ) ;
167
- assert . equal ( 2 , numberOfStudents ( ) ) ;
168
- assert . equal ( 2 , numberOfSelectedStudents ( ) ) ;
169
- assert ( addPartnersButtonRendered ( ) ) ;
170
-
171
- // click on second student again to unselect
172
- TestUtils . Simulate . click ( TestUtils . scryRenderedDOMComponentsWithClass ( component , 'student' ) [ 1 ] ) ;
173
- assert . equal ( 2 , numberOfStudents ( ) ) ;
174
- assert . equal ( 1 , numberOfSelectedStudents ( ) ) ;
175
- assert ( addPartnersButtonRendered ( ) ) ;
128
+ component . find ( '.student' ) . first ( ) . simulate ( 'click' ) ;
129
+ expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
130
+ expect ( component . find ( '.selected' ) . length ) . to . equal ( 1 ) ;
131
+ expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 1 ) ;
176
132
177
133
// click on first student again to unselect
178
- TestUtils . Simulate . click ( TestUtils . scryRenderedDOMComponentsWithClass ( component , 'student' ) [ 0 ] ) ;
179
- assert . equal ( 2 , numberOfStudents ( ) ) ;
180
- assert . equal ( 0 , numberOfSelectedStudents ( ) ) ;
181
- assert ( ! addPartnersButtonRendered ( ) ) ;
134
+ component . find ( '.student' ) . first ( ) . simulate ( 'click' ) ;
135
+ expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
136
+ expect ( component . find ( '.selected' ) . length ) . to . equal ( 0 ) ;
137
+ expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 0 ) ;
182
138
} ) ;
183
139
184
140
it ( 'should let you select a student and add them as a partner' , function ( ) {
185
- assert . equal ( 2 , numberOfStudents ( ) ) ;
186
- assert . equal ( 0 , numberOfSelectedStudents ( ) ) ;
187
- assert ( ! addPartnersButtonRendered ( ) ) ;
141
+ verifyStartingValues ( component , 2 ) ;
188
142
189
143
// click on first student to select
190
- TestUtils . Simulate . click ( TestUtils . scryRenderedDOMComponentsWithClass ( component , 'student' ) [ 0 ] ) ;
191
- assert . equal ( 2 , numberOfStudents ( ) ) ;
192
- assert . equal ( 1 , numberOfSelectedStudents ( ) ) ;
193
- assert ( addPartnersButtonRendered ( ) ) ;
144
+ component . find ( '.student' ) . first ( ) . simulate ( 'click' ) ;
145
+ expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
146
+ expect ( component . find ( '.selected' ) . length ) . to . equal ( 1 ) ;
147
+ expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 1 ) ;
194
148
195
149
// click on Add Partner to confirm
196
- TestUtils . Simulate . click ( addPartnersButton ( ) ) ;
150
+ component . find ( '.addPartners' ) . first ( ) . simulate ( 'click' ) ;
197
151
198
152
// verify that the right data is sent to the server
199
153
let data = server . requests [ server . requests . length - 1 ] . requestBody ;
200
- assert . equal ( '{"pairings":[{"id":11,"name":"First student"}]}' , data ) ;
154
+ expect ( '{"pairings":[{"id":11,"name":"First student"}]}' ) . to . equal ( data ) ;
201
155
} ) ;
202
156
} ) ;
203
157
204
158
describe ( 'for student who is currently pairing' , function ( ) {
205
- var ajaxUrl = '/pairings' ;
159
+ var component ;
160
+ var server ;
206
161
var ajaxState = {
207
162
sections : [ {
208
163
id : 1 ,
@@ -216,39 +171,24 @@ describe('Pairing component', function () {
216
171
} ;
217
172
218
173
beforeEach ( function ( ) {
219
- div = document . createElement ( "div" ) ;
220
-
221
- setupFakeAjax ( ajaxUrl , ajaxState ) ;
222
-
223
- render ( ajaxUrl ) ;
174
+ server = setupFakeAjax ( ajaxState ) ;
175
+ component = createDomElement ( ) ;
176
+ component . update ( ) ;
224
177
server . respond ( ) ;
225
178
} ) ;
226
179
227
180
afterEach ( function ( ) {
228
- teardownFakeAjax ( ) ;
229
-
230
- if ( div ) {
231
- ReactDOM . unmountComponentAtNode ( div ) ;
232
- component = null ;
233
- }
234
- } ) ;
235
-
236
- it ( 'should not render a section dropdown' , function ( ) {
237
- assert . equal ( 0 , TestUtils . scryRenderedDOMComponentsWithTag ( component , 'select' ) . length ) ;
238
- } ) ;
239
-
240
- it ( 'should render a list of students' , function ( ) {
241
- assert . equal ( 3 , numberOfStudents ( ) ) ;
181
+ teardownFakeAjax ( server ) ;
182
+ component = null ;
242
183
} ) ;
243
184
244
185
it ( 'should remove all students and go back to selection mode when clicking Stop' , function ( ) {
245
- assert . equal ( 3 , numberOfStudents ( ) ) ;
186
+ verifyStartingValues ( component , 3 , 0 , 1 ) ;
246
187
247
188
// click on stop button
248
- TestUtils . Simulate . click ( TestUtils . findRenderedDOMComponentWithClass ( component , 'stop' ) ) ;
249
-
250
- assert . equal ( 0 , numberOfStudents ( ) ) ;
251
- assert ( TestUtils . findRenderedDOMComponentWithTag ( component , 'select' ) ) ;
189
+ component . find ( '.stop' ) . simulate ( 'click' ) ;
190
+ expect ( component . find ( '.student' ) . length ) . to . equal ( 0 ) ;
191
+ expect ( component . find ( 'select' ) . length ) . to . equal ( 1 ) ;
252
192
} ) ;
253
193
} ) ;
254
194
} ) ;
0 commit comments