2024-10-31 17:59:46 +00:00
|
|
|
import UserEffect from "../../src/database/entities/app/UserEffect";
|
|
|
|
import EffectHelper from "../../src/helpers/EffectHelper";
|
|
|
|
|
2024-10-29 12:01:08 +00:00
|
|
|
describe("AddEffectToUserInventory", () => {
|
|
|
|
describe("GIVEN effect is in database", () => {
|
2024-10-31 17:59:46 +00:00
|
|
|
const effectMock = {
|
|
|
|
AddUnused: jest.fn(),
|
|
|
|
Save: jest.fn(),
|
|
|
|
};
|
2024-10-29 12:01:08 +00:00
|
|
|
|
2024-10-31 17:59:46 +00:00
|
|
|
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);
|
|
|
|
});
|
2024-10-29 12:01:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("GIVEN effect is not in database", () => {
|
2024-11-03 14:08:24 +00:00
|
|
|
beforeAll(async () => {
|
|
|
|
UserEffect.FetchOneByUserIdAndName = jest.fn().mockResolvedValue(null);
|
|
|
|
UserEffect.prototype.Save = jest.fn();
|
|
|
|
|
|
|
|
await EffectHelper.AddEffectToUserInventory("userId", "name", 1);
|
|
|
|
});
|
2024-10-29 12:01:08 +00:00
|
|
|
|
2024-11-03 14:08:24 +00:00
|
|
|
test("EXPECT effect to be saved", () => {
|
|
|
|
expect(UserEffect.prototype.Save).toHaveBeenCalledTimes(1);
|
|
|
|
expect(UserEffect.prototype.Save).toHaveBeenCalledWith(UserEffect, expect.any(UserEffect));
|
|
|
|
});
|
2024-10-29 12:01:08 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("UseEffect", () => {
|
|
|
|
describe("GIVEN effect is in database", () => {
|
|
|
|
describe("GIVEN now is before effect.WhenExpires", () => {
|
2024-11-03 14:08:24 +00:00
|
|
|
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);
|
|
|
|
});
|
2024-10-29 12:01:08 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe("GIVEN currently used effect is inactive", () => {
|
2024-11-03 14:08:24 +00:00
|
|
|
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", () => {
|
2024-10-29 12:01:08 +00:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
});
|