Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# CraigRodrigues/practiceSpec.js

Last active Oct 15, 2019
Function Practice Assertions
 // Setup mocha.setup('bdd'); chai.should(); chai.config.includeStack = false; const assert = chai.assert; describe('addTwoNumbers', () => { it('Adds two positive numbers', function() { assert.equal(addTwoNumbers(10, 2), 12); }); it('Adds two negative numbers', function() { assert.equal(addTwoNumbers(-10, -2), -12); }); }); describe('multiplyTwoNumbers', () => { it('Multiplies two positive numbers', () => { assert.equal(multiplyTwoNumbers(10, 2), 20); }); it('Multiplies positive and negative numbers', () => { assert.equal(multiplyTwoNumbers(-10, 2), -20); }); }); describe('isOldEnoughToDrink', () => { it('Should return false for people under 21', () => { assert.equal(isOldEnoughToDrink(15), false); }); it('Should return true for people over 21', () => { assert.equal(isOldEnoughToDrink(45), true); }); }); describe('isOldEnoughToDrive', () => { it('Should return false for people under 16', () => { assert.equal(isOldEnoughToDrive(5), false); }); it('Should return true for people over 16', () => { assert.equal(isOldEnoughToDrive(17), true); }); }); describe('getFirstElement', () => { it('Should return the first element in the array', () => { assert.equal(getFirstElement([1, 2, 3]), 1); }); it('Should return the only element in an array with one item', () => { assert.equal(getFirstElement([10]), 10); }); }); describe('getLastElement', () => { it('Should return the last element in the array', () => { assert.equal(getLastElement([1, 2, 3]), 3); }); it('Should return the only element in an array with one item', () => { assert.equal(getLastElement([10]), 10); }); }); describe('getNthElement', () => { it('Should return 1', () => { assert.equal(getNthElement([1, 2, 3], 0), 1); }); it('Should return 3', () => { assert.equal(getNthElement([1, 2, 3], 2), 3); }); it('Should return undefined if element is not in the array', () => { assert.equal(getNthElement([1, 2, 3], 100), undefined); }); }); describe('addToFront', () => { it('Should add 0', () => { assert.deepEqual(addToFront([1, 2, 3], 0), [0, 1, 2, 3]); }); it('Should add 2', () => { assert.deepEqual(addToFront([1, 2, 3], 2), [2, 1, 2, 3]); }); it('Should add to an empty array', () => { assert.deepEqual(addToFront([], 100), [100]); }); }); describe('addToBack', () => { it('Should add 0', () => { assert.deepEqual(addToBack([1, 2, 3], 0), [1, 2, 3, 0]); }); it('Should add 2', () => { assert.deepEqual(addToBack([1, 2, 3], 2), [1, 2, 3, 2]); }); it('Should add to an empty array', () => { assert.deepEqual(addToBack([], 100), [100]); }); }); describe('computeAreaOfARectangle', () => { it('Should return 2', () => { assert.equal(computeAreaOfARectangle(1, 2), 2); }); it('Should return 8', () => { assert.equal(computeAreaOfARectangle(2, 4), 8); }); it('Should return 0', () => { assert.equal(computeAreaOfARectangle(0, 2), 0); }); }); describe('addAny', () => { it('Should return 45', () => { assert.equal(addAny(1, 2, 3, 4, 5, 6, 7, 8, 9), 45); }); it('Should return 27', () => { assert.equal(addAny(-1, 2, -3, 4, -5, 6, 7, 8, 9), 27); }); }); describe('getFullName', () => { it('Should return the full name', () => { assert.equal(getFullName('a', 'b'), 'a b'); }); it('Should return craig rodrigues', () => { assert.equal(getFullName('craig', 'rodrigues'), 'craig rodrigues'); }); }); describe('getLengthOfWord', () => { it('Should return 8 for sniffles', () => { assert.equal(getLengthOfWord('sniffles'), 8); }); it('Should return 26 for the alphabet', () => { assert.equal(getLengthOfWord('abcdefghijklmnopqrstuvwxyz'), 26); }); }); describe('indexOfElement', () => { it('Should return the index of "b"', () => { assert.equal(indexOfElement(['a', 'b', 'c', 'd', 'e'], 'b'), 1); }); it('Should return the index of "d"', () => { assert.equal(indexOfElement(['a', 'b', 'c', 'd', 'e'], 'd'), 3); }); it('Should return -1 when the element does not exist', () => { assert.equal(indexOfElement(['a', 'b', 'c', 'd', 'e'], 'f'), -1); }); }); describe('getAllElementsButFirst', () => { it('Should return an array without the first element', () => { assert.deepEqual(getAllElementsButFirst(['a', 'b', 'c', 'd', 'e']), ['b', 'c', 'd', 'e']); }); it('Should return an empty array', () => { assert.deepEqual(getAllElementsButFirst(['a']), []); }); }); describe('getAllElementsButLast', () => { it('Should return an array without the last element', () => { assert.deepEqual(getAllElementsButFirst(['a', 'b', 'c', 'd', 'e']), ['a', 'b', 'c', 'd']); }); it('Should return an empty array', () => { assert.deepEqual(getAllElementsButFirst([]), []); }); }); describe('areValidCredentials', () => { it('Should return false for invalid credentials', () => { assert.equal(areValidCredentials('a', '123'), false); }); it('Should return true for valid credentials', () => { assert.equal(areValidCredentials('craig', 'superlongpassword'), true); }); it('Should return false for valid name, but invalid password', () => { assert.equal(areValidCredentials('craig', '123'), false); }); it('Should return false for an invalid name, but valid password', () => { assert.equal(areValidCredentials('abc', 'superlongpassword'), false); }); }); describe('removeAllEvenNumbers', () => { it('should return an array with only odd numbers', () => { assert.deepEqual(removeAllEvenNumbers([1, 2, 3, 4, 5, 6]), [1, 3, 5]); }); it('should return an empty array if all numbers are even', () => { assert.deepEqual(removeAllEvenNumbers([2, 4, 6, 8]), []); }); it('should remove nothing if there are no even numbers', () => { assert.deepEqual(removeAllEvenNumbers([1, 1, 3, 3, 5, 5]), [1, 1, 3, 3, 5, 5]); }); }); describe('removeAllOddNumbers', () => { it('should return an array with only even numbers', () => { assert.deepEqual(removeAllEvenNumbers([1, 2, 3, 4, 5, 6]), [2, 4, 6]); }); it('should return an empty array if all numbers are odd', () => { assert.deepEqual(removeAllEvenNumbers([1, 3, 5, 7]), []); }); it('should remove nothing if there are no odd numbers', () => { assert.deepEqual(removeAllEvenNumbers([2, 2, 4, 4, 6, 6]), [2, 2, 4, 4, 6, 6]); }); }); describe('putNumbersInArray', () => { it('should return an array full of numbers', () => { assert.deepEqual(putNumbersInArray(), [0, 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]) }); it('should return an array with 26 elements in it', () => { assert.lengthOf(putNumbersInArray(), 26); }); }); describe('addTenToEach', () => { it('should add ten to positive numbers', () => { assert.deepEqual(addTenToEach([1, 2, 3, 4, 5, 6]), [11, 12, 13, 14, 15, 16]); }); it('should add ten to negative numbers', () => { assert.deepEqual(addTenToEach([-1, -2, -3, -4, -5, -6]), [9, 8, 7, 6, 5, 4]); }); it('should do nothing to an empty array', () => { assert.deepEqual(addTenToEach([]), []); }); }); describe('personName', () => { it('should return the first name', () => { assert.equal(personName(), 'sally'); }); it('should not return undefined', () => { assert.isOk(personName()); }); }); describe('cityOfBirth', () => { it('should return Orem', () => { assert.equal(cityOfBirth(), 'Orem, UT'); }); it('should not return undefined', () => { assert.isOk(cityOfBirth()); }); }); describe('addPhoneToPocket', () => { it('should return an object', () => { assert.typeOf(addPhoneToPocket({}), 'object'); }) it('should return an object with a frontpocket property/key', () => { assert.property(addPhoneToPocket({}), 'frontpocket'); }); it('should have "iPhone" set at the frontpocket property/key', () => { assert.equal(addPhoneToPocket({}).frontpocket, 'iPhone'); }); }); describe('movePhoneToBackpocket', () => { it('should return an object', () => { assert.typeOf(movePhoneToBackpocket(), 'object'); }) it('should return an object with a backpocket property/key', () => { assert.property(movePhoneToBackpocket(), 'backpocket'); }); it('should have "iPhone" set at the backpocket property/key', () => { assert.equal(movePhoneToBackpocket().backpocket, 'iPhone'); }) }); describe('statsLooper', () => { it('should return an object', () => { assert.typeOf(statsLooper(), 'object'); }); it('should have an age property', () => { assert.property(statsLooper(), 'age'); }); it('should have an score property', () => { assert.property(statsLooper(), 'score'); }); it('should have an length property', () => { assert.property(statsLooper(), 'length'); }); it('should have an year property', () => { assert.property(statsLooper(), 'year'); }); it('age should be 0', () => { assert.equal(statsLooper().age, 0); }); it('score should be 0', () => { assert.equal(statsLooper().score, 0); }); it('length should be 0', () => { assert.equal(statsLooper().length, 0); }); it('year should be 0', () => { assert.equal(statsLooper().year, 0); }); }); describe('statePopulationLooper', () => { it('should return an object', () => { assert.typeOf(statePopulationLooper(), 'object'); }); it('should have a utah property', () => { assert.property(statePopulationLooper(), 'utah'); }); it('should have an texas property', () => { assert.property(statePopulationLooper(), 'texas'); }); it('should have an california property', () => { assert.property(statePopulationLooper(), 'california'); }); it('Utah pop should not change', () => { assert.equal(statePopulationLooper().utah, 2942902); }); it('Texas pop should be 0', () => { assert.equal(statePopulationLooper().texas, 0); }); it('California pop be 0', () => { assert.equal(statePopulationLooper().california, 0); }); }); describe('cleanUser', () => { let user = { name: 'Craig', password: '', age: 34, admin: false, prop1: 7, prop2: 'hi', prop3: false, prop4: 0, prop5: null, prop6: undefined, prop7: 'still here' }; let expected = { name: 'Craig', age: 34, prop1: 7, prop2: 'hi', prop7: 'still here' }; it('should return an object', () => { assert.typeOf(cleanUser(user), 'object'); }); it('should remove any falsy properties', () => { assert.deepEqual(cleanUser(user), expected); }); it('should have 5 properties left', () => { assert.lengthOf(Object.keys(cleanUser(user)), 5); }) }); describe('getAllKeys', () => { it('should return all keys', function() { assert.deepEqual(getAllKeys({ a: 1, b: 2, c: 3}), ['a', 'b', 'c']); }); it('should not use Object.keys', function() { let spy = sinon.spy(Object, 'keys'); getAllKeys({ a: 1, b: 2, c: 3}); sinon.assert.notCalled(spy); Object.keys.restore(); }); });
to join this conversation on GitHub. Already have an account? Sign in to comment