Skip to content

Commit

Permalink
Merge pull request #68 from edwin-chalmers/ed/it5
Browse files Browse the repository at this point in the history
split tests into different files
  • Loading branch information
Virulencies authored Feb 22, 2024
2 parents 8f7d8a2 + b578747 commit 085ac46
Show file tree
Hide file tree
Showing 9 changed files with 512 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h3>Account Details</h3>
<span><p>Stride Length:</p><h3 id="account-stride">stride length</h3></span>
<span><p>Daily Step Goal:</p><h3 id="account-step">daily step goal</h3></span>
<span><p>Friends:</p><h3 id="account-friends">friends</h3></span>
<!-- Update HTML in Account Details section to be dynamic with UpdateAccount refactor -->
<!-- Update HTML in Account Details section to be dynamic with UpdateAccount refactor -->
<!-- <span><p>User:</p><h3 id="name">name</h3></span>
<span><p>Address:</p><h3 id="address">address</h3></span>
<span><p>Email:</p><h3 id="email">email</h3></span>
Expand Down
2 changes: 1 addition & 1 deletion src/domUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function updateAccountFriends(friends) {
accountFriends.textContent = `${friends}`;
}

// refactor updateAccountFriends() + updateAccountStep() + updateAccountStride()
// refactor updateAccountFriends() + updateAccountStep() + updateAccountStride()
// + updateAccountName() + updateAccountEmail() + updateAccountAddress()
function updateAccountData(user) {
Object.keys(user).forEach(dataType => {
Expand Down
28 changes: 28 additions & 0 deletions src/hydration.js
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,
};
2 changes: 2 additions & 0 deletions src/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ function getWeeklyFluidOunces(userId) {
function getUserSleepData(randomUser) {
return appState.sleep.sleepData.filter(user => user.userID === randomUser.id)
}

function getAverageSleepHours(randomUser) {
let sameUserSleepData = getUserSleepData(randomUser)
let averageSleepHours = 0
Expand Down Expand Up @@ -137,6 +138,7 @@ function getMostRecentSleepQuality(randomUser) {

// refactored getMostRecentSleepHours() + getMostRecentSleepQuality()
function getMostRecentSleepData(randomUser, sleepDataType) {

let sameUserSleepData = getUserSleepData(randomUser)
let latestSleepDataIndex = sameUserSleepData.length - 1
return sameUserSleepData[latestSleepDataIndex][sleepDataType]
Expand Down
57 changes: 57 additions & 0 deletions src/sleep.js
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
};
10 changes: 9 additions & 1 deletion src/users.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import account from '../src/data/users-test-data'

// ----- * Users * ----- //

function generateRandomUser() {
const randomIndex = Math.floor(Math.random() * account.users.length);
return account.users[randomIndex];
Expand All @@ -16,7 +18,13 @@ function getAccountFriends(user) {
return friendNames.join(" - ")
}

function getAverageStepGoal(account) {
const totalStepsGoal = account.users.reduce((total, user) => total + user.dailyStepGoal, 0);
return totalStepsGoal / account.users.length;
}


export {
generateRandomUser,
getAccountFriends,
};
getAverageStepGoal,
108 changes: 108 additions & 0 deletions test/hydration-test.js
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;
});

});
Loading

0 comments on commit 085ac46

Please sign in to comment.