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); + }); }); });