首页 > 解决方案 > 存根推送器(事件)Javascript

问题描述

pusher用于我的套接字/事件。

我的推送通道被包装在一个对象中,该对象通过键/值管道传递到子组件。所以实际的频道在this.channel.valuewhilethis.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();
  });

问题:

如何创建可以传递我的属性(接收者、名称)并将事件侦听器绑定到它的存根/模拟?

标签: javascriptangularjasmineangular-test

解决方案


我找到了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
    };

推荐阅读