Skip to content

Instantly share code, notes, and snippets.

@ryan-hamblin
Created October 10, 2017 00:57
Show Gist options
  • Select an option

  • Save ryan-hamblin/66fa0b64f2ec96ef99fbc83a73ae9747 to your computer and use it in GitHub Desktop.

Select an option

Save ryan-hamblin/66fa0b64f2ec96ef99fbc83a73ae9747 to your computer and use it in GitHub Desktop.
testing schtuff
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const cases = require('../src/app.js');
const expect = chai.expect;
const assert = chai.assert;
chai.use(sinonChai);
describe('Cases from app.js', () => {
describe('apples', () => {
let newApples = [];
beforeEach(() => {
newApples.push('red', 'green', 'yellow');
});
afterEach(() => {
newApples = [];
});
const apples = cases.apples;
it('should be an array', () => {
expect(newApples).to.be.an('array'); // Begin here something is wrong, fix it.
});
it('should have a length of four indeces', () => {
expect(apples).to.have.lengthOf(4);
});
it('should have a string called "Red Delicious" ', () => {
expect(apples).to.include('Red Delicious');
});
// write a test to check to see if the apples array contains 'Red Delicious'.
});
describe('Ben', () => {
const Ben = cases.Ben;
it('should be an object', () => {
expect(Ben).to.be.an('object');
});
it('should have bens band equal to beastie boys', () => {
expect(Ben.favBand).to.equal('Beastie Boys');
});
});
describe('Sean', () => {
it('should be an object', () => {
const Sean = cases.Sean;
expect(Sean).to.be.an('object');
});
// write a test to check if Seans's favorite food is 'Pizza'.
});
describe('Ryan', () => {
it('should be an object', () => {
const Ryan = cases.Ryan;
expect(Ryan).to.be.an('object');
});
// write a test to see if Ryan's favBook is `not` 'Harry Potter'.
});
describe('Austen', () => {
it('should be an object', () => {
const Austen = cases.Austen;
expect(Austen).to.be.an('object');
});
// write a test to see if Austen's favColor is not 'Gold'.
});
describe('Karthik', () => {
it('should be an object', () => {
const Karthik = cases.Karthik;
expect(Karthik).to.be.an('object');
});
// write a test to check if Karthik's favActivity is 'Rock Climbing'.
});
describe('addNums', () => {
const addNums = cases.addNums;
it('should be a function', () => {
expect(addNums).to.be.a('function');
});
it('should equal 3 if 1 and 2 are given', () => {
expect(addNums(1, 2)).to.equal(3);
});
// write a test to check if 'addNums' returns the expected value
// i.e. if is called addNums(1, 2); the return value should be 3.
});
describe('callBackInvoker', () => {
it('should be a function', () => {
const callBackInvoker = cases.callBackInvoker;
expect(callBackInvoker).to.be.a('function');
});
it('should invoke a given callback passed to it', () => {
// this is where you're going to be using 'chai's sinon' spy function.
const callBack = sinon.spy();
const newCbInvoker = cases.callBackInvoker;
newCbInvoker(callBack);
newCbInvoker(callBack);
newCbInvoker(callBack);
expect(callBack).to.have.been.calledThrice;
});
});
describe('iterator', () => {
const iterator = cases.iterator;
it('should be a function', () => {
expect(iterator).to.be.a('function');
});
//similiar to above where we are utilizing our spy from sinon, this assertion should test if a cb is called x times.
it('should call a callback for n times passed to cases.iterator', () => {
const callBack = sinon.spy();
iterator(5, callBack);
expect(callBack).to.have.callCount(5);
});
});
describe('each', () => {
const each = cases.each;
it('should be a function', () => {
expect(each).to.be.a('function');
});
it('should pass values back from array to callback', () => {
const nums = [1, 2, 3];
const expectedOutPut = [];
const eachCb = (element, index) => {
expectedOutPut.push(element + 1);
};
each(nums, eachCb);
expect(expectedOutPut).to.deep.equal([2, 3, 4]);
});
it('should iterate x amount of times for the given length of an array', () => {
const nums = [1, 2, 3];
const length = nums.length;
const spy = sinon.spy();
each(nums, spy);
expect(spy).to.have.callCount(nums.length);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment