1
1
import React from 'react' ;
2
- import assert from 'assert' ;
3
2
import sinon from 'sinon' ;
4
3
import { mount } from 'enzyme' ;
5
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 component ;
11
- var server ;
12
-
13
- function createDomElement ( ajaxUrl ) {
14
- component = mount ( React . createElement ( Pairing , { source : ajaxUrl } ) ) ;
9
+ function createDomElement ( ) {
10
+ return mount ( React . createElement ( Pairing , { source : '/pairings' } ) ) ;
15
11
}
16
12
17
- function setupFakeAjax ( url , response ) {
18
- server = sinon . fakeServer . create ( ) ;
19
-
20
- server . respondWith ( "GET" , url , [
13
+ function setupFakeAjax ( response ) {
14
+ var server = sinon . fakeServer . create ( ) ;
15
+ server . respondWith ( "GET" , '/pairings' , [
21
16
200 , { "Content-Type" : "application/json" } , JSON . stringify ( response )
22
17
] ) ;
18
+ return server ;
23
19
}
24
20
25
- function teardownFakeAjax ( ) {
21
+ function teardownFakeAjax ( server ) {
26
22
server . restore ( ) ;
27
23
}
28
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
+
29
34
describe ( 'for student in multiple sections' , function ( ) {
30
- var ajaxUrl = '/pairings' ;
35
+ var component ;
36
+ var server ;
31
37
var ajaxState = {
32
38
sections : [ {
33
39
id : 1 ,
@@ -39,26 +45,20 @@ describe('Pairing component', function () {
39
45
} ;
40
46
41
47
beforeEach ( function ( ) {
42
- setupFakeAjax ( ajaxUrl , ajaxState ) ;
43
- createDomElement ( ajaxUrl ) ;
48
+ server = setupFakeAjax ( ajaxState ) ;
49
+ component = createDomElement ( ) ;
44
50
component . update ( ) ;
45
51
server . respond ( ) ;
46
52
} ) ;
47
53
48
54
afterEach ( function ( ) {
49
- teardownFakeAjax ( ) ;
55
+ teardownFakeAjax ( server ) ;
50
56
component = null ;
51
57
} ) ;
52
58
53
- it ( 'should render a section dropdown' , function ( ) {
54
- expect ( component . find ( 'select' ) . length ) . to . equal ( 1 ) ;
55
- } ) ;
56
-
57
- it ( 'should not render a list of students' , function ( ) {
58
- expect ( component . find ( '.student' ) . length ) . to . equal ( 0 ) ;
59
- } ) ;
60
-
61
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
+
62
62
// choose first section
63
63
component . find ( 'select' ) . simulate ( 'change' , { target : { value : '1' } } ) ;
64
64
expect ( component . find ( 'select' ) . props ( ) . value ) . to . equal ( 1 ) ;
@@ -72,6 +72,7 @@ describe('Pairing component', function () {
72
72
} ) ;
73
73
74
74
describe ( 'before ajax response is received' , function ( ) {
75
+ var component ;
75
76
beforeEach ( function ( ) {
76
77
component = mount ( React . createElement ( Pairing , { } ) ) ;
77
78
component . update ( ) ;
@@ -82,58 +83,49 @@ describe('Pairing component', function () {
82
83
} ) ;
83
84
84
85
it ( 'should not render a section dropdown' , function ( ) {
85
- expect ( component . find ( '.stop' ) . length ) . to . equal ( 0 ) ;
86
- expect ( component . find ( 'select' ) . length ) . to . equal ( 0 ) ;
86
+ verifyStartingValues ( component ) ;
87
87
} ) ;
88
88
} ) ;
89
89
90
90
describe ( 'for student in one section' , function ( ) {
91
- var ajaxUrl = '/pairings' ;
91
+ var component ;
92
+ var server ;
92
93
var ajaxState = {
93
- 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" } ] } ] ,
94
98
pairings : [ ]
95
99
} ;
96
100
97
101
beforeEach ( function ( ) {
98
- setupFakeAjax ( ajaxUrl , ajaxState ) ;
99
- createDomElement ( ajaxUrl ) ;
102
+ server = setupFakeAjax ( ajaxState ) ;
103
+ component = createDomElement ( ) ;
100
104
component . update ( ) ;
101
105
server . respond ( ) ;
102
106
} ) ;
103
107
104
108
afterEach ( function ( ) {
105
- teardownFakeAjax ( ) ;
109
+ teardownFakeAjax ( server ) ;
106
110
component = null ;
107
111
} ) ;
108
112
109
- it ( 'should not render a section dropdown' , function ( ) {
110
- expect ( component . find ( 'select' ) . length ) . to . equal ( 0 ) ;
111
- } ) ;
112
-
113
- it ( 'should render a list of students' , function ( ) {
114
- expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
115
- expect ( component . find ( '.selected' ) . length ) . to . equal ( 0 ) ;
116
- } ) ;
117
-
118
- it ( 'should select a student when clicking on it' , function ( ) {
119
- expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
120
- expect ( component . find ( '.selected' ) . length ) . to . equal ( 0 ) ;
121
- expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 0 ) ;
113
+ it ( 'should recall two students are selected when two students are clicked' , function ( ) {
114
+ verifyStartingValues ( component , 2 ) ;
122
115
123
- // click on first student to select
116
+ // click on both students to select
124
117
component . find ( '.student' ) . first ( ) . simulate ( 'click' ) ;
125
- expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
126
- expect ( component . find ( '.selected' ) . length ) . to . equal ( 1 ) ;
127
- expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 1 ) ;
128
-
129
- // click on second student to select
130
118
component . find ( '.student' ) . last ( ) . simulate ( 'click' ) ;
131
119
expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
132
120
expect ( component . find ( '.selected' ) . length ) . to . equal ( 2 ) ;
133
121
expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 1 ) ;
122
+ } ) ;
134
123
135
- // click on second student again to unselect
136
- component . find ( '.student' ) . last ( ) . simulate ( 'click' ) ;
124
+ it ( 'should stop displaying addPartners when student is unclicked' , function ( ) {
125
+ verifyStartingValues ( component , 2 ) ;
126
+
127
+ // click on first student to select
128
+ component . find ( '.student' ) . first ( ) . simulate ( 'click' ) ;
137
129
expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
138
130
expect ( component . find ( '.selected' ) . length ) . to . equal ( 1 ) ;
139
131
expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 1 ) ;
@@ -146,9 +138,7 @@ describe('Pairing component', function () {
146
138
} ) ;
147
139
148
140
it ( 'should let you select a student and add them as a partner' , function ( ) {
149
- expect ( component . find ( '.student' ) . length ) . to . equal ( 2 ) ;
150
- expect ( component . find ( '.selected' ) . length ) . to . equal ( 0 ) ;
151
- expect ( component . find ( '.addPartners' ) . length ) . to . equal ( 0 ) ;
141
+ verifyStartingValues ( component , 2 ) ;
152
142
153
143
// click on first student to select
154
144
component . find ( '.student' ) . first ( ) . simulate ( 'click' ) ;
@@ -161,12 +151,13 @@ describe('Pairing component', function () {
161
151
162
152
// verify that the right data is sent to the server
163
153
let data = server . requests [ server . requests . length - 1 ] . requestBody ;
164
- assert . equal ( '{"pairings":[{"id":11,"name":"First student"}]}' , data ) ;
154
+ expect ( '{"pairings":[{"id":11,"name":"First student"}]}' ) . to . equal ( data ) ;
165
155
} ) ;
166
156
} ) ;
167
157
168
158
describe ( 'for student who is currently pairing' , function ( ) {
169
- var ajaxUrl = '/pairings' ;
159
+ var component ;
160
+ var server ;
170
161
var ajaxState = {
171
162
sections : [ {
172
163
id : 1 ,
@@ -180,28 +171,19 @@ describe('Pairing component', function () {
180
171
} ;
181
172
182
173
beforeEach ( function ( ) {
183
- setupFakeAjax ( ajaxUrl , ajaxState ) ;
184
- createDomElement ( ajaxUrl ) ;
174
+ server = setupFakeAjax ( ajaxState ) ;
175
+ component = createDomElement ( ) ;
185
176
component . update ( ) ;
186
177
server . respond ( ) ;
187
178
} ) ;
188
179
189
180
afterEach ( function ( ) {
190
- teardownFakeAjax ( ) ;
181
+ teardownFakeAjax ( server ) ;
191
182
component = null ;
192
183
} ) ;
193
184
194
- it ( 'should not render a section dropdown' , function ( ) {
195
- expect ( component . find ( 'select' ) . length ) . to . equal ( 0 ) ;
196
- expect ( component . find ( 'Pairing' ) . length ) . to . equal ( 1 ) ;
197
- } ) ;
198
-
199
- it ( 'should render a list of students' , function ( ) {
200
- expect ( component . find ( '.student' ) . length ) . to . equal ( 3 ) ;
201
- } ) ;
202
-
203
185
it ( 'should remove all students and go back to selection mode when clicking Stop' , function ( ) {
204
- expect ( component . find ( '.student' ) . length ) . to . equal ( 3 ) ;
186
+ verifyStartingValues ( component , 3 , 0 , 1 ) ;
205
187
206
188
// click on stop button
207
189
component . find ( '.stop' ) . simulate ( 'click' ) ;
0 commit comments