vylbot-app/tests/database/entites/UserSetting.test.ts

66 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-12-14 18:04:04 +00:00
import AppDataSource from "../../../src/database/dataSources/appDataSource";
import UserSetting from "../../../src/database/entities/UserSetting";
2024-10-31 16:56:36 +00:00
describe("constructor", () => {
2024-12-14 18:04:04 +00:00
let userSetting: UserSetting;
beforeEach(() => {
userSetting = new UserSetting("userId", "key", "value");
});
test("EXPECT settings to be configured", () => {
expect(userSetting).toMatchSnapshot({
Id: expect.any(String),
WhenCreated: expect.any(Date),
WhenUpdated: expect.any(Date),
});
});
2024-10-31 16:56:36 +00:00
});
describe("UpdateValue", () => {
2024-12-14 18:04:04 +00:00
let userSetting: UserSetting;
beforeEach(() => {
userSetting = new UserSetting("userId", "key", "value");
userSetting.UpdateValue("newValue");
});
test("EXPECT value to be updated", () => {
expect(userSetting.Value).toBe("newValue");
});
2024-10-31 16:56:36 +00:00
});
describe("FetchOneByKey", () => {
2024-12-14 18:04:04 +00:00
let result: UserSetting | null;
let userSetting: UserSetting;
let findOneMock: jest.Mock;
beforeEach(async () => {
userSetting = new UserSetting("userId", "key", "value");
findOneMock = jest.fn().mockResolvedValue(userSetting);
AppDataSource.getRepository = jest.fn().mockReturnValue({
findOne: findOneMock,
});
result = await UserSetting.FetchOneByKey("userId", "key");
});
test("EXPECT getRepository to have been called", () => {
expect(AppDataSource.getRepository).toHaveBeenCalledTimes(1);
expect(AppDataSource.getRepository).toHaveBeenCalledWith(UserSetting);
});
test("EXPECT repository.findOne to have been called", () => {
expect(findOneMock).toHaveBeenCalledTimes(1);
expect(findOneMock).toHaveBeenCalledWith({ where: { UserId: "userId", Key: "key" }, relations: {}});
})
test("EXPECT single entity returned", () => {
expect(result).toBe(userSetting);
});
2024-10-31 16:56:36 +00:00
});