存根推送(事件)JavaScript

时间:2019-03-27 13:33:53

标签: javascript angular jasmine angular-test angular-testing

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

我的推送程序通道包装在一个对象中,该对象通过键/值管道传递到子组件。因此,实际的频道位于this.channel.value中,而this.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

角度/茉莉花单元测试:

  beforeEach(() => {
    fixture = TestBed.createComponent(ChatMessengerComponent);
    component = fixture.componentInstance;
    component.channel = {
      key: 'private-5c49fdc35abdba3fcl1fecb362795c49fe6d5cc2ea3ffcb4f895',
      value:
        {
          receiver: '5c49fdc35abdba3fccb36279',
          name: 'DonPeyote'
        }
    };
    fixture.detectChanges();
  });

问题:

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

1 个答案:

答案 0 :(得分:0)

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