0.0.12 • Published 5 years ago
topicsend v0.0.12
topicsend
npm install topicsend
Sample tests;
import { Chat } from "./";
import { mediate } from "./Chat"
import { Message, Mediator } from "./Mediator";
class Users {
users(): PromiseLike<string[]> {
throw new Error("Not implmented");
}
}
test('full static', async () => {
const chat = new Chat();
const topic = chat.topic(Users);
chat.subscribe(Users, {
users() {
return ["john", "bob"]
}
})
let users = await topic.users();
expect(users).toHaveLength(2);
expect(users[0]).toBe("john");
users = await chat.send("Users:users");
expect(users).toHaveLength(2);
expect(users[0]).toBe("john");
})
test('static topic - dynamic handler', async () => {
const chat = new Chat();
const topic = chat.topic(Users);
chat.handle("Users", "users", () => ["john", "bob"]);
const users = await topic.users();
expect(users).toHaveLength(2);
expect(users[0]).toBe("john");
})
test('dynamic', async () => {
const chat = new Chat();
const topic = chat.topic("Users");
chat.handle("Users:users", () => ["john", "bob"]);
const users = await topic.users();
expect(users).toHaveLength(2);
expect(users[0]).toBe("john");
})
test('global', async () => {
const chat = mediate();
const topic = chat.topic("Users");
chat.handle("Users:users", () => ["john", "bob"]);
const users = await topic.users();
expect(users).toHaveLength(2);
expect(users[0]).toBe("john");
})
test('Mediator', async () => {
class LoginResponse {
success = true;
}
class LoginRequest extends Message<LoginResponse> {
}
const mediator = Mediator.instance();
mediator.handle(LoginRequest, (m) => new LoginResponse());
mediator.before(LoginRequest, (m) => console.log("Before"));
mediator.after(LoginRequest, (m) => console.log("After"));
const result = await mediator.send(new LoginRequest());
expect(result.success).toBeTruthy();
})