import { randomUUID } from "crypto"; import "reflect-metadata"; import Item from "../../domain/entities/item"; import Stock from "../../domain/entities/stock"; import User from "../../domain/entities/user"; import ItemType from "../../domain/entities/vo/itemType.vo"; import ItemRepository from "../../domain/repositories/item.repository"; import StockRepository from "../../domain/repositories/stock.repository"; import UserRepository from "../../domain/repositories/user.repository"; import AssignStockToUserService from "../../domain/services/assignStockToUser"; import marketplaceContainer from "../../marketplace.injection"; import itemFixture from "../../utils/seed/items.seed"; describe("AssignStockToUserService", () => { const service = marketplaceContainer.resolve("Stock:AssignStockToUserService"); const itemRepo = marketplaceContainer.resolve("Items:ItemRepository"); const stockRepo = marketplaceContainer.resolve("Stock:StockRepository"); const userRepo = marketplaceContainer.resolve("Items:UserRepository"); const items = itemFixture.map( item => new Item(item)); describe("LOTTERY", () => { it("should assign available stock to user", async() =>{ const item = items[0]; expect(item.type).toBe(ItemType.LOTTERY); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo,"find").mockImplementationOnce(async() => new User(1, 6)); jest.spyOn(stockRepo, "find").mockImplementationOnce(async() => undefined); jest.spyOn(stockRepo, "create").mockImplementationOnce(async() => [new Stock(item, "code", 1)]); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isSuccess).toBeTruthy(); }); it("should not assign stock when user have lower level than required by item", async() => { const item = items[0]; expect(item.type).toBe(ItemType.LOTTERY); expect(item.price).toBe(6); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo,"find").mockImplementationOnce(async() => new User(1, 5)); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isFailure).toBeTruthy(); }); it("should not assign stock twice", async() => { const item = items[0]; expect(item.type).toBe(ItemType.LOTTERY); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo,"find").mockImplementationOnce(async() => new User(1, 6)); jest.spyOn(stockRepo, "find").mockImplementationOnce(async() => new Stock(item, "code", 1)); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isFailure).toBeTruthy(); }); }); describe("LIMITED", () => { it("should assign available stock to user", async() =>{ const item = items[2]; expect(item.type).toBe(ItemType.LIMITED); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo,"find").mockImplementationOnce(async() => new User(1, 3)); jest.spyOn(stockRepo, "find").mockImplementationOnce(async() => undefined); jest.spyOn(stockRepo, "assign").mockImplementationOnce(async() => new Stock(item, "code", 1)); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isSuccess).toBeTruthy(); }); it("should not assign stock when user have lower level than required by item", async() => { const item = items[2]; expect(item.type).toBe(ItemType.LIMITED); expect(item.price).toBe(3); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo,"find").mockImplementationOnce(async() => new User(1, 2)); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isFailure).toBeTruthy(); }); it("should not assign stock twice", async() => { const item = items[2]; expect(item.type).toBe(ItemType.LIMITED); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo,"find").mockImplementationOnce(async() => new User(1, 6)); jest.spyOn(stockRepo, "find").mockImplementationOnce(async() => new Stock(item, "code", 1)); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isFailure).toBeTruthy(); }); }); describe("UNLIMITED", () => { const item = new Item({ id: randomUUID(), name: "Unlimited Item", description: "", type: ItemType.UNLIMITED, price: 6, image: "http://store.com/some-image.png", }); it("should assign available stock to user", async() =>{ expect(item.type).toBe(ItemType.UNLIMITED); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo, "find").mockImplementationOnce(async() => new User(1, 7)); jest.spyOn(stockRepo, "find").mockImplementationOnce(async() => undefined); jest.spyOn(stockRepo, "find").mockImplementationOnce(async() => new Stock(item, "code", 1)); jest.spyOn(stockRepo, "create").mockImplementationOnce(async() => [new Stock(item, "code", 1)]); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isSuccess).toBeTruthy(); }); it("should not assign stock when user have lower level than required by item", async() => { expect(item.type).toBe(ItemType.UNLIMITED); expect(item.price).toBe(6); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo,"find").mockImplementationOnce(async() => new User(1, 2)); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isFailure).toBeTruthy(); }); it("should not assign stock twice", async() => { expect(item.type).toBe(ItemType.UNLIMITED); jest.spyOn(itemRepo, "find").mockImplementation(async() => item); jest.spyOn(userRepo,"find").mockImplementationOnce(async() => new User(1, 6)); jest.spyOn(stockRepo, "find").mockImplementationOnce(async() => new Stock(item, "code", 1)); const result = await service.run({ itemId: item.id, userId: 1 }); expect(result.isFailure).toBeTruthy(); }); }); });