Skip to content

Commit 7648ca5

Browse files
authored
Merge pull request #1 from MarcL/day2
Day 2 : Initial setup + basic tests
2 parents ac1070b + c606be2 commit 7648ca5

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["env"]
3+
}

package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,11 @@
1616
"bugs": {
1717
"url": "https://github.com/MarcL/javascript-testing-beginners-course/issues"
1818
},
19-
"homepage": "https://github.com/MarcL/javascript-testing-beginners-course#readme"
19+
"homepage": "https://github.com/MarcL/javascript-testing-beginners-course#readme",
20+
"devDependencies": {
21+
"babel-preset-env": "~1.6.0",
22+
"babel-register": "~6.24.1",
23+
"chai": "~4.1.1",
24+
"mocha": "~3.5.0"
25+
}
2026
}

src/day2.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function day2(data) {
2+
if (typeof data === 'object') {
3+
return Object.assign({}, data);
4+
}
5+
6+
if ((typeof data === 'string') && (data === 'error')) {
7+
throw new Error('Cannot pass error');
8+
}
9+
10+
return data;
11+
}
12+
13+
export default day2;

test/day2.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {expect} from 'chai';
2+
import day2 from '../src/day2';
3+
4+
describe('day2 tests', () => {
5+
describe('check data type', () => {
6+
it('should return undefined when no parameters are passed', () => {
7+
expect(day2()).to.be.undefined;
8+
});
9+
10+
it('should return a string when a string is passed', () => {
11+
expect(day2('a string')).to.be.a('string');
12+
});
13+
14+
it('should return a number when a number is passed', () => {
15+
expect(day2(10)).to.be.a('Number');
16+
});
17+
18+
it('should not be a string when a number is passed', () => {
19+
expect(day2(10)).to.not.be.a('string');
20+
});
21+
});
22+
23+
describe('checking equals', () => {
24+
it('should equal the string passed', () => {
25+
expect(day2('same string')).to.equal('same string');
26+
});
27+
28+
it('should deep equal the object passed', () => {
29+
const givenObject = {
30+
hello: 'world'
31+
};
32+
33+
expect(day2(givenObject)).to.deep.equal(givenObject);
34+
});
35+
});
36+
37+
describe('checking contains', () => {
38+
it('should contain part of the string passed', () => {
39+
const givenString = 'hello world';
40+
41+
expect(day2(givenString)).to.contain('world');
42+
});
43+
});
44+
45+
describe('checking errors', () => {
46+
it('should throw an error when "error" is passed', () => {
47+
function wrappedFunction() {
48+
day2('error');
49+
}
50+
51+
expect(wrappedFunction).to.throw('Cannot pass error');
52+
});
53+
});
54+
});

0 commit comments

Comments
 (0)