forked from turingschool-examples/fitlit-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from edwin-chalmers/ed/it5
split tests into different files
- Loading branch information
Showing
9 changed files
with
512 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import hydration from '../src/data/hydration-test-data' | ||
|
||
function getAverageDailyFluidOunces(userId, hydroData) { | ||
const userHydrationData = hydroData.filter((userRecord) => userRecord.userID === userId); | ||
const totalOunces = userHydrationData.reduce((acc, userRecord) => acc += userRecord.numOunces, 0); | ||
return userHydrationData.length > 0 ? totalOunces / userHydrationData.length : 0; | ||
} | ||
|
||
function getSpecificDay(userId, date, hydroData) { | ||
const dayEntry = hydroData.find(entry => entry.userID === userId && entry.date === date); | ||
return dayEntry ? dayEntry.numOunces : 0; | ||
} | ||
|
||
function getWeeklyFluidOunces(userId) { | ||
const userHydrationData = hydration.hydrationData.filter(record => record.userID === userId); | ||
userHydrationData.sort((a, b) => new Date(b.date) - new Date(a.date)); | ||
const lastWeekData = userHydrationData.slice(0, 7); | ||
return lastWeekData.map(record => ({ | ||
date: record.date, | ||
numOunces: record.numOunces | ||
})); | ||
} | ||
|
||
export { | ||
getAverageDailyFluidOunces, | ||
getSpecificDay, | ||
getWeeklyFluidOunces, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import sleep from '../src/data/sleep-test-data' | ||
|
||
function getUserSleepData(randomUser) { | ||
return sleep.sleepData.filter(user => user.userID === randomUser.id) | ||
} | ||
|
||
function getAverageSleepHours(randomUser) { | ||
let sameUserSleepData = getUserSleepData(randomUser) | ||
let averageSleepHours = 0 | ||
let totalSleepHours = 0 | ||
sameUserSleepData.forEach(obj => { | ||
totalSleepHours += obj.hoursSlept | ||
}) | ||
averageSleepHours = totalSleepHours / sameUserSleepData.length | ||
return averageSleepHours.toFixed(2) | ||
} | ||
|
||
function getAverageSleepQuality(randomUser) { | ||
let sameUserSleepData = getUserSleepData(randomUser) | ||
let averageSleepQuality = 0 | ||
let totalSleepQuality = 0 | ||
sameUserSleepData.forEach(obj => { | ||
totalSleepQuality += obj.sleepQuality | ||
}) | ||
averageSleepQuality = totalSleepQuality / sameUserSleepData.length | ||
return averageSleepQuality.toFixed(2) | ||
} | ||
|
||
function getMostRecentSleepHours(randomUser) { | ||
let sameUserSleepData = getUserSleepData(randomUser) | ||
let latestSleepDataIndex = sameUserSleepData.length - 1 | ||
return sameUserSleepData[latestSleepDataIndex].hoursSlept | ||
} | ||
|
||
function getMostRecentSleepQuality(randomUser) { | ||
let sameUserSleepData = getUserSleepData(randomUser) | ||
let latestSleepDataIndex = sameUserSleepData.length - 1 | ||
return sameUserSleepData[latestSleepDataIndex].sleepQuality | ||
} | ||
|
||
function getWeeklySleepHours(selectedWeek) { | ||
return selectedWeek[0].map(day => day.hoursSlept) | ||
} | ||
|
||
function getWeeklySleepQuality(selectedWeek) { | ||
return selectedWeek[0].map(day => day.sleepQuality) | ||
} | ||
|
||
export { | ||
getUserSleepData, | ||
getAverageSleepHours, | ||
getAverageSleepQuality, | ||
getMostRecentSleepHours, | ||
getMostRecentSleepQuality, | ||
getWeeklySleepHours, | ||
getWeeklySleepQuality | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { expect } from 'chai'; | ||
import { | ||
generateRandomUser, | ||
getAverageDailyFluidOunces, | ||
getSpecificDay, | ||
getWeeklyFluidOunces, | ||
} from '../src/hydration'; | ||
import account from '../src/data/users-test-data' | ||
import hydration from '../src/data/hydration-test-data' | ||
|
||
describe('getAverageDailyFluidOunces()', () => { | ||
|
||
const account = [ | ||
{ userID: 1, numOunces: 64 }, | ||
{ userID: 1, numOunces: 80 }, | ||
{ userID: 2, numOunces: 72 }, | ||
{ userID: 1, numOunces: 96 }, | ||
]; | ||
|
||
it('calculates the correct average for multiple records', () => { | ||
expect(getAverageDailyFluidOunces(1, account)).to.equal((64 + 80 + 96) / 3); | ||
}); | ||
|
||
it('returns 0 for a user with no records', () => { | ||
expect(getAverageDailyFluidOunces(3, account)).to.equal(0); | ||
}); | ||
|
||
it('returns the correct value for a single record', () => { | ||
const singleRecordAccount = [{ userID: 1, numOunces: 64 }]; | ||
expect(getAverageDailyFluidOunces(1, singleRecordAccount)).to.equal(64); | ||
}); | ||
|
||
it('filters records correctly among multiple users', () => { | ||
expect(getAverageDailyFluidOunces(2, account)).to.equal(72); | ||
}); | ||
|
||
}); | ||
|
||
describe('getSpecificDay()', () => { | ||
|
||
const hydroData = [ | ||
{ userID: 1, date: '2022-04-01', numOunces: 64 }, | ||
{ userID: 1, date: '2022-04-02', numOunces: 80 }, | ||
{ userID: 2, date: '2022-04-01', numOunces: 72 }, | ||
{ userID: 1, date: '2022-04-03', numOunces: 96 }, | ||
]; | ||
|
||
it('returns the correct numOunces for a user on a specific date', () => { | ||
const ounces = getSpecificDay(1, '2022-04-02', hydroData); | ||
expect(ounces).to.equal(80); | ||
}); | ||
|
||
it('returns 0 if no entry is found for the user on the specified date', () => { | ||
const ounces = getSpecificDay(1, '2022-04-04', hydroData); | ||
expect(ounces).to.equal(0); | ||
}); | ||
|
||
it('returns 0 if the user ID does not exist in the data', () => { | ||
const ounces = getSpecificDay(3, '2022-04-01', hydroData); | ||
expect(ounces).to.equal(0); | ||
}); | ||
|
||
it('handles searching for a date with multiple users\' data correctly', () => { | ||
const ounces = getSpecificDay(2, '2022-04-01', hydroData); | ||
expect(ounces).to.equal(72); | ||
}); | ||
|
||
it('returns 0 for invalid userId types', () => { | ||
const ounces = getSpecificDay(null, '2022-04-02', hydroData); | ||
expect(ounces).to.equal(0); | ||
}); | ||
|
||
it('returns 0 for invalid date formats', () => { | ||
const ounces = getSpecificDay(1, 'not-a-date', hydroData); | ||
expect(ounces).to.equal(0); | ||
}); | ||
|
||
it('returns 0 when hydroData is empty', () => { | ||
const ounces = getSpecificDay(1, '2022-04-02', []); | ||
expect(ounces).to.equal(0); | ||
}); | ||
|
||
}); | ||
|
||
describe('getWeeklyFluidOunces()', () => { | ||
|
||
let randomUser = generateRandomUser() | ||
|
||
it('returns the last week data for a user', function() { | ||
const result = getWeeklyFluidOunces(2); | ||
expect(result.length).to.equal(7); | ||
expect(result[0].numOunces).to.equal(78); | ||
expect(result[4].numOunces).to.equal(70); | ||
}); | ||
|
||
it('returns the last week data for a different user', function() { | ||
const result = getWeeklyFluidOunces(9); | ||
expect(result.length).to.equal(7); | ||
expect(result[0].numOunces).to.equal(86); | ||
expect(result[6].numOunces).to.equal(74); | ||
}); | ||
|
||
it('returns an empty array for users with no data', function() { | ||
const result = getWeeklyFluidOunces(999); | ||
expect(result).to.be.an('array').that.is.empty; | ||
}); | ||
|
||
}); |
Oops, something went wrong.