Commit 568a3c83 authored by Johan-Henrik Fylling's avatar Johan-Henrik Fylling
Browse files

Add more tests of workouts.js and api.js. Add --coverage to jest. Add export...

Add more tests of workouts.js and api.js. Add --coverage to jest. Add export to workouts and api.js to be able to get coverage reports.
parent 0b35dc5d
Pipeline #115274 passed with stages
in 39 seconds
module.exports = {
setupFiles: ["./setupJest.js"],
setupFiles: ["./setupJest.js"]
};
\ No newline at end of file
......@@ -5,7 +5,7 @@
"description": "An app for school purposes",
"main": "index.js",
"scripts": {
"test": "jest",
"test": "jest --coverage",
"dev": "nodemon devServer.js"
},
"keywords": [
......
......@@ -25,4 +25,8 @@ async function fetchWorkouts(ordering) {
return workouts;
}
}
\ No newline at end of file
}
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined'){
module.exports = {fetchExerciseTypes, fetchWorkouts}
}
const fs = require("fs");
const path = require("path");
const api = require("./api.js");
async function login(username, password) {
let body = { "username": username, "password": password };
let response = await sendRequest("POST", `${HOST}/api/token/`, body);
if (response.ok) {
let data = await response.json();
// access and refresh cookies each have a max age of 24 hours
setCookie("access", data.access, 86400, "/");
setCookie("refresh", data.refresh, 86400, "/");
} else {
let data = await response.json();
fail("Login failed: " + data);
}
}
describe("api.js test", () => {
beforeAll(() => {
const defaults = window.document.createElement("script");
defaults.textContent = fs.readFileSync(path.resolve(__dirname, "./defaults.js"), "utf-8");
window.document.body.appendChild(defaults);
const scripts = window.document.createElement("script");
scripts.textContent = fs.readFileSync(path.resolve(__dirname, "./scripts.js"), "utf-8");
window.document.body.appendChild(scripts);
return login("admin", "Password");
});
it("fetches workouts", async () => {
return api.fetchWorkouts("date")
.then((response) => {
expect(response.length).toBe(2);
});
});
it("fetches exercises", async () => {
return api.fetchExerciseTypes()
.then((response) => {
expect(response.length).toBe(3);
})
})
})
\ No newline at end of file
......@@ -191,4 +191,3 @@ function createAlert(header, data) {
}
window.addEventListener("DOMContentLoaded", updateNavBar);
......@@ -190,4 +190,9 @@ window.addEventListener("DOMContentLoaded", async () => {
toggleHideWorkouts(filteredWorkouts);
});
}
});
\ No newline at end of file
});
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined'){
module.exports = {renderWorkouts, renderExerciseOptions, toggleHideWorkouts, filterWorkouts}
}
......@@ -3,6 +3,8 @@ const fs = require("fs");
const path = require("path");
const html = fs.readFileSync(path.resolve(__dirname, "../workouts.html"), "utf-8");
const workoutsModule = require("./workouts.js");
async function login(username, password) {
let body = { "username": username, "password": password };
......@@ -60,23 +62,6 @@ describe("workouts test", () => {
expect(workoutAnchors.length).toBe(2);
});
// it("filters workouts based on search", async () => {
// try {
// let workoutsList = await fetchWorkouts("date");
// expect(workoutsList.length).toBe(2);
// search(workoutsList, "a");
// let workoutAnchors = window.document.querySelectorAll("a.workout:not(.hide)");
// expect(workoutAnchors.length).toBe(1);
// let h5 = workoutAnchors[0].querySelector("h5");
// expect(h5.textContent).toBe("a workout")
// } catch (error) {
// fail(error);
// }
// });
it("Has correct exercises", async () => {
let workouts = await fetchWorkouts("date");
......@@ -129,20 +114,20 @@ describe("workouts test", () => {
it("Renders exercise type filter options", async () => {
return fetchExerciseTypes()
.then((exerciseTypes) => {
renderExerciseOptions(exerciseTypes);
workoutsModule.renderExerciseOptions(exerciseTypes);
const selectElement = window.document.querySelector("#exercise-filter-select");
expect(selectElement.children.length).toBe(4);
});
});
it("Filters finds all workouts within range", async () => {
it("Filters workouts based on date", async () => {
const currentUser = await getCurrentUser();
const workouts = await fetchWorkouts("date");
let EXERCISE_FILTER = "any";
let DATE_RANGE = [new Date("2021-02-01T13:00:00"), new Date("2021-04-01T13:00:00")]
toggleHideWorkouts(
filterWorkouts(
workoutsModule.toggleHideWorkouts(
workoutsModule.filterWorkouts(
currentUser,
workouts,
SEARCH_STRING,
......@@ -155,8 +140,47 @@ describe("workouts test", () => {
expect(workoutAnchors.length).toBe(2);
DATE_RANGE = [new Date("2021-03-01T13:00:00"), new Date("2021-04-01T13:00:00")]
toggleHideWorkouts(
filterWorkouts(
workoutsModule.toggleHideWorkouts(
workoutsModule.filterWorkouts(
currentUser,
workouts,
SEARCH_STRING,
EXERCISE_FILTER,
WORKOUT_VISIBILITY,
DATE_RANGE
));
workoutAnchors = window.document.querySelectorAll("a.workout:not(.hide)");
expect(workoutAnchors.length).toBe(1);
});
it("Filters workouts by search", async () => {
const currentUser = await getCurrentUser();
const workouts = await fetchWorkouts("date");
let EXERCISE_FILTER = "any";
let DATE_RANGE = [new Date("2019-01-01T13:00:00"), new Date("2022-10-01T13:00:00")];
let SEARCH_STRING = undefined;
workoutsModule.toggleHideWorkouts(
workoutsModule.filterWorkouts(
currentUser,
workouts,
SEARCH_STRING,
EXERCISE_FILTER,
WORKOUT_VISIBILITY,
DATE_RANGE
));
let workoutAnchors = window.document.querySelectorAll("a.workout:not(.hide)");
expect(workoutAnchors.length).toBe(2);
SEARCH_STRING = "a";
workoutsModule.toggleHideWorkouts(
workoutsModule.filterWorkouts(
currentUser,
workouts,
SEARCH_STRING,
......@@ -169,4 +193,42 @@ describe("workouts test", () => {
expect(workoutAnchors.length).toBe(1);
});
it("Filters workouts by exercise type", async () => {
const currentUser = await getCurrentUser();
const workouts = await fetchWorkouts("date");
const exerciseTypes = await fetchExerciseTypes();
let EXERCISE_FILTER = "any";
let DATE_RANGE = [new Date("2019-01-01T13:00:00"), new Date("2022-10-01T13:00:00")]
workoutsModule.toggleHideWorkouts(
workoutsModule.filterWorkouts(
currentUser,
workouts,
SEARCH_STRING,
EXERCISE_FILTER,
WORKOUT_VISIBILITY,
DATE_RANGE
));
let workoutAnchors = window.document.querySelectorAll("a.workout:not(.hide)");
expect(workoutAnchors.length).toBe(2);
EXERCISE_FILTER = exerciseTypes[0].url;
workoutsModule.toggleHideWorkouts(
workoutsModule.filterWorkouts(
currentUser,
workouts,
SEARCH_STRING,
EXERCISE_FILTER,
WORKOUT_VISIBILITY,
DATE_RANGE
));
workoutAnchors = window.document.querySelectorAll("a.workout:not(.hide)");
expect(workoutAnchors.length).toBe(1);
});
});
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment