Skip to content

Commit 724603b

Browse files
authored
Merge pull request code-dot-org#26711 from code-dot-org/remove-react-pairing-test
Remove react-test-utils in PairingTest.js
2 parents 163dea5 + 0e7282a commit 724603b

File tree

1 file changed

+85
-145
lines changed

1 file changed

+85
-145
lines changed
Lines changed: 85 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,39 @@
11
import React from 'react';
2-
import ReactDOM from 'react-dom';
3-
import TestUtils from 'react-addons-test-utils';
4-
import assert from 'assert';
52
import sinon from 'sinon';
3+
import {mount} from 'enzyme';
4+
import {expect} from '../../../../util/configuredChai';
65

76
import Pairing from '@cdo/apps/code-studio/components/pairing/Pairing.jsx';
87

98
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'}));
3611
}
3712

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', [
4216
200, {"Content-Type": "application/json"}, JSON.stringify(response)
4317
]);
18+
return server;
4419
}
4520

46-
function teardownFakeAjax() {
21+
function teardownFakeAjax(server) {
4722
server.restore();
4823
}
4924

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+
5034
describe('for student in multiple sections', function () {
51-
var ajaxUrl = '/pairings';
35+
var component;
36+
var server;
5237
var ajaxState = {
5338
sections: [{
5439
id: 1,
@@ -60,149 +45,119 @@ describe('Pairing component', function () {
6045
};
6146

6247
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();
6851
server.respond();
6952
});
7053

7154
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;
8657
});
8758

8859
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+
8962
// 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);
9366

9467
// 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);
9871
});
9972
});
10073

10174
describe('before ajax response is received', function () {
75+
var component;
10276
beforeEach(function () {
103-
div = document.createElement("div");
104-
component = ReactDOM.render(React.createElement(Pairing, {}), div);
77+
component = mount(React.createElement(Pairing, {}));
78+
component.update();
10579
});
10680

10781
afterEach(function () {
108-
if (div) {
109-
ReactDOM.unmountComponentAtNode(div);
110-
component = null;
111-
}
82+
component = null;
11283
});
11384

11485
it('should not render a section dropdown', function () {
115-
assert.equal(0, TestUtils.scryRenderedDOMComponentsWithTag(component, 'select').length);
86+
verifyStartingValues(component);
11687
});
11788
});
11889

11990
describe('for student in one section', function () {
120-
var ajaxUrl = '/pairings';
91+
var component;
92+
var server;
12193
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"}]}],
12398
pairings: []
12499
};
125100

126101
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();
132105
server.respond();
133106
});
134107

135108
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;
146111
});
147112

113+
it('should recall two students are selected when two students are clicked', function () {
114+
verifyStartingValues(component, 2);
148115

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);
152122
});
153123

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);
158126

159127
// 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);
176132

177133
// 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);
182138
});
183139

184140
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);
188142

189143
// 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);
194148

195149
// click on Add Partner to confirm
196-
TestUtils.Simulate.click(addPartnersButton());
150+
component.find('.addPartners').first().simulate('click');
197151

198152
// verify that the right data is sent to the server
199153
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);
201155
});
202156
});
203157

204158
describe('for student who is currently pairing', function () {
205-
var ajaxUrl = '/pairings';
159+
var component;
160+
var server;
206161
var ajaxState = {
207162
sections: [{
208163
id: 1,
@@ -216,39 +171,24 @@ describe('Pairing component', function () {
216171
};
217172

218173
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();
224177
server.respond();
225178
});
226179

227180
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;
242183
});
243184

244185
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);
246187

247188
// 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);
252192
});
253193
});
254194
});

0 commit comments

Comments
 (0)