97 lines
2.8 KiB
JavaScript
97 lines
2.8 KiB
JavaScript
jest.mock("../db/pool", () => ({
|
|
query: jest.fn(),
|
|
}));
|
|
|
|
const pool = require("../db/pool");
|
|
const AvailableItems = require("../models/available-item.model");
|
|
|
|
describe("available-item.model", () => {
|
|
beforeEach(() => {
|
|
pool.query.mockReset();
|
|
});
|
|
|
|
test("lists household store items", async () => {
|
|
pool.query.mockResolvedValueOnce({
|
|
rowCount: 1,
|
|
rows: [
|
|
{
|
|
item_id: 55,
|
|
item_name: "milk",
|
|
item_image: null,
|
|
image_mime_type: null,
|
|
item_type: null,
|
|
item_group: null,
|
|
zone: null,
|
|
has_managed_settings: false,
|
|
},
|
|
],
|
|
});
|
|
|
|
const result = await AvailableItems.listAvailableItems(1, 2);
|
|
|
|
expect(result).toEqual([
|
|
expect.objectContaining({
|
|
item_id: 55,
|
|
item_name: "milk",
|
|
}),
|
|
]);
|
|
expect(pool.query).toHaveBeenCalledWith(
|
|
expect.stringContaining("FROM household_store_items hsi"),
|
|
[1, 2]
|
|
);
|
|
});
|
|
|
|
test("creates a household store item when needed", async () => {
|
|
pool.query
|
|
.mockResolvedValueOnce({ rowCount: 0, rows: [] })
|
|
.mockResolvedValueOnce({ rowCount: 1, rows: [{ id: 77, name: "granola" }] })
|
|
.mockResolvedValueOnce({
|
|
rowCount: 1,
|
|
rows: [{ item_id: 77, item_name: "granola" }],
|
|
});
|
|
|
|
const result = await AvailableItems.createAvailableItem(1, 2, "Granola");
|
|
|
|
expect(result).toEqual(expect.objectContaining({ item_id: 77, item_name: "granola" }));
|
|
expect(pool.query).toHaveBeenNthCalledWith(
|
|
2,
|
|
expect.stringContaining("INSERT INTO household_store_items"),
|
|
[1, 2, "granola", "granola"]
|
|
);
|
|
});
|
|
|
|
test("updates household store item images and returns refreshed data", async () => {
|
|
const imageBuffer = Buffer.from("abc");
|
|
pool.query
|
|
.mockResolvedValueOnce({ rowCount: 1, rows: [{ id: 55 }] })
|
|
.mockResolvedValueOnce({
|
|
rowCount: 1,
|
|
rows: [{ item_id: 55, item_name: "milk", item_image: "YWJj", image_mime_type: "image/jpeg" }],
|
|
});
|
|
|
|
const result = await AvailableItems.updateAvailableItem(1, 2, 55, {
|
|
imageBuffer,
|
|
mimeType: "image/jpeg",
|
|
});
|
|
|
|
expect(result).toEqual(expect.objectContaining({ item_id: 55, image_mime_type: "image/jpeg" }));
|
|
expect(pool.query).toHaveBeenNthCalledWith(
|
|
1,
|
|
expect.stringContaining("UPDATE household_store_items"),
|
|
[1, 2, 55, imageBuffer, "image/jpeg"]
|
|
);
|
|
});
|
|
|
|
test("deletes the household store item", async () => {
|
|
pool.query.mockResolvedValueOnce({ rowCount: 1, rows: [] });
|
|
|
|
const deleted = await AvailableItems.deleteAvailableItem(1, 2, 55);
|
|
|
|
expect(deleted).toBe(true);
|
|
expect(pool.query).toHaveBeenCalledWith(
|
|
expect.stringContaining("DELETE FROM household_store_items"),
|
|
[1, 2, 55]
|
|
);
|
|
});
|
|
});
|