let ServiceBroker = require("../../src/service-broker");

describe("Test Service handlers", () => {

	let createdHandler = jest.fn();
	let startedHandler = jest.fn();
	let stoppedHandler = jest.fn();
	let eventHandler = jest.fn();

	let broker = new ServiceBroker({ nodeID: "node-1" });

	broker.createService({
		name: "posts",

		created: createdHandler,
		started: startedHandler,
		stopped: stoppedHandler,

		events: {
			"user.*": eventHandler
		}
	});

	it("should called created handler", () => {
		expect(createdHandler).toHaveBeenCalledTimes(1);
	});

	it("should called start handler", () => {
		return broker.start().then(() => {
			expect(startedHandler).toHaveBeenCalledTimes(1);
		});
	});

	it("should called event handler", () => {
		broker.broadcastLocal("user.created", { id: 1, name: "John" });
		expect(eventHandler).toHaveBeenCalledTimes(1);
		expect(eventHandler).toHaveBeenCalledWith({ id: 1, name: "John" }, "node-1", "user.created");
	});

	it("should called stop handler", () => {
		return broker.stop().then(() => {
			expect(stoppedHandler).toHaveBeenCalledTimes(1);
		});
	});
});

describe("Test Service handlers after broker.call", () => {

	let createdHandler = jest.fn();
	let startedHandler = jest.fn();
	let stoppedHandler = jest.fn();
	let eventHandler = jest.fn();

	let broker = new ServiceBroker({ nodeID: "node-1" });

	beforeAll(() => broker.start());

	it("load service dynamically", () => {
		broker.createService({
			name: "posts",

			created: createdHandler,
			started: startedHandler,
			stopped: stoppedHandler,

			events: {
				"user.*": eventHandler
			}
		});
	});

	it("should called created & started handler", () => {
		return broker.Promise.delay(100).then(() => {
			expect(createdHandler).toHaveBeenCalledTimes(1);
			expect(startedHandler).toHaveBeenCalledTimes(1);
		});
	});

	it("should called event handler", () => {
		broker.broadcastLocal("user.created", { id: 1, name: "John" });
		expect(eventHandler).toHaveBeenCalledTimes(1);
		expect(eventHandler).toHaveBeenCalledWith({ id: 1, name: "John" }, "node-1", "user.created");
	});

	it("should called stop handler", () => {
		return broker.stop().then(() => {
			expect(stoppedHandler).toHaveBeenCalledTimes(1);
		});
	});
});

@@ -461,34 +461,32 @@
 %09it(%22should call
-ed
  created handler
@@ -496,16 +496,16 @@
 () =%3E %7B%0A
+
 %09%09expect
@@ -569,18 +569,16 @@
 uld call
-ed
  start h
@@ -703,34 +703,32 @@
 %09it(%22should call
-ed
  event handler%22,
@@ -961,34 +961,32 @@
 %09it(%22should call
-ed
  stop handler%22, 
@@ -1142,20 +1142,21 @@
  broker.
-call
+start
 %22, () =%3E
@@ -1623,18 +1623,16 @@
 uld call
-ed
  created
@@ -1840,18 +1840,16 @@
 uld call
-ed
  event h
@@ -2074,32 +2074,32 @@
 reated%22);%0A%09%7D);%0A%0A
+
 %09it(%22should call
@@ -2098,18 +2098,16 @@
 uld call
-ed
  stop ha

fix typo