From 0f1e7b92bc5ac00ab97d610c8d1679859448b661 Mon Sep 17 00:00:00 2001 From: Molly Brown Date: Thu, 15 Jul 2021 15:18:56 -0400 Subject: [PATCH] Attempted to complete testing 1 --- README.md | 16 +++++----- index.js | 78 +++++++++++++++++++++++++++++++++++++++------- index.test.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 155 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 5b1f6419..999b72a7 100644 --- a/README.md +++ b/README.md @@ -11,23 +11,23 @@ There are two possible ways to submit your project. Your instructor should have #### Option A - Codegrade - [ ] Fork and clone the repository. -- [ ] Open the assignment in Canvas and click on the "Set up git" option. +- [] Open the assignment in Canvas and click on the "Set up git" option. - [ ] Follow instructions to set up Codegrade's Webhook and Deploy Key. - [ ] Push your first commit: `git commit --allow-empty -m "first commit" && git push`. - [ ] Check to see that Codegrade has accepted your git submission. #### Option B - Pull Request -- [ ] Fork and clone the repository. -- [ ] Implement your project in a `firstname-lastname` branch. -- [ ] Create a pull request of `firstname-lastname` against your `main` branch. -- [ ] Open the assignment in Canvas and submit your pull request. +- [x ] Fork and clone the repository. +- [ x] Implement your project in a `firstname-lastname` branch. +- [ x] Create a pull request of `firstname-lastname` against your `main` branch. +- [ x] Open the assignment in Canvas and submit your pull request. ### Task 2: Minimum Viable Product -- [ ] For Exercises 1-7 inside `index.js`: - - [ ] Implement the function or the class in `index.js`. - - [ ] Write the corresponding tests in `index.test.js`. +- [ x] For Exercises 1-7 inside `index.js`: + - [x ] Implement the function or the class in `index.js`. + - [x ] Write the corresponding tests in `index.test.js`. #### Notes diff --git a/index.js b/index.js index 28090f12..a10e30ac 100644 --- a/index.js +++ b/index.js @@ -7,7 +7,20 @@ * trimProperties({ name: ' jane ' }) // returns a new object { name: 'jane' } */ function trimProperties(obj) { - // ✨ implement + const objCopy = {}; + + for (const key in obj) { + const value = obj[key]; + + if(typeof value === 'string') { + const trimmedValue = value.trim(); + objCopy[key] = trimmedValue; + } else { + objCopy[key] = value; + } + + }; + return objCopy; } /** @@ -19,7 +32,15 @@ function trimProperties(obj) { * trimPropertiesMutation({ name: ' jane ' }) // returns the object mutated in place { name: 'jane' } */ function trimPropertiesMutation(obj) { - // ✨ implement + for (const key in obj) { + const value = obj[key]; + + if(typeof value === "string") { + const trimmedValue = value.trim(); + obj[key] = trimmedValue; + } + }; + return obj. } /** @@ -31,7 +52,15 @@ function trimPropertiesMutation(obj) { * findLargestInteger([{ integer: 1 }, { integer: 3 }, { integer: 2 }]) // returns 3 */ function findLargestInteger(integers) { - // ✨ implement + let largestInteger = 0; + + integers.forEach(integer => { + if (integer.integer > largestInteger) { + largestInteger = integer.integer; + } + + }) + return largestInteger; } class Counter { @@ -40,7 +69,7 @@ class Counter { * @param {number} initialNumber - the initial state of the count */ constructor(initialNumber) { - // ✨ initialize whatever properties are needed + this.number = initialNumber; } /** @@ -56,7 +85,11 @@ class Counter { * counter.countDown() // returns 0 */ countDown() { - // ✨ implement + const originalNumber = this.number; + if(this.number > 0){ + this.number -= 1; //becuse we're counting down + } + return originalNumber; } } @@ -65,7 +98,7 @@ class Seasons { * [Exercise 5A] Seasons creates a seasons object */ constructor() { - // ✨ initialize whatever properties are needed + this.season = initialSeason; } /** @@ -81,7 +114,15 @@ class Seasons { * seasons.next() // returns "summer" */ next() { - // ✨ implement + const originalSeason = this.season; + + const seasons = ["summer, fall, winter, spring"]; + const originalIndex = seasons.indexOf(orignalSeason); + const nextIndex = (originalIndex + 1) % seasons.length; //Is there a simpler way to achieve this? + + const nextSeason = seasons[nextIndex]; + this.season = nextSeason; + return originalSeason; } } @@ -95,7 +136,7 @@ class Car { constructor(name, tankSize, mpg) { this.odometer = 0 // car initilizes with zero miles this.tank = tankSize // car initiazes full of gas - // ✨ initialize whatever other properties are needed + this.mpg = mpg } /** @@ -112,7 +153,13 @@ class Car { * focus.drive(200) // returns 600 (ran out of gas after 100 miles) */ drive(distance) { - // ✨ implement + const originalOdometer = this.odometer; + if (this.tank > 0){ + this.tank -= distance /this.mpg; + this.odometer += distance; + + } + return this.odometer; } /** @@ -127,7 +174,8 @@ class Car { * focus.refuel(99) // returns 600 (tank only holds 20) */ refuel(gallons) { - // ✨ implement + const gallons = this.gallons; + if (this.odometer) } } @@ -151,8 +199,16 @@ class Car { * }) */ function isEvenNumberAsync(number) { - // ✨ implement + } +//ASYNC STRUCTURE +// it("name", async () => { +// const distance = await odyssey.driveAsync(10) +// expect(distance).toBe(10) +// }) + + + module.exports = { trimProperties, diff --git a/index.test.js b/index.test.js index cfb0a0b3..60eb359f 100644 --- a/index.test.js +++ b/index.test.js @@ -1,23 +1,56 @@ const utils = require('./index') +const { + trimProperties, + trimPropertiesMutation, + findLargestInteger, + Counter, + Seasons, + Car +} = require ('./index'); + describe('[Exercise 1] trimProperties', () => { test('[1] returns an object with the properties trimmed', () => { // EXAMPLE - const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' } - const expected = { foo: 'foo', bar: 'bar', baz: 'baz' } - const actual = utils.trimProperties(input) - expect(actual).toEqual(expected) - }) - // test('[2] returns a copy, leaving the original object intact', () => {}) + const input = { foo: ' foo ', bar: 'bar ', baz: ' baz' }//arrange + const expected = { foo: 'foo', bar: 'bar', baz: 'baz' }//arrange + const actual = utils.trimProperties(input)//act + expect(actual).toEqual(expected) //assert + }) + + test('[2] returns a copy, leaving the original object intact', () => { + const input = {name: ' jane ', age: ' 20 '} + let expected = {name: 'jane', age:'20'} + expect(trimProperties(input)).toEqual(expected) + expect(trimProperties(input)).not.toBe(expected) + }) + }) describe('[Exercise 2] trimPropertiesMutation', () => { // test('[3] returns an object with the properties trimmed', () => {}) // test('[4] the object returned is the exact same one we passed in', () => {}) + const input = { name: ' jane ', age: ' 20 '} + let expected = { name: 'jane', age: '20' } + + test('[3] returns an object with the properties trimmed', () => { + expect(trimPropertiesMutation(input)).toEqual(expected) + }) + + test('[4] the object returned is the exact same one we passed in', () => { + expect(trimPropertiesMutation(input)).toBe(input) + }) + +}) + }) describe('[Exercise 3] findLargestInteger', () => { // test('[5] returns the largest number in an array of objects { integer: 2 }', () => {}) + test('[5] returns the largest number in an array of objects { integer: 2 }', () => { + expect(findLargestInteger([{integer: 2}, {integer: 1}, {integer: 3}])).toEqual(3) + }) + }) describe('[Exercise 4] Counter', () => { @@ -28,6 +61,18 @@ describe('[Exercise 4] Counter', () => { // test('[6] the FIRST CALL of counter.countDown returns the initial count', () => {}) // test('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => {}) // test('[8] the count eventually reaches zero but does not go below zero', () => {}) + test('[6] the FIRST CALL of counter.countDown returns the initial count', () => { + expect(counter.countDown()).toEqual(3) + }) + + test('[7] the SECOND CALL of counter.countDown returns the initial count minus one', () => { + expect(counter.countDown()).not.toEqual(2) + }) + + test('[8] the count eventually reaches zero but does not go below zero', () => { + expect(counter.countDown()).toBeGreaterThanOrEqual(0) + }) + }) describe('[Exercise 5] Seasons', () => { @@ -41,6 +86,32 @@ describe('[Exercise 5] Seasons', () => { // test('[12] the FOURTH call of seasons.next returns "spring"', () => {}) // test('[13] the FIFTH call of seasons.next returns again "summer"', () => {}) // test('[14] the 40th call of seasons.next returns "spring"', () => {}) + + test('[9] the FIRST call of seasons.next returns "summer"', () => { + expect(seasons.next()).toEqual('summer') + }) + test('[10] the SECOND call of seasons.next returns "fall"', () => { + expect(seasons.next()).toEqual('fall') + }) + test('[11] the THIRD call of seasons.next returns "winter"', () => { + expect(seasons.next()).toEqual('winter') + }) + test('[12] the FOURTH call of seasons.next returns "spring"', () => { + expect(seasons.next()).toEqual('spring') + }) + test('[13] the FIFTH call of seasons.next returns again "summer"', () => { + expect(seasons.next()).toEqual('summer') + }) + test('[14] the 40th call of seasons.next returns "spring"', () => { + // create loop 35 times to keep calling next until it returns spring + let testSeason = "" + + for (let i = 5; i < 40; i++) { + testSeason = seasons.next() + } + expect(testSeason).toEqual('spring') + + }) }) describe('[Exercise 6] Car', () => { @@ -52,9 +123,12 @@ describe('[Exercise 6] Car', () => { // test('[16] driving the car uses gas', () => {}) // test('[17] refueling allows to keep driving', () => {}) // test('[18] adding fuel to a full tank has no effect', () => {}) + }) describe('[Exercise 7] isEvenNumberAsync', () => { // test('[19] resolves true if passed an even number', () => {}) // test('[20] resolves false if passed an odd number', () => {}) }) +//EXAMPLE + //return Promise.resolve(distance) -- how you return the asychronous... stuff