Sapthika
Sapthika

Reputation: 31

how to test global event bus which is inside mounted method in jest

export default class Test extends Vue {

 mounted() {
    this.getData();
    eventBus.$on("get", (id: string) => {
      this.displayData(id);
      
    });
}

getData(){

return "hello"

}

displayData(id){

}

I have written spec for the Test component like below. I have used Global Event Bus and trying to check event is emitted or not.

const EventBus = new Vue();
const GlobalPlugins = {
    install(v:any) {
        // Event bus
        v.prototype.eventBus = EventBus;
    }
};
 const localVue = createLocalVue()
  localVue.prototype.eventBus = createLocalVue();
   localVue.use(GlobalPlugins);

  describe('Test TestSuite', () => {
    let wrapper: any
    let TestObj: any;

    beforeEach(() => {
      const mocks = {
        eventBus: {
            $on: jest.fn(),
            $emit: jest.fn()
            }
        };
    wrapper = mount(Test, {
        mocks,
        localVue,
        router
       
      });
      console.log(eventBus.$emit) ---> returns null
      TestObj = wrapper.findComponent(Test).vm;
      console.log(eventBus.$emit) ---> returns null
    });

Here, I tried to check the get event is emitted or not. but it gives null object only.

Upvotes: 0

Views: 724

Answers (1)

Sapthika
Sapthika

Reputation: 31

Problem solved by emitting event and mocking components before mounting.

Upvotes: -1

Related Questions