import PActionBar from '@squirrel/components/p-action-bar/p-action-bar.vue';
import { createWrapperFor } from '@tests/vitest.helpers';
import { type VueWrapper } from '@vue/test-utils';
import { defineComponent } from 'vue';
const createPDropdownStub = () => {
return defineComponent({
name: 'PDropdownStub',
data() {
return {
show: false,
};
},
async mounted() {
const btn = this.$el.querySelector('button');
btn.addEventListener('click', this.toggleShow);
},
beforeUnmount() {
const btn = this.$el.querySelector('button');
btn.removeEventListener('click', this.toggleShow);
},
methods: {
toggleShow() {
this.show = !this.show;
},
hide() {
this.show = false;
},
},
template: `
`,
});
};
const createComponentWrapper = (args = {}) =>
createWrapperFor(PActionBar, {
global: {
stubs: {
Teleport: {
template: '
',
},
PDropdown: createPDropdownStub(),
},
},
...args,
});
describe('PActionBar.vue', () => {
it.each([true, false])('shows/hides when the show prop is %s', async (val) => {
const wrapper = createComponentWrapper({
props: { show: false, label: 'Hello World', actions: [{ label: 'Say Hi', name: 'greet' }] },
});
await wrapper.setProps({ show: val });
const mainDiv = wrapper.find('.teleport-stub').find('.fixed.bottom-6');
expect(mainDiv.exists()).toBe(val);
});
it('renders correctly', async () => {
const wrapper = createComponentWrapper({
props: {
show: true,
label: 'Hello World',
actions: [
{ label: 'Say Hi', name: 'greet' },
{ label: 'Action menu', subActions: [{ label: 'Say Bye', name: 'bye' }] },
],
},
});
const mainDiv = wrapper.find('.teleport-stub').find('.fixed.bottom-6');
expect(mainDiv.exists()).toBe(true);
expect(mainDiv.classes()).toEqual(['fixed', 'bottom-6', 'left-1/2', 'z-[100]', '-translate-x-2/4']);
const actionBarDiv = mainDiv.find('.bg-p-purple-60');
expect(actionBarDiv.exists()).toBe(true);
expect(actionBarDiv.classes()).toEqual([
'flex',
'h-12',
'w-max',
'select-none',
'items-center',
'rounded-lg',
'bg-p-purple-60',
'px-2',
'text-sm',
'font-medium',
'text-white',
]);
const buttons = wrapper.findAllComponents({ name: 'PBtn' });
buttons.forEach((button: VueWrapper) => {
expect(button.find('.slot-wrapper > div').classes()).toEqual(['flex', 'items-center', 'gap-2', 'px-1']);
});
const paragraph = wrapper.find('p');
expect(paragraph.text()).toContain('Hello World');
const dismissIcon = wrapper.find('i.x-white-icon');
expect(dismissIcon.exists()).toBe(true);
expect(actionBarDiv.find('.text-xs').text()).toBe('Clear All');
const actionBtn = buttons.find((button: VueWrapper) => button.text().includes('Say Hi'));
expect(actionBtn).toBeDefined();
expect(actionBtn.text()).toContain('Say Hi');
});
it('renders p-icons', async () => {
const wrapper = createComponentWrapper({
props: {
show: true,
label: 'Hello World',
actions: [
{ label: 'Say Hi', name: 'greet', icon: 'test-icon-1' },
{
label: 'Action menu',
icon: 'test-icon-2',
subActions: [{ label: 'Say Bye', name: 'bye', icon: 'test-icon-3' }],
},
],
},
});
const mainDiv = wrapper.find('.teleport-stub').find('.fixed.bottom-6');
mainDiv.find('.p-dropdown-stub > button').trigger('click');
await wrapper.vm.$nextTick();
const icons = mainDiv.findAll('iconify-icon');
expect(icons.length).toBe(3);
expect(icons[0].attributes().icon).toBe('test-icon-1');
expect(icons[1].attributes().icon).toBe('test-icon-2');
expect(icons[2].attributes().icon).toBe('test-icon-3');
});
it('attrs fall through', async () => {
const wrapper = createComponentWrapper({
props: {
show: true,
label: 'Hello World',
actions: [
{ label: 'Say Hi', name: 'greet' },
{ label: 'Action menu', subActions: [{ label: 'Say Bye', name: 'bye' }] },
],
'data-testid': 'TestId',
'custom-attr': 'Test custom attr',
class: 'test-class',
},
});
const actionBar = wrapper.find('.teleport-stub').find('div');
expect(actionBar.attributes('role')).toBe('alertdialog');
expect(actionBar.attributes('data-testid')).toBe('TestId');
expect(actionBar.attributes('custom-attr')).toBe('Test custom attr');
expect(actionBar.classes()).toContain('test-class');
});
it('emits a click:dismiss event', async () => {
const wrapper = createComponentWrapper({
props: { show: true, label: 'Hello World', actions: [{ label: 'Say Hi', name: 'greet' }] },
});
const button = wrapper.find('button.inline-flex');
await button.trigger('click');
expect(wrapper.emitted()['click:dismiss']).toBeTruthy();
});
it('emits a click:action event with the correct name', async () => {
const wrapper = createComponentWrapper({
props: {
show: true,
label: 'Hello World',
actions: [
{ label: 'Say Hi', name: 'greet' },
{ label: 'Action menu', subActions: [{ label: 'Say Bye', name: 'bye' }] },
],
},
});
const button = wrapper.findByText('Say Hi', 'button');
await button.trigger('click');
expect(wrapper.emitted()['click:action'][0]).toEqual(['greet']);
const actionMenu = wrapper.findByText('Action menu', 'button');
await actionMenu.trigger('click');
const subAction = wrapper.findByText('Say Bye', 'button');
await subAction.trigger('click');
expect(wrapper.emitted()['click:action'][1]).toEqual(['bye']);
});
});