diff --git a/packages/x-components/src/__tests__/bus.dummy.ts b/packages/x-components/src/__tests__/bus.dummy.ts index d71863d126..922f7afb5b 100644 --- a/packages/x-components/src/__tests__/bus.dummy.ts +++ b/packages/x-components/src/__tests__/bus.dummy.ts @@ -1,5 +1,6 @@ import { Dictionary } from '@empathyco/x-utils'; -import { EmittedData, EventPayload, XPriorityBus } from '@empathyco/x-bus'; +import { EmittedData, EventPayload, SubjectPayload, XPriorityBus } from '@empathyco/x-bus'; +import { Subject } from 'rxjs'; import { WireMetadata, XEventsTypes } from '../wiring'; export class XDummyBus< @@ -22,3 +23,17 @@ export class XDummyBus< return Promise.resolve({ event, ...emittedPayload }); } } + +/** + * Dummy function to create an emitter for a given event. + * + * @param event - The event to create the emitter for. + */ +export function dummyCreateEmitter( + this: XPriorityBus, + event: SomeEvent +): void { + this.emitters[event] = new Subject< + SubjectPayload, WireMetadata> + >(); +} diff --git a/packages/x-components/src/components/__tests__/snippet-callbacks.spec.ts b/packages/x-components/src/components/__tests__/snippet-callbacks.spec.ts index 933d709fb7..c84077d76c 100644 --- a/packages/x-components/src/components/__tests__/snippet-callbacks.spec.ts +++ b/packages/x-components/src/components/__tests__/snippet-callbacks.spec.ts @@ -1,18 +1,20 @@ import { mount, Wrapper } from '@vue/test-utils'; -import { installNewXPlugin } from '../../__tests__/utils'; import { baseSnippetConfig } from '../../views/base-config'; import { XEventListeners } from '../../x-installer/api/api.types'; import SnippetCallbacks from '../snippet-callbacks.vue'; +import { bus } from '../../plugins/x-bus'; +import { dummyCreateEmitter } from '../../__tests__/bus.dummy'; + +// Making bus not repeat subjects +jest.spyOn(bus, 'createEmitter' as any).mockImplementation(dummyCreateEmitter.bind(bus) as any); function renderSnippetCallbacks({ callbacks = {} }: RenderSnippetCallbacksOptions = {}): RenderSnippetCallbacksAPI { - const [, localVue] = installNewXPlugin(); const wrapper = mount(SnippetCallbacks, { provide: { - snippetConfig: localVue.observable({ ...baseSnippetConfig, callbacks }) - }, - localVue + snippetConfig: { ...baseSnippetConfig, callbacks } + } }); return { @@ -20,9 +22,11 @@ function renderSnippetCallbacks({ }; } -// TODO: Refactor in EMP-3380 -// eslint-disable-next-line jest/no-disabled-tests -describe.skip('testing SnippetCallbacks component', () => { +describe('testing SnippetCallbacks component', () => { + beforeAll(() => { + jest.useFakeTimers(); + }); + it('executes a callback injected from the snippetConfig', () => { const acceptedAQueryCallback = jest.fn(payload => payload); const clickedColumnPickerCallback = jest.fn(payload => payload); @@ -33,33 +37,30 @@ describe.skip('testing SnippetCallbacks component', () => { } }); - wrapper.vm.$x.emit('UserAcceptedAQuery', 'lego'); + bus.emit('UserAcceptedAQuery', 'lego'); + jest.runAllTimers(); expect(acceptedAQueryCallback).toHaveBeenCalledTimes(1); - expect(acceptedAQueryCallback).toHaveBeenCalledWith('lego', { - location: undefined, - moduleName: null, - replaceable: true - }); + expect(acceptedAQueryCallback).toHaveBeenCalledWith('lego', expect.any(Object)); expect(clickedColumnPickerCallback).not.toHaveBeenCalled(); - wrapper.vm.$x.emit('UserClickedColumnPicker', 1); + bus.emit('UserClickedColumnPicker', 1); + jest.runAllTimers(); expect(acceptedAQueryCallback).toHaveBeenCalledTimes(1); expect(clickedColumnPickerCallback).toHaveBeenCalledTimes(1); - expect(clickedColumnPickerCallback).toHaveBeenCalledWith(1, { - location: undefined, - moduleName: null, - replaceable: true - }); + expect(clickedColumnPickerCallback).toHaveBeenCalledWith(1, expect.any(Object)); + + // Force unsubscribing + wrapper.destroy(); }); it('emits a SnippetCallbackExecuted event when a callback is executed', () => { const acceptedAQueryCallback = jest.fn((payload: string) => payload + '1'); const clickedColumnPickerCallback = jest.fn((payload: number) => payload + 1); - const { wrapper } = renderSnippetCallbacks({ + renderSnippetCallbacks({ callbacks: { UserAcceptedAQuery: acceptedAQueryCallback, UserClickedColumnPicker: clickedColumnPickerCallback @@ -67,9 +68,11 @@ describe.skip('testing SnippetCallbacks component', () => { }); const eventSpy = jest.fn(); - wrapper.vm.$x.on('SnippetCallbackExecuted').subscribe(eventSpy); + bus.on('SnippetCallbackExecuted').subscribe(eventSpy); + + bus.emit('UserAcceptedAQuery', 'playmobil'); + jest.runAllTimers(); - wrapper.vm.$x.emit('UserAcceptedAQuery', 'playmobil'); expect(eventSpy).toHaveBeenCalledTimes(1); expect(eventSpy).toHaveBeenCalledWith({ event: 'UserAcceptedAQuery', @@ -78,7 +81,9 @@ describe.skip('testing SnippetCallbacks component', () => { metadata: expect.any(Object) }); - wrapper.vm.$x.emit('UserClickedColumnPicker', 3); + bus.emit('UserClickedColumnPicker', 3); + jest.runAllTimers(); + expect(eventSpy).toHaveBeenCalledTimes(2); expect(eventSpy).toHaveBeenCalledWith({ event: 'UserClickedColumnPicker', diff --git a/packages/x-components/src/components/snippet-callbacks.vue b/packages/x-components/src/components/snippet-callbacks.vue index 60c74ef361..34d6d37cc4 100644 --- a/packages/x-components/src/components/snippet-callbacks.vue +++ b/packages/x-components/src/components/snippet-callbacks.vue @@ -4,10 +4,11 @@