Skip to content

Commit

Permalink
Merge pull request #1 from MarcL/day2
Browse files Browse the repository at this point in the history
Day 2 : Initial setup + basic tests
  • Loading branch information
MarcL committed Aug 9, 2017
2 parents ac1070b + c606be2 commit 7648ca5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["env"]
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
"bugs": {
"url": "https://github.com/MarcL/javascript-testing-beginners-course/issues"
},
"homepage": "https://github.com/MarcL/javascript-testing-beginners-course#readme"
"homepage": "https://github.com/MarcL/javascript-testing-beginners-course#readme",
"devDependencies": {
"babel-preset-env": "~1.6.0",
"babel-register": "~6.24.1",
"chai": "~4.1.1",
"mocha": "~3.5.0"
}
}
13 changes: 13 additions & 0 deletions src/day2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function day2(data) {
if (typeof data === 'object') {
return Object.assign({}, data);
}

if ((typeof data === 'string') && (data === 'error')) {
throw new Error('Cannot pass error');
}

return data;
}

export default day2;
54 changes: 54 additions & 0 deletions test/day2.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import {expect} from 'chai';
import day2 from '../src/day2';

describe('day2 tests', () => {
describe('check data type', () => {
it('should return undefined when no parameters are passed', () => {
expect(day2()).to.be.undefined;
});

it('should return a string when a string is passed', () => {
expect(day2('a string')).to.be.a('string');
});

it('should return a number when a number is passed', () => {
expect(day2(10)).to.be.a('Number');
});

it('should not be a string when a number is passed', () => {
expect(day2(10)).to.not.be.a('string');
});
});

describe('checking equals', () => {
it('should equal the string passed', () => {
expect(day2('same string')).to.equal('same string');
});

it('should deep equal the object passed', () => {
const givenObject = {
hello: 'world'
};

expect(day2(givenObject)).to.deep.equal(givenObject);
});
});

describe('checking contains', () => {
it('should contain part of the string passed', () => {
const givenString = 'hello world';

expect(day2(givenString)).to.contain('world');
});
});

describe('checking errors', () => {
it('should throw an error when "error" is passed', () => {
function wrappedFunction() {
day2('error');
}

expect(wrappedFunction).to.throw('Cannot pass error');
});
});
});

0 comments on commit 7648ca5

Please sign in to comment.