Initial commit

This commit is contained in:
2023-08-31 21:44:37 -05:00
commit f7a8fe6ea8
23 changed files with 10464 additions and 0 deletions

View 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");
});
});
});

View 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);
});
});
});