From 3d8745dc0f8fab9eccb3ce1f0b396a661aa583ab Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 26 Oct 2024 18:12:11 +0100 Subject: [PATCH 1/8] Create UserEffect database entity --- .../Down/01-table-userEffect.sql | 1 + .../Up/01-table-userEffect.sql | 10 ++++ src/database/entities/app/UserEffect.ts | 51 +++++++++++++++++++ .../app/0.9/1729962056556-createUserEffect.ts | 18 +++++++ 4 files changed, 80 insertions(+) create mode 100644 database/0.9/1729962056556-createUserEffect/Down/01-table-userEffect.sql create mode 100644 database/0.9/1729962056556-createUserEffect/Up/01-table-userEffect.sql create mode 100644 src/database/entities/app/UserEffect.ts create mode 100644 src/database/migrations/app/0.9/1729962056556-createUserEffect.ts diff --git a/database/0.9/1729962056556-createUserEffect/Down/01-table-userEffect.sql b/database/0.9/1729962056556-createUserEffect/Down/01-table-userEffect.sql new file mode 100644 index 0000000..ca2a800 --- /dev/null +++ b/database/0.9/1729962056556-createUserEffect/Down/01-table-userEffect.sql @@ -0,0 +1 @@ +DROP TABLE `user_effect`; diff --git a/database/0.9/1729962056556-createUserEffect/Up/01-table-userEffect.sql b/database/0.9/1729962056556-createUserEffect/Up/01-table-userEffect.sql new file mode 100644 index 0000000..17c1811 --- /dev/null +++ b/database/0.9/1729962056556-createUserEffect/Up/01-table-userEffect.sql @@ -0,0 +1,10 @@ +CREATE TABLE `user_effect` ( + `Id` varchar(255) NOT NULL, + `WhenCreated` datetime NOT NULL, + `WhenUpdated` datetime NOT NULL, + `Name` varchar(255) NOT NULL, + `UserId` varchar(255) NOT NULL, + `Unused` int NOT NULL DEFAULT 0, + `WhenExpires` datetime NULL, + PRIMARY KEY (`Id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; diff --git a/src/database/entities/app/UserEffect.ts b/src/database/entities/app/UserEffect.ts new file mode 100644 index 0000000..3b518b1 --- /dev/null +++ b/src/database/entities/app/UserEffect.ts @@ -0,0 +1,51 @@ +import {Column, Entity} from "typeorm"; +import AppBaseEntity from "../../../contracts/AppBaseEntity"; + +@Entity() +export default class UserEffect extends AppBaseEntity { + constructor(name: string, userId: string, unused: number, WhenExpires?: Date) { + super(); + + this.Name = name; + this.UserId = userId; + this.Unused = unused; + this.WhenExpires = WhenExpires; + } + + @Column() + Name: string; + + @Column() + UserId: string; + + @Column() + Unused: number; + + @Column({ nullable: true }) + WhenExpires?: Date; + + public AddUnused(amount: number) { + this.Unused += amount; + } + + public UseEffect(whenExpires: Date): boolean { + if (this.Unused == 0) { + return false; + } + + this.Unused -= 1; + this.WhenExpires = whenExpires; + + return true; + } + + public IsEffectActive(): boolean { + const now = new Date(); + + if (this.WhenExpires && now < this.WhenExpires) { + return true; + } + + return false; + } +} diff --git a/src/database/migrations/app/0.9/1729962056556-createUserEffect.ts b/src/database/migrations/app/0.9/1729962056556-createUserEffect.ts new file mode 100644 index 0000000..f59b7d4 --- /dev/null +++ b/src/database/migrations/app/0.9/1729962056556-createUserEffect.ts @@ -0,0 +1,18 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; +import MigrationHelper from "../../../../helpers/MigrationHelper"; + +export class CreateUserEffect1729962056556 implements MigrationInterface { + + public async up(queryRunner: QueryRunner): Promise { + MigrationHelper.Up("1729962056556-createUserEffect", "0.9", [ + "01-table-userEffect", + ], queryRunner); + } + + public async down(queryRunner: QueryRunner): Promise { + MigrationHelper.Down("1729962056556-createUserEffect", "0.9", [ + "01-table-userEffect", + ], queryRunner); + } + +} -- 2.45.2 From 17d89d4a44b05b230e5af7bb99690a5228feb1ed Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 26 Oct 2024 21:38:23 +0100 Subject: [PATCH 2/8] Create EffectHelper class --- src/database/entities/app/UserEffect.ts | 9 +++++ src/helpers/EffectHelper.ts | 49 +++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/helpers/EffectHelper.ts diff --git a/src/database/entities/app/UserEffect.ts b/src/database/entities/app/UserEffect.ts index 3b518b1..fa1b584 100644 --- a/src/database/entities/app/UserEffect.ts +++ b/src/database/entities/app/UserEffect.ts @@ -1,5 +1,6 @@ import {Column, Entity} from "typeorm"; import AppBaseEntity from "../../../contracts/AppBaseEntity"; +import AppDataSource from "../../dataSources/appDataSource"; @Entity() export default class UserEffect extends AppBaseEntity { @@ -48,4 +49,12 @@ export default class UserEffect extends AppBaseEntity { return false; } + + public static async FetchOneByUserIdAndName(userId: string, name: string): Promise { + const repository = AppDataSource.getRepository(UserEffect); + + const single = await repository.findOne({ where: { UserId: userId, Name: name } }); + + return single; + } } diff --git a/src/helpers/EffectHelper.ts b/src/helpers/EffectHelper.ts new file mode 100644 index 0000000..14c2f43 --- /dev/null +++ b/src/helpers/EffectHelper.ts @@ -0,0 +1,49 @@ +import UserEffect from "../database/entities/app/UserEffect"; + +export default class EffectHelper { + public static async AddEffectToUserInventory(userId: string, name: string, quantity: number = 1) { + let effect = await UserEffect.FetchOneByUserIdAndName(userId, name); + + if (!effect) { + effect = new UserEffect(name, userId, quantity); + } else { + effect.AddUnused(quantity); + } + + await effect.Save(UserEffect, effect); + } + + public static async UseEffect(userId: string, name: string, whenExpires: Date): Promise { + const effect = await UserEffect.FetchOneByUserIdAndName(userId, name); + const now = new Date(); + + if (!effect || effect.Unused == 0) { + return false; + } + + if (effect.WhenExpires && now < effect.WhenExpires) { + return false; + } + + effect.UseEffect(whenExpires); + + await effect.Save(UserEffect, effect); + + return true; + } + + public static async HasEffect(userId: string, name: string): Promise { + const effect = await UserEffect.FetchOneByUserIdAndName(userId, name); + const now = new Date(); + + if (!effect || !effect.WhenExpires) { + return false; + } + + if (now > effect.WhenExpires) { + return false; + } + + return true; + } +} -- 2.45.2 From 2becf6b95aa15c61a165d9b754a8bba259543f0a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 26 Oct 2024 21:48:24 +0100 Subject: [PATCH 3/8] WIP: Start of planning tests --- package.json | 2 +- .../database/entities/app/UserEffect.test.ts | 41 ++++++++++++ tests/helpers/EffectHelper.test.ts | 0 tests/registry.test.ts | 66 ------------------- 4 files changed, 42 insertions(+), 67 deletions(-) create mode 100644 tests/database/entities/app/UserEffect.test.ts create mode 100644 tests/helpers/EffectHelper.test.ts delete mode 100644 tests/registry.test.ts diff --git a/package.json b/package.json index 8b068bc..2509358 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "clean": "rm -rf node_modules/ dist/", "build": "tsc", "start": "node ./dist/bot.js", - "test": "echo true", + "test": "jest", "lint": "eslint .", "lint:fix": "eslint . --fix", "db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js", diff --git a/tests/database/entities/app/UserEffect.test.ts b/tests/database/entities/app/UserEffect.test.ts new file mode 100644 index 0000000..7b5c16b --- /dev/null +++ b/tests/database/entities/app/UserEffect.test.ts @@ -0,0 +1,41 @@ +describe("AddUnused", () => { + test.todo("EXPECT unused to be the amount more"); +}); + +describe("UseEffect", () => { + describe("GIVEN Unused is 0", () => { + test.todo("EXPECT false returned"); + }); + + describe("GIVEN Unused is greater than 0", () => { + test.todo("EXPECT true returned"); + + test.todo("EXPECT Unused to be subtracted by 1"); + + test.todo("EXPECT WhenExpires to be set"); + }); +}); + +describe("IsEffectActive", () => { + describe("GIVEN WhenExpires is null", () => { + test.todo("EXPECT false returned"); + }); + + describe("GIVEN WhenExpires is defined", () => { + describe("AND WhenExpires is in the past", () => { + test.todo("EXPECT false returned"); + }); + + describe("AND WhenExpires is in the future", () => { + test.todo("EXPECT true returned"); + }); + }); +}); + +describe("FetchOneByUserIdAndName", () => { + test.todo("EXPECT entity to be returned"); + + test.todo("EXPECT AppDataSource.getRepository to have been called"); + + test.todo("EXPECT repository.findOne to have been called"); +}); diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts new file mode 100644 index 0000000..e69de29 diff --git a/tests/registry.test.ts b/tests/registry.test.ts deleted file mode 100644 index 71d80db..0000000 --- a/tests/registry.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import {CoreClient} from "../src/client/client"; -import Registry from "../src/registry"; -import fs from "fs"; -import path from "path"; - -describe("RegisterCommands", () => { - test("EXPECT every command in the commands folder to be registered", () => { - const registeredCommands: string[] = []; - - CoreClient.RegisterCommand = jest.fn().mockImplementation((name: string) => { - registeredCommands.push(name); - }); - - Registry.RegisterCommands(); - - const commandFiles = getFilesInDirectory(path.join(process.cwd(), "src", "commands")) - .filter(x => x.endsWith(".ts")); - - for (const file of commandFiles) { - expect(registeredCommands).toContain(file.split("/").pop()!.split(".")[0]); - } - - expect(commandFiles.length).toBe(registeredCommands.length); - }); -}); - -describe("RegisterButtonEvents", () => { - test("EXEPCT every button event in the button events folder to be registered", () => { - const registeredButtonEvents: string[] = []; - - CoreClient.RegisterButtonEvent = jest.fn().mockImplementation((name: string) => { - registeredButtonEvents.push(name); - }); - - Registry.RegisterButtonEvents(); - - const eventFiles = getFilesInDirectory(path.join(process.cwd(), "src", "buttonEvents")) - .filter(x => x.endsWith(".ts")); - - for (const file of eventFiles) { - expect(registeredButtonEvents).toContain(file.split("/").pop()!.split(".")[0].toLowerCase()); - } - - expect(eventFiles.length).toBe(registeredButtonEvents.length); - }); -}); - -function getFilesInDirectory(dir: string): string[] { - let results: string[] = []; - const list = fs.readdirSync(dir); - - list.forEach(file => { - file = path.join(dir, file); - const stat = fs.statSync(file); - - if (stat && stat.isDirectory()) { - /* recurse into a subdirectory */ - results = results.concat(getFilesInDirectory(file)); - } else { - /* is a file */ - results.push(file); - } - }); - - return results; -} -- 2.45.2 From cc6dbe313b83dea86fda7cc48e7803226161e049 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 29 Oct 2024 12:01:08 +0000 Subject: [PATCH 4/8] Plan tests --- tests/helpers/EffectHelper.test.ts | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts index e69de29..d9520a2 100644 --- a/tests/helpers/EffectHelper.test.ts +++ b/tests/helpers/EffectHelper.test.ts @@ -0,0 +1,63 @@ +describe("AddEffectToUserInventory", () => { + describe("GIVEN effect is in database", () => { + test.todo("EXPECT effect to be updated"); + + test.todo("EXPECT effect to be saved"); + }); + + describe("GIVEN effect is not in database", () => { + test.todo("EXPECT effect to be created"); + + test.todo("EXPECT effect to be saved"); + }); +}); + +describe("UseEffect", () => { + describe("GIVEN effect is in database", () => { + describe("GIVEN effect.WhenExpires is undefined", () => { + test.todo("EXPECT false returned"); + }); + + describe("GIVEN now is before effect.WhenExpires", () => { + test.todo("EXPECT false returned"); + }); + + describe("GIVEN currently used effect is inactive", () => { + test.todo("EXPECT UseEffect to be called"); + + test.todo("EXPECT effect to be saved"); + + test.todo("EXPECT true returned"); + }); + }); + + describe("GIVEN effect is not in database", () => { + test.todo("EXPECT false returned"); + }); + + describe("GIVEN effect.Unused is 0", () => { + test.todo("EXPECT false returned"); + }); +}); + +describe("HasEffect", () => { + describe("GIVEN effect is in database", () => { + describe("GIVEN effect.WhenExpires is defined", () => { + describe("GIVEN now is before effect.WhenExpires", () => { + test.todo("EXPECT true returned"); + }); + + describe("GIVEN now is after effect.WhenExpires", () => { + test.todo("EXPECT false returned"); + }); + }); + + describe("GIVEN effect.WhenExpires is undefined", () => { + test.todo("EXPECT false returned"); + }); + }); + + describe("GIVEN effect is not in database", () => { + test.todo("EXPECT false returned"); + }); +}); -- 2.45.2 From e449e1a2b2bc1a31c8984d0a7b2af97b2d9840b7 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 30 Oct 2024 10:37:36 +0000 Subject: [PATCH 5/8] WIP: Create UserEffect tests --- .../database/entities/app/UserEffect.test.ts | 95 +++++++++++++++---- 1 file changed, 79 insertions(+), 16 deletions(-) diff --git a/tests/database/entities/app/UserEffect.test.ts b/tests/database/entities/app/UserEffect.test.ts index 7b5c16b..bcd5b21 100644 --- a/tests/database/entities/app/UserEffect.test.ts +++ b/tests/database/entities/app/UserEffect.test.ts @@ -1,41 +1,104 @@ +import AppDataSource from "../../../../src/database/dataSources/appDataSource"; +import UserEffect from "../../../../src/database/entities/app/UserEffect"; + +let userEffect: UserEffect; +const now = new Date(); + +beforeEach(() => { + userEffect = new UserEffect("name", "userId", 1); +}); + describe("AddUnused", () => { - test.todo("EXPECT unused to be the amount more"); + beforeEach(() => { + userEffect.AddUnused(1); + }); + + test("EXPECT unused to be the amount more", () => { + expect(userEffect.Unused).toBe(2); + }); }); describe("UseEffect", () => { describe("GIVEN Unused is 0", () => { - test.todo("EXPECT false returned"); + let result: boolean; + + beforeEach(() => { + userEffect.Unused = 0; + + result = userEffect.UseEffect(now); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); + + test("EXPECT details not to be changed", () => { + expect(userEffect.Unused).toBe(0); + expect(userEffect.WhenExpires).toBeUndefined(); + }); }); describe("GIVEN Unused is greater than 0", () => { - test.todo("EXPECT true returned"); + let result: boolean; - test.todo("EXPECT Unused to be subtracted by 1"); + beforeEach(() => { + result = userEffect.UseEffect(now); + }); - test.todo("EXPECT WhenExpires to be set"); + test("EXPECT true returned", () => { + expect(result).toBe(true); + }); + + test("EXPECT Unused to be subtracted by 1", () => { + expect(userEffect.Unused).toBe(0); + }); + + test("EXPECT WhenExpires to be set", () => { + expect(userEffect.WhenExpires).toBe(now); + }); }); }); describe("IsEffectActive", () => { describe("GIVEN WhenExpires is null", () => { - test.todo("EXPECT false returned"); + let result: boolean; + + beforeEach(() => { + result = userEffect.IsEffectActive(); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); }); describe("GIVEN WhenExpires is defined", () => { describe("AND WhenExpires is in the past", () => { - test.todo("EXPECT false returned"); + let result: boolean; + + beforeEach(() => { + userEffect.WhenExpires = new Date(now.getTime() - 100); + + result = userEffect.IsEffectActive(); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); }); describe("AND WhenExpires is in the future", () => { - test.todo("EXPECT true returned"); + let result: boolean; + + beforeEach(() => { + userEffect.WhenExpires = new Date(now.getTime() + 100); + + result = userEffect.IsEffectActive(); + }); + + test("EXPECT true returned", () => { + expect(result).toBe(true); + }); }); }); }); - -describe("FetchOneByUserIdAndName", () => { - test.todo("EXPECT entity to be returned"); - - test.todo("EXPECT AppDataSource.getRepository to have been called"); - - test.todo("EXPECT repository.findOne to have been called"); -}); -- 2.45.2 From 60bb39253f98858ff4e4e89fb0b698f15b7b7730 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 31 Oct 2024 17:59:46 +0000 Subject: [PATCH 6/8] WIP Start creating EffectHelper tests --- tests/helpers/EffectHelper.test.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts index d9520a2..9e3b5b0 100644 --- a/tests/helpers/EffectHelper.test.ts +++ b/tests/helpers/EffectHelper.test.ts @@ -1,8 +1,33 @@ +import UserEffect from "../../src/database/entities/app/UserEffect"; +import EffectHelper from "../../src/helpers/EffectHelper"; + describe("AddEffectToUserInventory", () => { describe("GIVEN effect is in database", () => { - test.todo("EXPECT effect to be updated"); + const effectMock = { + AddUnused: jest.fn(), + Save: jest.fn(), + }; - test.todo("EXPECT effect to be saved"); + beforeAll(async () => { + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(effectMock); + + await EffectHelper.AddEffectToUserInventory("userId", "name", 1); + }); + + test("EXPECT database to be fetched", () => { + expect(UserEffect.FetchOneByUserIdAndName).toHaveBeenCalledTimes(1); + expect(UserEffect.FetchOneByUserIdAndName).toHaveBeenCalledWith("userId", "name"); + }); + + test("EXPECT effect to be updated", () => { + expect(effectMock.AddUnused).toHaveBeenCalledTimes(1); + expect(effectMock.AddUnused).toHaveBeenCalledWith(1); + }); + + test("EXPECT effect to be saved", () => { + expect(effectMock.Save).toHaveBeenCalledTimes(1); + expect(effectMock.Save).toHaveBeenCalledWith(UserEffect, effectMock); + }); }); describe("GIVEN effect is not in database", () => { -- 2.45.2 From e28618d7a073451cd3cc3d4573e69b2b834c264d Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 3 Nov 2024 14:08:24 +0000 Subject: [PATCH 7/8] WIP: More EffectHelper tests --- jest.setup.js | 3 +- .../database/entities/app/UserEffect.test.ts | 1 - tests/helpers/EffectHelper.test.ts | 77 +++++++++++++++++-- 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/jest.setup.js b/jest.setup.js index d583d1a..49b6721 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -1,3 +1,4 @@ jest.setTimeout(1 * 1000); // 1 second jest.resetModules(); -jest.resetAllMocks(); \ No newline at end of file +jest.resetAllMocks(); +jest.useFakeTimers("modern"); \ No newline at end of file diff --git a/tests/database/entities/app/UserEffect.test.ts b/tests/database/entities/app/UserEffect.test.ts index bcd5b21..66992ec 100644 --- a/tests/database/entities/app/UserEffect.test.ts +++ b/tests/database/entities/app/UserEffect.test.ts @@ -1,4 +1,3 @@ -import AppDataSource from "../../../../src/database/dataSources/appDataSource"; import UserEffect from "../../../../src/database/entities/app/UserEffect"; let userEffect: UserEffect; diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts index 9e3b5b0..21bb3e6 100644 --- a/tests/helpers/EffectHelper.test.ts +++ b/tests/helpers/EffectHelper.test.ts @@ -31,23 +31,86 @@ describe("AddEffectToUserInventory", () => { }); describe("GIVEN effect is not in database", () => { - test.todo("EXPECT effect to be created"); + beforeAll(async () => { + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(null); + UserEffect.prototype.Save = jest.fn(); - test.todo("EXPECT effect to be saved"); + await EffectHelper.AddEffectToUserInventory("userId", "name", 1); + }); + + test("EXPECT effect to be saved", () => { + expect(UserEffect.prototype.Save).toHaveBeenCalledTimes(1); + expect(UserEffect.prototype.Save).toHaveBeenCalledWith(UserEffect, expect.any(UserEffect)); + }); }); }); describe("UseEffect", () => { describe("GIVEN effect is in database", () => { - describe("GIVEN effect.WhenExpires is undefined", () => { - test.todo("EXPECT false returned"); - }); - describe("GIVEN now is before effect.WhenExpires", () => { - test.todo("EXPECT false returned"); + let result: boolean | undefined; + + // nowMock < whenExpires + const nowMock = new Date(2024, 11, 3, 13, 30); + const whenExpires = new Date(2024, 11, 3, 14, 0); + + const userEffect = { + Unused: 1, + WhenExpires: whenExpires, + }; + + beforeAll(async () => { + jest.setSystemTime(nowMock); + + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(userEffect); + + result = await EffectHelper.UseEffect("userId", "name", new Date()); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); }); describe("GIVEN currently used effect is inactive", () => { + let result: boolean | undefined; + + // nowMock > whenExpires + const nowMock = new Date(2024, 11, 3, 13, 30); + const whenExpires = new Date(2024, 11, 3, 13, 0); + const whenExpiresNew = new Date(2024, 11, 3, 15, 0); + + const userEffect = { + Unused: 1, + WhenExpires: whenExpires, + UseEffect: jest.fn(), + Save: jest.fn(), + }; + + beforeAll(async () => { + jest.setSystemTime(nowMock); + + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(userEffect); + + result = await EffectHelper.UseEffect("userId", "name", whenExpiresNew); + }); + + test("EXPECT UseEffect to be called", () => { + expect(userEffect.UseEffect).toHaveReturnedTimes(1); + expect(userEffect.UseEffect).toHaveBeenCalledWith(whenExpiresNew); + }); + + test("EXPECT effect to be saved", () => { + expect(userEffect.Save).toHaveBeenCalledTimes(1); + expect(userEffect.Save).toHaveBeenCalledWith(UserEffect, userEffect); + }); + + test("EXPECT true returned", () => { + expect(result).toBe(true); + }); + }); + + describe("GIVEN effect.WhenExpires is null", () => { test.todo("EXPECT UseEffect to be called"); test.todo("EXPECT effect to be saved"); -- 2.45.2 From 706951e05f9208b030d6ff5a66ec8dc8b99f549b Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Tue, 5 Nov 2024 19:37:56 +0000 Subject: [PATCH 8/8] Update tests --- jest.setup.js | 2 +- tests/helpers/EffectHelper.test.ts | 152 ++++++++++++++++++++++++++--- 2 files changed, 142 insertions(+), 12 deletions(-) diff --git a/jest.setup.js b/jest.setup.js index 49b6721..8e9ae9a 100644 --- a/jest.setup.js +++ b/jest.setup.js @@ -1,4 +1,4 @@ jest.setTimeout(1 * 1000); // 1 second jest.resetModules(); jest.resetAllMocks(); -jest.useFakeTimers("modern"); \ No newline at end of file +jest.useFakeTimers(); \ No newline at end of file diff --git a/tests/helpers/EffectHelper.test.ts b/tests/helpers/EffectHelper.test.ts index 21bb3e6..343f06c 100644 --- a/tests/helpers/EffectHelper.test.ts +++ b/tests/helpers/EffectHelper.test.ts @@ -58,7 +58,7 @@ describe("UseEffect", () => { Unused: 1, WhenExpires: whenExpires, }; - + beforeAll(async () => { jest.setSystemTime(nowMock); @@ -86,7 +86,7 @@ describe("UseEffect", () => { UseEffect: jest.fn(), Save: jest.fn(), }; - + beforeAll(async () => { jest.setSystemTime(nowMock); @@ -111,20 +111,88 @@ describe("UseEffect", () => { }); describe("GIVEN effect.WhenExpires is null", () => { - test.todo("EXPECT UseEffect to be called"); + let result: boolean | undefined; - test.todo("EXPECT effect to be saved"); + // nowMock > whenExpires + const nowMock = new Date(2024, 11, 3, 13, 30); + const whenExpiresNew = new Date(2024, 11, 3, 15, 0); - test.todo("EXPECT true returned"); + const userEffect = { + Unused: 1, + WhenExpires: null, + UseEffect: jest.fn(), + Save: jest.fn(), + }; + + beforeAll(async () => { + jest.setSystemTime(nowMock); + + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(userEffect); + + result = await EffectHelper.UseEffect("userId", "name", whenExpiresNew); + }); + + test("EXPECT UseEffect to be called", () => { + expect(userEffect.UseEffect).toHaveBeenCalledTimes(1); + expect(userEffect.UseEffect).toHaveBeenCalledWith(whenExpiresNew); + }); + + test("EXPECT effect to be saved", () => { + expect(userEffect.Save).toHaveBeenCalledTimes(1); + expect(userEffect.Save).toHaveBeenCalledWith(UserEffect, userEffect); + }); + + test("EXPECT true returned", () => { + expect(result).toBe(true); + }); }); }); describe("GIVEN effect is not in database", () => { - test.todo("EXPECT false returned"); + let result: boolean | undefined; + + // nowMock > whenExpires + const nowMock = new Date(2024, 11, 3, 13, 30); + const whenExpiresNew = new Date(2024, 11, 3, 15, 0); + + beforeAll(async () => { + jest.setSystemTime(nowMock); + + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(null); + + result = await EffectHelper.UseEffect("userId", "name", whenExpiresNew); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); }); describe("GIVEN effect.Unused is 0", () => { - test.todo("EXPECT false returned"); + let result: boolean | undefined; + + // nowMock > whenExpires + const nowMock = new Date(2024, 11, 3, 13, 30); + const whenExpiresNew = new Date(2024, 11, 3, 15, 0); + + const userEffect = { + Unused: 0, + WhenExpires: null, + UseEffect: jest.fn(), + Save: jest.fn(), + }; + + beforeAll(async () => { + jest.setSystemTime(nowMock); + + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(userEffect); + + result = await EffectHelper.UseEffect("userId", "name", whenExpiresNew); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); }); }); @@ -132,20 +200,82 @@ describe("HasEffect", () => { describe("GIVEN effect is in database", () => { describe("GIVEN effect.WhenExpires is defined", () => { describe("GIVEN now is before effect.WhenExpires", () => { - test.todo("EXPECT true returned"); + let result: boolean | undefined; + + const nowMock = new Date(2024, 11, 3, 13, 30); + const whenExpires = new Date(2024, 11, 3, 15, 0); + + const userEffect = { + WhenExpires: whenExpires, + }; + + beforeAll(async () => { + jest.setSystemTime(nowMock); + + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(userEffect); + + result = await EffectHelper.HasEffect("userId", "name"); + }); + + test("EXPECT true returned", () => { + expect(result).toBe(true); + }); }); describe("GIVEN now is after effect.WhenExpires", () => { - test.todo("EXPECT false returned"); + let result: boolean | undefined; + + const nowMock = new Date(2024, 11, 3, 16, 30); + const whenExpires = new Date(2024, 11, 3, 15, 0); + + const userEffect = { + WhenExpires: whenExpires, + }; + + beforeAll(async () => { + jest.setSystemTime(nowMock); + + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(userEffect); + + result = await EffectHelper.HasEffect("userId", "name"); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); }); }); describe("GIVEN effect.WhenExpires is undefined", () => { - test.todo("EXPECT false returned"); + let result: boolean | undefined; + + const userEffect = { + WhenExpires: undefined, + }; + + beforeAll(async () => { + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(userEffect); + + result = await EffectHelper.HasEffect("userId", "name"); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); }); }); describe("GIVEN effect is not in database", () => { - test.todo("EXPECT false returned"); + let result: boolean | undefined; + + beforeAll(async () => { + UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(null); + + result = await EffectHelper.HasEffect("userId", "name"); + }); + + test("EXPECT false returned", () => { + expect(result).toBe(false); + }); }); }); -- 2.45.2