javascript - 存根推送器(事件)Javascript
问题描述
我pusher
用于我的套接字/事件。
我的推送通道被包装在一个对象中,该对象通过键/值管道传递到子组件。所以实际的频道在this.channel.value
whilethis.channel.key
只是一个带键的字符串。
代码
@Input() channel;
...
ngOnInit() {
console.log('channel.value', this.channel.value, typeof this.channel.value);
this.channel.value.bind('client-msg', (msg) => {
...
});
给console.log
了我一个对象e
,我假设这是一个事件,类型为 Object?
channel.value e {callbacks: t, global_callbacks: Array(0), failThrough: ƒ, name: "private-5c49fdc35abdba3fccb362795c49fe6d5cc2ea3ffcb4f895", pusher: t, …}callbacks: t {_callbacks: {…}}failThrough: ƒ (t,n)global_callbacks: []name: "private-5c49fdc35abdba3fccb362795c49fe6d5cc2ea3ffcb4f895"pusher: t {key: "0d4bf48a9d086414a4da", config: {…}, channels: t, global_emitter: t, sessionID: 123906324, …}receiver: "5c49fdc35abdba3fccb36279"receiverName: "Don Peyote"subscribed: truesubscriptionCancelled: falsesubscriptionPending: falsetype: "chat"__proto__: e object
Angular/Jasmine 单元测试:
beforeEach(() => {
fixture = TestBed.createComponent(ChatMessengerComponent);
component = fixture.componentInstance;
component.channel = {
key: 'private-5c49fdc35abdba3fcl1fecb362795c49fe6d5cc2ea3ffcb4f895',
value:
{
receiver: '5c49fdc35abdba3fccb36279',
name: 'DonPeyote'
}
};
fixture.detectChanges();
});
问题:
如何创建可以传递我的属性(接收者、名称)并将事件侦听器绑定到它的存根/模拟?
解决方案
我找到了pusher-js-mock模块。
import { PusherMock } from "pusher-js-mock";
...
describe('ChatMessengerComponent', () => {
let component: ChatMessengerComponent;
let fixture: ComponentFixture<ChatMessengerComponent>;
const pusher = new PusherMock();
...
beforeEach(() => {
fixture = TestBed.createComponent(ChatMessengerComponent);
component = fixture.componentInstance;
const channel = pusher.subscribe('private-5c49fdc35abdba3fcl1fecb362795c49fe6d5cc2ea3ffcb4f895');
component.channel = {
key: 'private-5c49fdc35abdba3fcl1fecb362795c49fe6d5cc2ea3ffcb4f895',
value: channel
};
推荐阅读
- html - 未应用 Http/2 预加载的 css 并被视为未使用
- html - JQuery 可排序和可调整大小的 3 列。使 div 变大会影响下面的整行
- java - 使用 Selenium 在方法中更改按条件查找
- android - 如何在活动之间传递参数?
- flutter - Flutter:如何在 dart 的后台线程上运行函数
- javascript - 如何使用 React 将一组数据正确映射到表上
- java - 如何从android的firebase数据库中的uid获取/检索多个数据?
- optimization - IBM 的 CPLEX cplex.setOut(env.getNullStream()) 无法正常工作
- audio - 分割语音和清音?
- spring - 将列表作为 url 参数发送时返回实体列表