-
Notifications
You must be signed in to change notification settings - Fork 6
test(home): add unit tests for FAQ component #3920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
242 changes: 242 additions & 0 deletions
242
src/modules/home/tests/home.faq.component.unit.tests.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| import { mount } from '@vue/test-utils'; | ||
| import { createVuetify } from 'vuetify'; | ||
| import * as components from 'vuetify/components'; | ||
| import * as directives from 'vuetify/directives'; | ||
| import { beforeEach, describe, it, expect, vi } from 'vitest'; | ||
| import HomeFaqComponent from '../components/home.faq.component.vue'; | ||
|
|
||
| /** | ||
| * Mock child components. | ||
| */ | ||
| vi.mock('../components/utils/home.content.component.vue', () => ({ | ||
| default: { name: 'homeContentComponent', template: '<div class="mock-content" />', props: ['setup'] }, | ||
| })); | ||
|
|
||
| /** | ||
| * Mock theme helpers. | ||
| */ | ||
| vi.mock('../../../lib/helpers/theme', () => ({ | ||
| style: vi.fn(() => ({})), | ||
| overlapStyle: vi.fn(() => ({})), | ||
| colorModeStyle: vi.fn(() => ({})), | ||
| })); | ||
|
|
||
| /** | ||
| * Mock @unhead/vue to prevent SSR side-effects. | ||
| */ | ||
| vi.mock('@unhead/vue', () => ({ | ||
| useHead: vi.fn(), | ||
| })); | ||
|
|
||
| const mockConfig = { | ||
| vuetify: { theme: { rounded: 'rounded-xl', maxWidth: '1200px', flat: false } }, | ||
| }; | ||
|
|
||
| /** | ||
| * Build global options with Vuetify + config global property. | ||
| * @param {object} vuetify - Vuetify instance. | ||
| * @returns {object} Vue test-utils global config. | ||
| */ | ||
| const globalOpts = (vuetify) => ({ | ||
| plugins: [vuetify], | ||
| config: { | ||
| globalProperties: { config: mockConfig }, | ||
| }, | ||
| }); | ||
|
|
||
| /** | ||
| * Create a FAQ setup object with optional overrides. | ||
| * @param {object} [overrides] - Fields to override in the default FAQ. | ||
| * @returns {object} FAQ setup for component props. | ||
| */ | ||
| const makeFaq = (overrides = {}) => ({ | ||
| icon: 'fa-solid fa-circle-question', | ||
| title: 'FAQ', | ||
| content: [ | ||
| { question: 'How does it work?', answer: 'It works **great**.' }, | ||
| { question: 'Is there a free trial?', answer: 'Yes, 14 days.' }, | ||
| ], | ||
| ...overrides, | ||
| }); | ||
|
PierreBrisorgueil marked this conversation as resolved.
PierreBrisorgueil marked this conversation as resolved.
|
||
|
|
||
| describe('HomeFaqComponent', () => { | ||
| let vuetify; | ||
|
|
||
| beforeEach(() => { | ||
| vuetify = createVuetify({ components, directives }); | ||
| }); | ||
|
|
||
| // --- Rendering --- | ||
|
|
||
| it('renders expansion panels for each FAQ item', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq() }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| const panels = wrapper.findAllComponents({ name: 'VExpansionPanel' }); | ||
| expect(panels).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('renders question text in panel titles', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq() }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| const titles = wrapper.findAllComponents({ name: 'VExpansionPanelTitle' }); | ||
| expect(titles[0].text()).toContain('How does it work?'); | ||
| expect(titles[1].text()).toContain('Is there a free trial?'); | ||
| }); | ||
|
|
||
| it('renders subtitle when provided', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq({ subtitle: 'Everything you need to know' }) }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| const contents = wrapper.findAllComponents({ name: 'homeContentComponent' }); | ||
| const subtitleContent = contents.find((c) => c.props('setup')?.subtitle); | ||
| expect(subtitleContent).toBeTruthy(); | ||
| expect(subtitleContent.props('setup').subtitle).toBe('Everything you need to know'); | ||
| }); | ||
|
|
||
| it('does not render subtitle row when not provided', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq() }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| const contents = wrapper.findAllComponents({ name: 'homeContentComponent' }); | ||
| const subtitleContent = contents.find((c) => c.props('setup')?.subtitle); | ||
| expect(subtitleContent).toBeUndefined(); | ||
| }); | ||
|
|
||
| // --- Columns --- | ||
|
|
||
| it('defaults to single column', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq() }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| expect(wrapper.vm.columns).toHaveLength(1); | ||
| expect(wrapper.vm.colWidth).toBe(8); | ||
| }); | ||
|
|
||
| it('splits into 2 columns when configured', () => { | ||
| const setup = makeFaq({ | ||
| columns: 2, | ||
| content: [ | ||
| { question: 'Q1', answer: 'A1' }, | ||
| { question: 'Q2', answer: 'A2' }, | ||
| { question: 'Q3', answer: 'A3' }, | ||
| ], | ||
| }); | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| expect(wrapper.vm.columns).toHaveLength(2); | ||
| expect(wrapper.vm.columns[0]).toHaveLength(2); | ||
| expect(wrapper.vm.columns[1]).toHaveLength(1); | ||
| expect(wrapper.vm.colWidth).toBe(6); | ||
| }); | ||
|
|
||
| // --- Null-safety --- | ||
|
|
||
| it('handles empty content gracefully', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq({ content: [] }) }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| expect(wrapper.vm.columns).toEqual([]); | ||
| const panels = wrapper.findAllComponents({ name: 'VExpansionPanel' }); | ||
| expect(panels).toHaveLength(0); | ||
| }); | ||
|
|
||
| it('handles undefined content gracefully', () => { | ||
| const setup = { title: 'FAQ', icon: 'fa-solid fa-question' }; | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| expect(wrapper.vm.columns).toEqual([]); | ||
| }); | ||
|
|
||
| it('filters out items without question or answer', () => { | ||
| const setup = makeFaq({ | ||
| content: [ | ||
| { question: 'Valid?', answer: 'Yes' }, | ||
| { question: '', answer: 'No question' }, | ||
| { question: 'No answer', answer: '' }, | ||
| { question: 'Also valid?', answer: 'Sure' }, | ||
| ], | ||
| }); | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| const panels = wrapper.findAllComponents({ name: 'VExpansionPanel' }); | ||
| expect(panels).toHaveLength(2); | ||
| }); | ||
|
|
||
| // --- Computed styles --- | ||
|
|
||
| it('uses default variant when not specified', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq() }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| expect(wrapper.vm.variant).toBe('default'); | ||
| }); | ||
|
|
||
| it('uses alternate variant when specified', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq({ variant: 'alternate' }) }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| expect(wrapper.vm.variant).toBe('alternate'); | ||
| }); | ||
|
|
||
| it('computes containerStyle with maxWidth from config', () => { | ||
| const wrapper = mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq() }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
| expect(wrapper.vm.containerStyle['max-width']).toBe('1200px'); | ||
| }); | ||
|
|
||
| // --- JSON-LD --- | ||
|
|
||
| it('calls useHead with FAQPage schema when content exists', async () => { | ||
| const { useHead } = await import('@unhead/vue'); | ||
| useHead.mockClear(); | ||
|
|
||
| mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq() }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
|
|
||
| expect(useHead).toHaveBeenCalledWith({ | ||
| script: [ | ||
| expect.objectContaining({ | ||
| type: 'application/ld+json', | ||
| }), | ||
| ], | ||
| }); | ||
|
|
||
| const call = useHead.mock.calls[0][0]; | ||
| const json = JSON.parse(call.script[0].innerHTML.replace(/\\u003c/g, '<')); | ||
| expect(json['@type']).toBe('FAQPage'); | ||
| expect(json.mainEntity).toHaveLength(2); | ||
| expect(json.mainEntity[0]['@type']).toBe('Question'); | ||
| }); | ||
|
|
||
| it('does not call useHead when content is empty', async () => { | ||
| const { useHead } = await import('@unhead/vue'); | ||
| useHead.mockClear(); | ||
|
|
||
| mount(HomeFaqComponent, { | ||
| props: { setup: makeFaq({ content: [] }) }, | ||
| global: globalOpts(vuetify), | ||
| }); | ||
|
|
||
| expect(useHead).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.