Skip to content

Commit 506474d

Browse files
test[2020-day-01]: unit tests for expense validation process
1 parent 24e9139 commit 506474d

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* eslint-env mocha */
2+
const { expect } = require('chai')
3+
const { validateRecords } = require('./expenseValidation')
4+
const testData = [
5+
1721,
6+
979,
7+
366,
8+
299,
9+
675,
10+
1456
11+
]
12+
13+
describe('--- 2020 Day 1: Report Repair ---', () => {
14+
describe('Part 1', () => {
15+
describe('validateRecords()', () => {
16+
it('it finds the two records that sum to 2020', () => {
17+
const expected = [1721, 299]
18+
const results = validateRecords(testData, 2020)
19+
// Should be 2 results
20+
expect(results.length).to.equal(2)
21+
// Result order is unnecessary, but all expected hould be in the result set
22+
expected.forEach(result => {
23+
expect(testData.indexOf(result)).to.be.greaterThan(-1)
24+
})
25+
})
26+
it('it supports specifying an alternate checksum', () => {
27+
const arrSum = (arr) => arr.reduce((x, y) => x + y, 0)
28+
const expected = [testData[3], testData[5]]
29+
const checksum = arrSum(expected)
30+
const results = validateRecords(testData, checksum)
31+
// Should be 2 results
32+
expect(results.length).to.equal(2)
33+
// Result order is unnecessary, but all expected hould be in the result set
34+
expected.forEach(result => {
35+
expect(results.indexOf(result)).to.be.greaterThan(-1)
36+
})
37+
// Results add up to the checksum
38+
expect(arrSum(results)).to.equal(checksum)
39+
})
40+
it('Throws an error when no records provided', () => {
41+
expect(validateRecords).to.throw()
42+
})
43+
it('Throws an error when no records match checksum', () => {
44+
expect(() => validateRecords([1, 2, 3], 100)).to.throw('Couldn\'t find a checksum match')
45+
})
46+
})
47+
})
48+
})

0 commit comments

Comments
 (0)