🐳 Switched to the TS template with the correct docker-compose config
This commit is contained in:
@@ -1,113 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
import { ServiceBroker } from "moleculer";
|
||||
import TestService from "../../services/products.service";
|
||||
|
||||
describe("Test 'products' service", () => {
|
||||
|
||||
describe("Test actions", () => {
|
||||
const broker = new ServiceBroker({ logger: false });
|
||||
const service = broker.createService(TestService);
|
||||
service.seedDB = null; // Disable seeding
|
||||
|
||||
beforeAll(() => broker.start());
|
||||
afterAll(() => broker.stop());
|
||||
|
||||
const record = {
|
||||
name: "Awesome item",
|
||||
price: 999,
|
||||
};
|
||||
let newID: string;
|
||||
|
||||
it("should contains the seeded items", async () => {
|
||||
const res = await broker.call("products.list");
|
||||
expect(res).toEqual({ page: 1, pageSize: 10, rows: [], total: 0, totalPages: 0 });
|
||||
});
|
||||
|
||||
it("should add the new item", async () => {
|
||||
const res: any = await broker.call("products.create", record);
|
||||
expect(res).toEqual({
|
||||
_id: expect.any(String),
|
||||
name: "Awesome item",
|
||||
price: 999,
|
||||
quantity: 0,
|
||||
});
|
||||
newID = res._id;
|
||||
|
||||
const res2 = await broker.call("products.count");
|
||||
expect(res2).toBe(1);
|
||||
});
|
||||
|
||||
it("should get the saved item", async () => {
|
||||
const res = await broker.call("products.get", { id: newID });
|
||||
expect(res).toEqual({
|
||||
_id: expect.any(String),
|
||||
name: "Awesome item",
|
||||
price: 999,
|
||||
quantity: 0,
|
||||
});
|
||||
|
||||
const res2 = await broker.call("products.list");
|
||||
expect(res2).toEqual({
|
||||
page: 1,
|
||||
pageSize: 10,
|
||||
rows: [{ _id: newID, name: "Awesome item", price: 999, quantity: 0 }],
|
||||
total: 1,
|
||||
totalPages: 1,
|
||||
});
|
||||
});
|
||||
|
||||
it("should update an item", async () => {
|
||||
const res = await broker.call("products.update", { id: newID, price: 499 });
|
||||
expect(res).toEqual({
|
||||
_id: expect.any(String),
|
||||
name: "Awesome item",
|
||||
price: 499,
|
||||
quantity: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("should get the updated item", async () => {
|
||||
const res = await broker.call("products.get", { id: newID });
|
||||
expect(res).toEqual({
|
||||
_id: expect.any(String),
|
||||
name: "Awesome item",
|
||||
price: 499,
|
||||
quantity: 0,
|
||||
});
|
||||
});
|
||||
|
||||
it("should increase the quantity", async () => {
|
||||
const res = await broker.call("products.increaseQuantity", { id: newID, value: 5 });
|
||||
expect(res).toEqual({
|
||||
_id: expect.any(String),
|
||||
name: "Awesome item",
|
||||
price: 499,
|
||||
quantity: 5,
|
||||
});
|
||||
});
|
||||
|
||||
it("should decrease the quantity", async () => {
|
||||
const res = await broker.call("products.decreaseQuantity", { id: newID, value: 2 });
|
||||
expect(res).toEqual({
|
||||
_id: expect.any(String),
|
||||
name: "Awesome item",
|
||||
price: 499,
|
||||
quantity: 3,
|
||||
});
|
||||
});
|
||||
|
||||
it("should remove the updated item", async () => {
|
||||
const res = await broker.call("products.remove", { id: newID });
|
||||
expect(res).toBe(1);
|
||||
|
||||
const res2 = await broker.call("products.count");
|
||||
expect(res2).toBe(0);
|
||||
|
||||
const res3 = await broker.call("products.list");
|
||||
expect(res3).toEqual({ page: 1, pageSize: 10, rows: [], total: 0, totalPages: 0 });
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@@ -1,180 +0,0 @@
|
||||
"use strict";
|
||||
|
||||
import { Context, Errors, ServiceBroker } from "moleculer";
|
||||
import TestService from "../../../services/products.service";
|
||||
|
||||
describe("Test 'products' service", () => {
|
||||
|
||||
describe("Test actions", () => {
|
||||
const broker = new ServiceBroker({ logger: false });
|
||||
const service = broker.createService(TestService);
|
||||
|
||||
jest.spyOn(service.adapter, "updateById");
|
||||
jest.spyOn(service, "transformDocuments");
|
||||
jest.spyOn(service, "entityChanged");
|
||||
|
||||
beforeAll(() => broker.start());
|
||||
afterAll(() => broker.stop());
|
||||
|
||||
const record = {
|
||||
_id: "123",
|
||||
name: "Awesome thing",
|
||||
price: 999,
|
||||
quantity: 25,
|
||||
createdAt: Date.now(),
|
||||
};
|
||||
|
||||
describe("Test 'products.increaseQuantity'", () => {
|
||||
|
||||
it("should call the adapter updateById method & transform result", async () => {
|
||||
service.adapter.updateById.mockImplementation(async () => record);
|
||||
service.transformDocuments.mockClear();
|
||||
service.entityChanged.mockClear();
|
||||
|
||||
const res = await broker.call("products.increaseQuantity", {
|
||||
id: "123",
|
||||
value: 10,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
_id: "123",
|
||||
name: "Awesome thing",
|
||||
price: 999,
|
||||
quantity: 25,
|
||||
});
|
||||
|
||||
expect(service.adapter.updateById).toBeCalledTimes(1);
|
||||
expect(service.adapter.updateById).toBeCalledWith("123", { $inc: { quantity: 10 } } );
|
||||
|
||||
expect(service.transformDocuments).toBeCalledTimes(1);
|
||||
expect(service.transformDocuments).toBeCalledWith(expect.any(Context), { id: "123", value: 10 }, record);
|
||||
|
||||
expect(service.entityChanged).toBeCalledTimes(1);
|
||||
expect(service.entityChanged).toBeCalledWith("updated", { _id: "123", name: "Awesome thing", price: 999, quantity: 25 }, expect.any(Context));
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("Test 'products.decreaseQuantity'", () => {
|
||||
|
||||
it("should call the adapter updateById method & transform result", async () => {
|
||||
service.adapter.updateById.mockClear();
|
||||
service.transformDocuments.mockClear();
|
||||
service.entityChanged.mockClear();
|
||||
|
||||
const res = await broker.call("products.decreaseQuantity", {
|
||||
id: "123",
|
||||
value: 10,
|
||||
});
|
||||
expect(res).toEqual({
|
||||
_id: "123",
|
||||
name: "Awesome thing",
|
||||
price: 999,
|
||||
quantity: 25,
|
||||
});
|
||||
|
||||
expect(service.adapter.updateById).toBeCalledTimes(1);
|
||||
expect(service.adapter.updateById).toBeCalledWith("123", { $inc: { quantity: -10 } } );
|
||||
|
||||
expect(service.transformDocuments).toBeCalledTimes(1);
|
||||
expect(service.transformDocuments).toBeCalledWith(expect.any(Context), { id: "123", value: 10 }, record);
|
||||
|
||||
expect(service.entityChanged).toBeCalledTimes(1);
|
||||
expect(service.entityChanged).toBeCalledWith("updated", { _id: "123", name: "Awesome thing", price: 999, quantity: 25 }, expect.any(Context));
|
||||
});
|
||||
|
||||
it("should throw error if params is not valid", async () => {
|
||||
service.adapter.updateById.mockClear();
|
||||
service.transformDocuments.mockClear();
|
||||
service.entityChanged.mockClear();
|
||||
|
||||
expect.assertions(2);
|
||||
try {
|
||||
await broker.call("products.decreaseQuantity", {
|
||||
id: "123",
|
||||
value: -5,
|
||||
});
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(Errors.ValidationError);
|
||||
expect(err.data).toEqual([{
|
||||
action: "products.decreaseQuantity",
|
||||
actual: -5,
|
||||
field: "value",
|
||||
message: "The 'value' field must be a positive number.",
|
||||
nodeID: broker.nodeID,
|
||||
type: "numberPositive",
|
||||
}]);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("Test methods", () => {
|
||||
const broker = new ServiceBroker({ logger: false });
|
||||
const service = broker.createService(TestService);
|
||||
|
||||
jest.spyOn(service.adapter, "insertMany");
|
||||
jest.spyOn(service, "seedDB");
|
||||
|
||||
beforeAll(() => broker.start());
|
||||
afterAll(() => broker.stop());
|
||||
|
||||
describe("Test 'seedDB'", () => {
|
||||
|
||||
it("should be called after service started & DB connected", async () => {
|
||||
expect(service.seedDB).toBeCalledTimes(1);
|
||||
expect(service.seedDB).toBeCalledWith();
|
||||
});
|
||||
|
||||
it("should insert 3 documents", async () => {
|
||||
expect(service.adapter.insertMany).toBeCalledTimes(1);
|
||||
expect(service.adapter.insertMany).toBeCalledWith([
|
||||
{ name: "Samsung Galaxy S10 Plus", quantity: 10, price: 704 },
|
||||
{ name: "iPhone 11 Pro", quantity: 25, price: 999 },
|
||||
{ name: "Huawei P30 Pro", quantity: 15, price: 679 },
|
||||
]);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("Test hooks", () => {
|
||||
const broker = new ServiceBroker({ logger: false });
|
||||
const createActionFn = jest.fn();
|
||||
// @ts-ignore
|
||||
broker.createService(TestService, {
|
||||
actions: {
|
||||
create: {
|
||||
handler: createActionFn,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
beforeAll(() => broker.start());
|
||||
afterAll(() => broker.stop());
|
||||
|
||||
describe("Test before 'create' hook", () => {
|
||||
|
||||
it("should add quantity with zero", async () => {
|
||||
await broker.call("products.create", {
|
||||
id: "111",
|
||||
name: "Test product",
|
||||
price: 100,
|
||||
});
|
||||
|
||||
expect(createActionFn).toBeCalledTimes(1);
|
||||
expect(createActionFn.mock.calls[0][0].params).toEqual({
|
||||
id: "111",
|
||||
name: "Test product",
|
||||
price: 100,
|
||||
quantity: 0,
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user