Skip to content

Commit 0e7282a

Browse files
committed
Refactored unit test to remove global variables, remove duplicate tests, and break apart large tests.
1 parent b4c9974 commit 0e7282a

File tree

1 file changed

+51
-69
lines changed

1 file changed

+51
-69
lines changed

apps/test/unit/code-studio/components/pairing/PairingTest.js

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
import React from 'react';
2-
import assert from 'assert';
32
import sinon from 'sinon';
43
import {mount} from 'enzyme';
54
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 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'}));
1511
}
1612

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

25-
function teardownFakeAjax() {
21+
function teardownFakeAjax(server) {
2622
server.restore();
2723
}
2824

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+
2934
describe('for student in multiple sections', function () {
30-
var ajaxUrl = '/pairings';
35+
var component;
36+
var server;
3137
var ajaxState = {
3238
sections: [{
3339
id: 1,
@@ -39,26 +45,20 @@ describe('Pairing component', function () {
3945
};
4046

4147
beforeEach(function () {
42-
setupFakeAjax(ajaxUrl, ajaxState);
43-
createDomElement(ajaxUrl);
48+
server = setupFakeAjax(ajaxState);
49+
component = createDomElement();
4450
component.update();
4551
server.respond();
4652
});
4753

4854
afterEach(function () {
49-
teardownFakeAjax();
55+
teardownFakeAjax(server);
5056
component = null;
5157
});
5258

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-
6159
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+
6262
// choose first section
6363
component.find('select').simulate('change', {target: {value: '1'}});
6464
expect(component.find('select').props().value).to.equal(1);
@@ -72,6 +72,7 @@ describe('Pairing component', function () {
7272
});
7373

7474
describe('before ajax response is received', function () {
75+
var component;
7576
beforeEach(function () {
7677
component = mount(React.createElement(Pairing, {}));
7778
component.update();
@@ -82,58 +83,49 @@ describe('Pairing component', function () {
8283
});
8384

8485
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);
8787
});
8888
});
8989

9090
describe('for student in one section', function () {
91-
var ajaxUrl = '/pairings';
91+
var component;
92+
var server;
9293
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"}]}],
9498
pairings: []
9599
};
96100

97101
beforeEach(function () {
98-
setupFakeAjax(ajaxUrl, ajaxState);
99-
createDomElement(ajaxUrl);
102+
server = setupFakeAjax(ajaxState);
103+
component = createDomElement();
100104
component.update();
101105
server.respond();
102106
});
103107

104108
afterEach(function () {
105-
teardownFakeAjax();
109+
teardownFakeAjax(server);
106110
component = null;
107111
});
108112

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

123-
// click on first student to select
116+
// click on both students to select
124117
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
130118
component.find('.student').last().simulate('click');
131119
expect(component.find('.student').length).to.equal(2);
132120
expect(component.find('.selected').length).to.equal(2);
133121
expect(component.find('.addPartners').length).to.equal(1);
122+
});
134123

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');
137129
expect(component.find('.student').length).to.equal(2);
138130
expect(component.find('.selected').length).to.equal(1);
139131
expect(component.find('.addPartners').length).to.equal(1);
@@ -146,9 +138,7 @@ describe('Pairing component', function () {
146138
});
147139

148140
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);
152142

153143
// click on first student to select
154144
component.find('.student').first().simulate('click');
@@ -161,12 +151,13 @@ describe('Pairing component', function () {
161151

162152
// verify that the right data is sent to the server
163153
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);
165155
});
166156
});
167157

168158
describe('for student who is currently pairing', function () {
169-
var ajaxUrl = '/pairings';
159+
var component;
160+
var server;
170161
var ajaxState = {
171162
sections: [{
172163
id: 1,
@@ -180,28 +171,19 @@ describe('Pairing component', function () {
180171
};
181172

182173
beforeEach(function () {
183-
setupFakeAjax(ajaxUrl, ajaxState);
184-
createDomElement(ajaxUrl);
174+
server = setupFakeAjax(ajaxState);
175+
component = createDomElement();
185176
component.update();
186177
server.respond();
187178
});
188179

189180
afterEach(function () {
190-
teardownFakeAjax();
181+
teardownFakeAjax(server);
191182
component = null;
192183
});
193184

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-
203185
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);
205187

206188
// click on stop button
207189
component.find('.stop').simulate('click');

0 commit comments

Comments
 (0)