-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
220 lines (206 loc) · 5.6 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// index.test.js
'use strict'
const expect = require('chai').expect;
const ptable = require('./index.js').PeriodicTable;
const Compound = require('./index.js').Compound;
const Utility = require('./index.js').Utility;
describe('PeriodicTable.js', ()=> {
describe('getElement()', ()=> {
it('Returns correct element', ()=> {
var element = ptable.getElement("Li");
expect(element).to.have.property('name', 'lithium');
})
it('Returns null for invalid element', ()=> {
var element = ptable.getElement("Aa");
expect(element).to.be.null;
})
})
describe('getAtomic()', ()=> {
it('Returns correct element', ()=> {
var element = ptable.getAtomic(3);
expect(element).to.have.property('name', 'lithium');
})
it('Returns null for invalid element', ()=> {
var element = ptable.getAtomic(345);
expect(element).to.be.null;
})
})
describe('getGroup()', ()=> {
it('Returns correct group', ()=> {
var group = ptable.getGroup(2);
expect(group.length).equals(6);
})
it('Returns null for invalid group', ()=> {
var group = ptable.getGroup(55);
expect(group).to.be.null;
})
})
describe('getPeriod()', ()=> {
it('Returns correct period', ()=> {
var period = ptable.getPeriod(3);
expect(period.length).equals(8);
})
it('Returns null for invalid period', ()=> {
var period = ptable.getPeriod(96);
expect(period).to.be.null;
})
})
describe('getType()', ()=> {
it('Returns correct type', ()=> {
var type = ptable.getType("halogen");
expect(type.length).equals(6);
})
it('Returns null for invalid type', ()=> {
var type = ptable.getType("halatoot");
expect(type).to.be.null;
})
})
})
describe('Compound.js', ()=> {
describe('new Compound()', ()=> {
it('The new keyword works', ()=> {
let c = new Compound();
expect(c).to.not.be.null;
})
it('Initial mass is 0', ()=> {
let c = new Compound();
expect(c.getMass()).equals(0);
})
})
describe('new Compound(element_list)', ()=> {
const element_list = {
"Li": 3,
"Cl": 1
}
it('The new keyword works', ()=> {
let c = new Compound(element_list);
expect(c).to.not.be.null;
})
it('Correct initial mass', ()=> {
let c = new Compound(element_list);
expect(c.getMass()).equals(56.27)
})
it('Elements are added to compound', ()=> {
let c = new Compound(element_list);
let length = Object.keys(c.elements).length
expect(length).equals(2);
})
})
describe('add()', ()=> {
it('Element is added to compound', ()=> {
let c = new Compound();
var added = c.add("Li");
expect(added).to.be.true;
})
it('Invalid element is not added', ()=> {
let c = new Compound();
var added = c.add("Aa");
expect(added).to.be.false;
})
it('Correct element is added to compound', ()=> {
let c = new Compound();
c.add("Li");
expect(c.elements["Li"]).to.exist;
})
it('Element is added to elementList', ()=> {
let c = new Compound();
c.add("Li");
expect(c.elementsList.length).equals(1);
})
it('Multiple of same element', ()=> {
let c = new Compound();
c.add("Li", 2);
expect(c.elements["Li"]).equals(2)
})
})
describe('remove()', ()=> {
it('Element is removed from compound', ()=> {
let c = new Compound({"Li": 2});
var removed = c.remove("Li");
expect(removed).to.be.true;
})
it('Invalid element is not removed', ()=> {
let c = new Compound({"Li": 2});
var removed = c.remove("Aa");
expect(removed).to.be.false;
})
it('Element at 0 is not removed', ()=> {
let c = new Compound({"Li": 1});
c.remove("Li");
var removed = c.remove("Li");
expect(removed).to.be.false;
})
it('Element removed from compound at 0', ()=> {
let c = new Compound({"Li": 1});
c.remove("Li");
expect(c.elements["Li"]).to.not.exist;
})
it('Correct element is removed from compound', ()=> {
let c = new Compound({"Li": 2});
c.remove("Li");
expect(c.elements["Li"]).equals(1);
})
it('Element is removed from elementList', ()=> {
let c = new Compound();
c.add("Li");
c.remove("Li");
expect(c.elementsList.length).equals(0);
})
})
describe('clear()', ()=> {
it('Resets mass', ()=> {
let c = new Compound({"Li": 2});
c.clear();
expect(c.getMass()).equals(0);
})
it('Resets elements', ()=> {
let c = new Compound({"Li": 2});
c.clear();
let length = Object.keys(c.elements).length
expect(length).equals(0)
})
})
describe('getMass()', ()=> {
it('Returns a number', ()=> {
let c = new Compound({"Li": 3, "Cl": 1});
let mass = c.getMass();
expect(mass).to.be.a('number');
})
it('Returns correct mass', ()=> {
let c = new Compound({"Li": 3, "Cl": 1});
let mass = c.getMass();
expect(mass).equals(56.27);
})
})
describe('getPercentages()', ()=> {
it('Returns an array', ()=> {
let c = new Compound({"Li": 3, "Cl": 1});
let percentages = c.getPercentages();
expect(percentages).to.be.instanceof(Array);
})
})
describe('toHTML()', ()=> {
it('Returns valid html for single element', ()=> {
let c = new Compound({"Li": 2});
let html = c.toHTML();
expect(html).equals('Li<sub>2</sub>');
})
it('Return valid html for multiple elements', ()=> {
let c = new Compound({"Li": 2, "Cl": 4});
let html = c.toHTML();
expect(html).equals('Li<sub>2</sub>Cl<sub>4</sub>');
})
})
})
describe('Utility.js', ()=> {
describe('stringToElementList()', ()=> {
it('Returns correct element list', ()=> {
var elementList = Utility.stringToElementList("H2O");
expect(elementList).to.deep.equal({'H': 2, 'O': 1});
})
it('Returns null for invalid string', ()=> {
var elementList = Utility.stringToElementList("Hh2O");
expect(elementList).to.be.null;
})
})
})