Skip to content

Commit

Permalink
add jest setup and teardown hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
brauliorivas committed May 20, 2024
1 parent f265a32 commit a51b6df
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 58 deletions.
52 changes: 29 additions & 23 deletions test/objects.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { InfoBox, Link } from "../js/objects";

describe("InfoBox", () => {
it("should initialize with correct values", () => {
const id = 1;
const infoBox = new InfoBox(id);
let infoBox;

beforeEach(() => {
infoBox = new InfoBox(1);
});

afterEach(() => {
infoBox = null;
});

expect(infoBox.id).toBe(id);
it("should initialize with correct values", () => {
expect(infoBox.id).toBe(1);
expect(infoBox.x).toBe(0);
expect(infoBox.y).toBe(0);
expect(infoBox.width).toBe(120);
Expand Down Expand Up @@ -37,31 +44,27 @@ describe("InfoBox", () => {
});

it("should return true if (x, y) coordinates are within the box", () => {
const infoBox = new InfoBox(1);
const x = 60;
const y = 120;

expect(infoBox.isHere(x, y)).toBe(true);
});

it("should return false if x coordinate is outside the box", () => {
const infoBox = new InfoBox(1);
const x = 200;
const y = 120;

expect(infoBox.isHere(x, y)).toBe(false);
});

it("should return false if y coordinate is outside the box", () => {
const infoBox = new InfoBox(1);
const x = 50;
const y = -1;

expect(infoBox.isHere(x, y)).toBe(false);
});

it("should return true if box is visible within the given area", () => {
const infoBox = new InfoBox(1);
const x = 0;
const y = 0;
const width = 200;
Expand All @@ -71,7 +74,6 @@ describe("InfoBox", () => {
});

it("should return false if the box is to the right of the area", () => {
const infoBox = new InfoBox(1);
infoBox.x = 300;
const x = 0;
const y = 0;
Expand All @@ -82,7 +84,6 @@ describe("InfoBox", () => {
});

it("should return false if the box is to the left of the area", () => {
const infoBox = new InfoBox(1);
infoBox.x = -300;
const x = 0;
const y = 0;
Expand All @@ -93,7 +94,6 @@ describe("InfoBox", () => {
});

it("should return false if the box is below the area", () => {
const infoBox = new InfoBox(1);
infoBox.y = 300;
const x = 0;
const y = 0;
Expand All @@ -104,7 +104,6 @@ describe("InfoBox", () => {
});

it("should return false if the box is above the area", () => {
const infoBox = new InfoBox(1);
infoBox.y = -300;
const x = 0;
const y = 0;
Expand All @@ -116,33 +115,40 @@ describe("InfoBox", () => {
});

describe("Link", () => {
it("should construct correctly", () => {
const link = new Link(1, 0, 1);
let link;

beforeEach(() => {
link = new Link(1, 0, 1);
});

afterEach(() => {
link = null;
});

it("should construct correctly", () => {
expect(link.id).toBe(1);
expect(link.from).toBe(0);
expect(link.to).toBe(1);
expect(link.color).toBe("#A00");
expect(link.xShift).toBe(0);
});

let firstInfoBox, secondInfoBox, infoBoxes;

beforeEach(() => {
firstInfoBox = new InfoBox(0);
secondInfoBox = new InfoBox(1);
infoBoxes = [firstInfoBox, secondInfoBox];
});

it("should return true if the link is visible", () => {
const firstInfoBox = new InfoBox(0);
const secondInfoBox = new InfoBox(1);
secondInfoBox.x = 140;
secondInfoBox.y = 250;
const infoBoxes = [firstInfoBox, secondInfoBox];
const link = new Link(1, 0, 1);

expect(link.isVisible(0, 0, 250, 250, infoBoxes)).toBe(true);
});

it("should return false if the link is not visible", () => {
const firstInfoBox = new InfoBox(0);
const secondInfoBox = new InfoBox(1);
const infoBoxes = [firstInfoBox, secondInfoBox];
const link = new Link(1, 0, 1);

expect(link.isVisible(10, 10, 50, 50, infoBoxes)).toBe(false);
});
});
59 changes: 32 additions & 27 deletions test/primitives.test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
import { jest } from "@jest/globals";
import { drawRoundedRect, drawTex } from "../js/graphic-primitives.js";

let ctx;

beforeEach(() => {
ctx = {
save: jest.fn(),
fillStyle: null,
beginPath: jest.fn(),
roundRect: jest.fn(),
fill: jest.fn(),
strokeStyle: null,
lineWidth: null,
stroke: jest.fn(),
restore: jest.fn(),
drawImage: jest.fn(),
};
});

afterEach(() => {
ctx = null;
});

describe("drawRoundedRect", () => {
it("should draw a rounded rectangle with the correct properties", () => {
const ctx = {
save: jest.fn(),
fillStyle: null,
beginPath: jest.fn(),
roundRect: jest.fn(),
fill: jest.fn(),
strokeStyle: null,
lineWidth: null,
stroke: jest.fn(),
restore: jest.fn(),
};
drawRoundedRect(ctx, 10, 20, 100, 200, "red");

expect(ctx.save).toHaveBeenCalled();
Expand All @@ -31,16 +41,20 @@ describe("drawRoundedRect", () => {
});

describe("drawTex", () => {
it("should draw an image with the correct properties", () => {
const ctx = {
save: jest.fn(),
drawImage: jest.fn(),
restore: jest.fn(),
};
const texImg = {
let texImg;

beforeEach(() => {
texImg = {
naturalWidth: 200,
naturalHeight: 100,
};
});

afterEach(() => {
texImg = null;
});

it("should draw an image with the correct properties", () => {
drawTex(ctx, 10, 20, texImg, 50);

expect(ctx.save).toHaveBeenCalled();
Expand All @@ -49,15 +63,6 @@ describe("drawTex", () => {
});

it("should draw an image scaled to 2 if the scale is greater than 2", () => {
const ctx = {
save: jest.fn(),
drawImage: jest.fn(),
restore: jest.fn(),
};
const texImg = {
naturalWidth: 200,
naturalHeight: 100,
};
drawTex(ctx, 10, 20, texImg, 500);

expect(ctx.save).toHaveBeenCalled();
Expand Down
21 changes: 13 additions & 8 deletions test/tools.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { infoMsg, errorMsg } from "../js/tools";

let msgDiv;

beforeEach(() => {
document.body.innerHTML = "<div id='input-message'></div>";
msgDiv = document.getElementById("input-message");
});

describe("infoMsg", () => {
it("should add a message to the input-message div", () => {
document.body.innerHTML = '<div id="input-message"></div>';
infoMsg("Test message");
const msgDiv = document.getElementById("input-message");

expect(msgDiv.classList.contains("mb-20")).toBe(true);
expect(msgDiv.style.color).toBe("gray");
Expand All @@ -13,11 +18,11 @@ describe("infoMsg", () => {
});

describe("errorMsg", () => {
document.body.innerHTML = '<div id="input-message"></div>';
errorMsg("Test error message");
const msgDiv = document.getElementById("input-message");
it("should add an error message to the input-message div", () => {
errorMsg("Test error message");

expect(msgDiv.classList.contains("mb-20")).toBe(true);
expect(msgDiv.style.color).toBe("red");
expect(msgDiv.innerHTML).toBe("<p>ERROR: Test error message</p>");
expect(msgDiv.classList.contains("mb-20")).toBe(true);
expect(msgDiv.style.color).toBe("red");
expect(msgDiv.innerHTML).toBe("<p>ERROR: Test error message</p>");
});
});

0 comments on commit a51b6df

Please sign in to comment.