🍔 Added files
This commit is contained in:
113
test/integration/products.service.spec.ts
Normal file
113
test/integration/products.service.spec.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
"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 });
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
85
test/unit/mixins/db.mixin.spec.ts
Normal file
85
test/unit/mixins/db.mixin.spec.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
"use strict";
|
||||
|
||||
import { ServiceBroker } from "moleculer";
|
||||
import DbService from "moleculer-db";
|
||||
import DbMixin from "../../../mixins/db.mixin";
|
||||
|
||||
describe("Test DB mixin", () => {
|
||||
|
||||
describe("Test schema generator", () => {
|
||||
const broker = new ServiceBroker({ logger: false, cacher: "Memory" });
|
||||
|
||||
beforeAll(() => broker.start());
|
||||
afterAll(() => broker.stop());
|
||||
|
||||
it("check schema properties", async () => {
|
||||
const schema = new DbMixin("my-collection").start();
|
||||
|
||||
expect(schema.mixins).toEqual([DbService]);
|
||||
// @ts-ignore
|
||||
expect(schema.adapter).toBeInstanceOf(DbService.MemoryAdapter);
|
||||
expect(schema.started).toBeDefined();
|
||||
expect(schema.events["cache.clean.my-collection"]).toBeInstanceOf(Function);
|
||||
});
|
||||
|
||||
it("check cache event handler", async () => {
|
||||
jest.spyOn(broker.cacher, "clean");
|
||||
|
||||
const schema = new DbMixin("my-collection").start();
|
||||
|
||||
// @ts-ignore
|
||||
await schema.events["cache.clean.my-collection"].call({ broker, fullName: "my-service" });
|
||||
|
||||
expect(broker.cacher.clean).toBeCalledTimes(1);
|
||||
expect(broker.cacher.clean).toBeCalledWith("my-service.*");
|
||||
});
|
||||
|
||||
describe("Check service started handler", () => {
|
||||
|
||||
it("should not call seedDB method", async () => {
|
||||
const schema = new DbMixin("my-collection").start();
|
||||
|
||||
schema.adapter.count = jest.fn(async () => 10);
|
||||
const seedDBFn = jest.fn();
|
||||
|
||||
// @ts-ignore
|
||||
await schema.started.call({ broker, logger: broker.logger, adapter: schema.adapter, seedDB: seedDBFn });
|
||||
|
||||
expect(schema.adapter.count).toBeCalledTimes(1);
|
||||
expect(schema.adapter.count).toBeCalledWith();
|
||||
|
||||
expect(seedDBFn).toBeCalledTimes(0);
|
||||
});
|
||||
|
||||
it("should call seedDB method", async () => {
|
||||
const schema = new DbMixin("my-collection").start();
|
||||
|
||||
schema.adapter.count = jest.fn(async () => 0);
|
||||
const seedDBFn = jest.fn();
|
||||
|
||||
// @ts-ignore
|
||||
await schema.started.call({ broker, logger: broker.logger, adapter: schema.adapter, seedDB: seedDBFn });
|
||||
|
||||
expect(schema.adapter.count).toBeCalledTimes(2);
|
||||
expect(schema.adapter.count).toBeCalledWith();
|
||||
|
||||
expect(seedDBFn).toBeCalledTimes(1);
|
||||
expect(seedDBFn).toBeCalledWith();
|
||||
});
|
||||
});
|
||||
|
||||
it("should broadcast a cache clear event", async () => {
|
||||
const schema = new DbMixin("my-collection").start();
|
||||
|
||||
const ctx = {
|
||||
broadcast: jest.fn(),
|
||||
};
|
||||
|
||||
await schema.methods.entityChanged(null, null, ctx);
|
||||
|
||||
expect(ctx.broadcast).toBeCalledTimes(1);
|
||||
expect(ctx.broadcast).toBeCalledWith("cache.clean.my-collection");
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
40
test/unit/services/greeter.spec.ts
Normal file
40
test/unit/services/greeter.spec.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
"use strict";
|
||||
|
||||
import { Errors, ServiceBroker} from "moleculer";
|
||||
import TestService from "../../../services/greeter.service";
|
||||
|
||||
describe("Test 'greeter' service", () => {
|
||||
const broker = new ServiceBroker({ logger: false });
|
||||
broker.createService(TestService);
|
||||
|
||||
beforeAll(() => broker.start());
|
||||
afterAll(() => broker.stop());
|
||||
|
||||
describe("Test 'greeter.hello' action", () => {
|
||||
|
||||
it("should return with 'Hello Moleculer'", async () => {
|
||||
const res = await broker.call("greeter.hello");
|
||||
expect(res).toBe("Hello Moleculer");
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe("Test 'greeter.welcome' action", () => {
|
||||
|
||||
it("should return with 'Welcome'", async () => {
|
||||
const res = await broker.call("greeter.welcome", { name: "Adam" });
|
||||
expect(res).toBe("Welcome, Adam");
|
||||
});
|
||||
|
||||
it("should reject an ValidationError", async () => {
|
||||
expect.assertions(1);
|
||||
try {
|
||||
await broker.call("greeter.welcome");
|
||||
} catch (err) {
|
||||
expect(err).toBeInstanceOf(Errors.ValidationError);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
180
test/unit/services/products.spec.ts
Normal file
180
test/unit/services/products.spec.ts
Normal file
@@ -0,0 +1,180 @@
|
||||
"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