Skip to content

Commit

Permalink
test: fix baseIdTogglePanelButton test
Browse files Browse the repository at this point in the history
  • Loading branch information
albertjcuac committed Aug 14, 2024
1 parent 7f5a5e3 commit 310261d
Showing 1 changed file with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { AnyFunction } from '@empathyco/x-utils';
import { mount, Wrapper } from '@vue/test-utils';
import { mount, VueWrapper } from '@vue/test-utils';
import { getDataTestSelector, installNewXPlugin } from '../../../__tests__/utils';
import { XEvent } from '../../../wiring';
import BaseIdTogglePanelButton from '../base-id-toggle-panel-button.vue';
import { nextTick } from 'vue';
import { XPlugin } from '../../../plugins/index';

/**
* Renders the {@link BaseIdTogglePanelButton} with the provided options.
Expand All @@ -12,15 +14,14 @@ import BaseIdTogglePanelButton from '../base-id-toggle-panel-button.vue';
*/
function renderBaseIdToggleButton({
panelId = 'myToggle',
scopedSlots = {
slots = {
default: `<span data-test="default-slot">Panel: ${panelId}</span>`
}
}: RenderBaseIdToggleButtonOptions = {}): RenderBaseIdToggleButtonAPI {
const [, localVue] = installNewXPlugin();
const containerWrapper = mount(BaseIdTogglePanelButton, {
localVue,
propsData: { panelId },
scopedSlots
props: { panelId },
global: { plugins: [installNewXPlugin()] },
slots
});

const wrapper = containerWrapper.findComponent(BaseIdTogglePanelButton);
Expand All @@ -32,8 +33,8 @@ function renderBaseIdToggleButton({
await wrapper.trigger('click');
},
async emit(event: XEvent) {
wrapper.vm.$x.emit(event, true, { id: panelId });
await localVue.nextTick();
XPlugin.bus.emit(event, true, { id: panelId, moduleName: null });
await nextTick();
}
};
}
Expand All @@ -42,7 +43,7 @@ describe('testing BaseIdTogglePanelButton component', () => {
it('emits UserClickedPanelToggleButton with the panel id as payload', async () => {
const { wrapper, panelId, click } = renderBaseIdToggleButton();
const listener = jest.fn();
wrapper.vm.$x.on('UserClickedPanelToggleButton').subscribe(listener);
XPlugin.bus.on('UserClickedPanelToggleButton').subscribe(listener);

await click();

Expand All @@ -52,15 +53,15 @@ describe('testing BaseIdTogglePanelButton component', () => {

it('renders a custom slot content', () => {
const { wrapper } = renderBaseIdToggleButton({
scopedSlots: { default: `<span data-test="custom-slot">Custom slot</span>` }
slots: { default: `<span data-test="custom-slot">Custom slot</span>` }
});

expect(wrapper.find(getDataTestSelector('custom-slot')).text()).toEqual('Custom slot');
});

it('renders a custom slot using the isPanelOpen property', async () => {
const { wrapper, emit } = renderBaseIdToggleButton({
scopedSlots: {
slots: {
default: `
<template #default="{ isPanelOpen }">
<span data-test="custom-slot" v-if="isPanelOpen">Close aside</span>
Expand All @@ -82,12 +83,12 @@ interface RenderBaseIdToggleButtonOptions {
/** Id of the panel to toggle. */
panelId?: string;
/** The scoped slots to render. */
scopedSlots?: Record<string, string | AnyFunction>;
slots?: Record<string, string | AnyFunction>;
}

interface RenderBaseIdToggleButtonAPI {
/** The wrapper for the toggle button component. */
wrapper: Wrapper<Vue>;
wrapper: VueWrapper;
/** The panel id. */
panelId: string;
/** Emits the provided event. */
Expand Down

0 comments on commit 310261d

Please sign in to comment.