import PSteps from '@squirrel/components/p-steps/p-steps.vue'; import { createWrapperFor } from '@tests/vitest.helpers'; import { type VueWrapper } from '@vue/test-utils'; const createSteps = () => [ { value: 'first', text: 'First' }, { value: 'second', text: 'Second' }, { value: 'third', text: 'Third' }, ]; const createWrapper = (props = {}) => { return createWrapperFor(PSteps, { props: { steps: createSteps(), activeStep: null, clickable: false, ...props, }, }); }; describe('PSteps.vue', () => { describe('render tests', () => { it('renders correctly', () => { const wrapper = createWrapper(); expect(wrapper.html()).toMatchSnapshot(); }); it('renders the correct number of steps', () => { const wrapper = createWrapper({ steps: createSteps(), activeStep: 'second', }); const stepElements = wrapper.findAll('.rounded-full.border'); expect(stepElements.length).toBe(createSteps().length); }); it('applies correct classes and data-state for current step', () => { const wrapper = createWrapper({ activeStep: 'second', }); const stepElements = wrapper.findAll('.rounded-full.border'); // First step should be completed (blue text) expect(stepElements[0].classes()).toContain('text-nowrap'); expect(stepElements[0].classes()).toContain('text-p-blue-50'); expect(stepElements[0].classes()).toContain('border-p-blue-50'); expect(stepElements[0].attributes('data-state')).toBe('inactive'); // Second step should be current (blue background) expect(stepElements[1].classes()).toContain('text-nowrap'); expect(stepElements[1].classes()).toContain('bg-p-blue-50'); expect(stepElements[1].classes()).toContain('text-surface'); expect(stepElements[1].classes()).toContain('border-p-blue-50'); expect(stepElements[1].attributes('data-state')).toBe('active'); // Third step should be upcoming (gray) expect(stepElements[2].classes()).toContain('text-nowrap'); expect(stepElements[2].classes()).toContain('text-p-gray-30'); expect(stepElements[2].classes()).toContain('border-p-gray-30'); expect(stepElements[2].attributes('data-state')).toBe('inactive'); }); it('displays step titles', () => { const wrapper = createWrapper({ steps: [ { value: 'step1', text: 'Custom Step 1' }, { value: 'step2', text: 'Custom Step 2' }, { value: 'step3', text: 'Custom Step 3' }, ], activeStep: 'step2', }); const stepElements = wrapper.findAll('.rounded-full.border'); expect(stepElements[0].text()).toBe('Custom Step 1'); expect(stepElements[1].text()).toBe('Custom Step 2'); expect(stepElements[2].text()).toBe('Custom Step 3'); }); it('displays step titles in startCase when the text is missing', () => { const wrapper = createWrapper({ steps: [{ value: 'customStep1' }, { value: 'customStep2' }, { value: 'customStep3' }], activeStep: 'customStep2', }); const stepElements = wrapper.findAll('.rounded-full.border'); expect(stepElements[0].text()).toBe('Custom Step 1'); expect(stepElements[1].text()).toBe('Custom Step 2'); expect(stepElements[2].text()).toBe('Custom Step 3'); }); it('renders the correct number of arrows between steps', () => { const wrapper = createWrapper({ steps: [ { value: 1, text: '1' }, { value: 2, text: '2' }, { value: 3, text: '3' }, { value: 4, text: '4' }, ], activeStep: 'second', }); // There should be 3 arrows for 4 steps const arrowElements = wrapper.findAll('[icon="material-symbols:arrow-right-alt-rounded"]'); expect(arrowElements.length).toBe(3); }); it('applies the correct classes to arrows based on current step', () => { const wrapper = createWrapper({ steps: [ { value: 1, text: '1' }, { value: 2, text: '2' }, { value: 3, text: '3' }, { value: 4, text: '4' }, ], activeStep: 2, }); const arrowElements = wrapper.findAll('[icon="material-symbols:arrow-right-alt-rounded"]'); expect(arrowElements.length).toBe(3); // Arrow between first and second step should be colored expect(arrowElements[0].classes()).toContain('text-p-blue-50'); // Arrow after current step should be gray expect(arrowElements[1].classes()).toContain('text-p-gray-30'); expect(arrowElements[2].classes()).toContain('text-p-gray-30'); }); it('sets cursor-pointer when clickable is true', async () => { const wrapper = createWrapper({ activeStep: 'first', clickable: true }); const stepElements = wrapper.findAll('.rounded-full.border'); stepElements.forEach((stepElement: VueWrapper) => { expect(stepElement.classes()).toContain('cursor-pointer'); }); }); it('sets cursor-pointer clickable steps only', async () => { const wrapper = createWrapper({ steps: [ { value: 1, text: '1', disabled: false }, { value: 2, text: '2', disabled: true }, { value: 3, text: '3', disabled: false }, ], activeStep: 1, clickable: true, }); const stepElements = wrapper.findAll('.rounded-full.border'); // First step should be clickable expect(stepElements[0].classes()).toContain('cursor-pointer'); // Second step should not be clickable expect(stepElements[1].classes()).not.toContain('cursor-pointer'); // Third step should be clickable expect(stepElements[2].classes()).toContain('cursor-pointer'); }); it('does not set cursor-pointer when item disabled is true', async () => { const wrapper = createWrapper({ steps: [ { value: 1, text: '1', disabled: true }, { value: 2, text: '2', disabled: true }, ], activeStep: 1, clickable: true, }); const stepElements = wrapper.findAll('.rounded-full.border'); stepElements.forEach((stepElement: VueWrapper) => { expect(stepElement.classes()).not.toContain('cursor-pointer'); }); }); it('emits click:step when a step is clicked', async () => { const wrapper = createWrapper({ activeStep: 'first', clickable: true }); const stepElements = wrapper.findAll('.rounded-full.border'); await stepElements[1].trigger('click'); expect(wrapper.emitted()['click:step'][0]).toEqual([{ text: 'Second', value: 'second' }, 1]); }); it('updates the active step when activeStep changes', async () => { const wrapper = createWrapper({ activeStep: 'first' }); const getStepElements = () => wrapper.findAll('.rounded-full.border'); expect(getStepElements()[0].classes()).toContain('bg-p-blue-50'); await wrapper.setProps({ activeStep: 'second' }); expect(getStepElements()[1].classes()).toContain('bg-p-blue-50'); }); }); });