Initial commit
This commit is contained in:
93
test/unit/mixins/db.mixin.spec.ts
Normal file
93
test/unit/mixins/db.mixin.spec.ts
Normal file
@@ -0,0 +1,93 @@
|
||||
import { Context, ServiceBroker } from "moleculer";
|
||||
import type { ServiceEventHandler, StartedStoppedHandler } 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());
|
||||
|
||||
test("check schema properties", () => {
|
||||
const schema = DbMixin("my-collection");
|
||||
|
||||
expect(schema.mixins).toEqual([DbService]);
|
||||
expect(schema.adapter).toBeInstanceOf(DbService.MemoryAdapter);
|
||||
expect(schema.started).toBeDefined();
|
||||
expect(schema.events!["cache.clean.my-collection"]).toBeInstanceOf(Function);
|
||||
});
|
||||
|
||||
test("check cache event handler", async () => {
|
||||
jest.spyOn(broker.cacher!, "clean");
|
||||
|
||||
const schema = DbMixin("my-collection");
|
||||
|
||||
await (schema.events!["cache.clean.my-collection"] as ServiceEventHandler).call(
|
||||
{
|
||||
broker,
|
||||
fullName: "my-service",
|
||||
},
|
||||
Context.create(broker),
|
||||
);
|
||||
|
||||
expect(broker.cacher!.clean).toHaveBeenCalledTimes(1);
|
||||
expect(broker.cacher!.clean).toHaveBeenCalledWith("my-service.*");
|
||||
});
|
||||
|
||||
describe("Check service started handler", () => {
|
||||
test("should not call seedDB method", async () => {
|
||||
const schema = DbMixin("my-collection");
|
||||
|
||||
schema.adapter!.count = jest.fn(() => Promise.resolve(10));
|
||||
const seedDBFn = jest.fn();
|
||||
|
||||
await (schema.started as StartedStoppedHandler).call({
|
||||
broker,
|
||||
logger: broker.logger,
|
||||
adapter: schema.adapter,
|
||||
seedDB: seedDBFn,
|
||||
});
|
||||
|
||||
expect(schema.adapter!.count).toHaveBeenCalledTimes(1);
|
||||
expect(schema.adapter!.count).toHaveBeenCalledWith();
|
||||
|
||||
expect(seedDBFn).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
test("should call seedDB method", async () => {
|
||||
const schema = DbMixin("my-collection");
|
||||
|
||||
schema.adapter!.count = jest.fn(() => Promise.resolve(0));
|
||||
const seedDBFn = jest.fn();
|
||||
|
||||
await (schema.started as StartedStoppedHandler).call({
|
||||
broker,
|
||||
logger: broker.logger,
|
||||
adapter: schema.adapter,
|
||||
seedDB: seedDBFn,
|
||||
});
|
||||
|
||||
expect(schema.adapter!.count).toHaveBeenCalledTimes(2);
|
||||
expect(schema.adapter!.count).toHaveBeenCalledWith();
|
||||
|
||||
expect(seedDBFn).toHaveBeenCalledTimes(1);
|
||||
expect(seedDBFn).toHaveBeenCalledWith();
|
||||
});
|
||||
});
|
||||
|
||||
test("should broadcast a cache clear event", async () => {
|
||||
const schema = DbMixin("my-collection");
|
||||
|
||||
const ctx = Context.create(broker);
|
||||
|
||||
jest.spyOn(ctx, "broadcast");
|
||||
|
||||
await schema.methods!.entityChanged!("update", null, ctx);
|
||||
|
||||
expect(ctx.broadcast).toHaveBeenCalledTimes(1);
|
||||
expect(ctx.broadcast).toHaveBeenCalledWith("cache.clean.my-collection");
|
||||
});
|
||||
});
|
||||
});
|
||||
28
test/unit/services/greeter.spec.ts
Normal file
28
test/unit/services/greeter.spec.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
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", () => {
|
||||
test("should return with 'Hello Moleculer'", async () => {
|
||||
const res = await broker.call("greeter.hello");
|
||||
expect(res).toBe("Hello Moleculer");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Test 'greeter.welcome' action", () => {
|
||||
test("should return with 'Welcome'", async () => {
|
||||
const res = await broker.call("greeter.welcome", { name: "Adam" });
|
||||
expect(res).toBe("Welcome, Adam");
|
||||
});
|
||||
|
||||
test("should reject an ValidationError", async () => {
|
||||
await expect(broker.call("greeter.welcome")).rejects.toThrow(Errors.ValidationError);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user