Skip to content

Commit a650558

Browse files
Fix intermittent ModalDialog unit test failures
Refactor `ModalDialog` unit tests to use `shallowMount` consistently. Previously, tests sometimes failed due to the `UseSvgLoader` hook attempting icon loads during component teardown, which occasionally led led to errors when the `window` object became unavailable. By switching to `shallowMount`, tests no longer deeply render child components, mitigating the risk of such errors and aligning with unit testing best practices. Additionally, this commit sets a default value for the `modelValue` prop in test setups to address Vue warnings about missing required props, further stabilizing the test environment.
1 parent b16e136 commit a650558

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

tests/unit/presentation/components/Shared/Modal/ModalDialog.spec.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { describe, it, expect } from 'vitest';
2-
import { shallowMount, mount } from '@vue/test-utils';
2+
import { shallowMount } from '@vue/test-utils';
33
import ModalDialog from '@/presentation/components/Shared/Modal/ModalDialog.vue';
4-
import ModalContainer from '@/presentation/components/Shared/Modal/ModalContainer.vue';
54

65
const DOM_CLOSE_BUTTON_SELECTOR = '.dialog__close-button';
76
const MODAL_CONTAINER_COMPONENT_NAME = 'ModalContainer';
@@ -19,18 +18,19 @@ describe('ModalDialog.vue', () => {
1918
describe(`binds the visibility flag ${MODAL_CONTAINER_COMPONENT_NAME}`, () => {
2019
it('given true', () => {
2120
// arrange & act
22-
const wrapper = mountComponent({ modelValue: true, deepMount: true });
21+
const wrapper = mountComponent({ modelValue: true });
2322

2423
// assert
25-
const modalContainerWrapper = wrapper.findComponent(ModalContainer);
24+
const modalContainerWrapper = wrapper.findComponent({ name: MODAL_CONTAINER_COMPONENT_NAME });
25+
2626
expect(modalContainerWrapper.props('modelValue')).to.equal(true);
2727
});
2828
it('given false', () => {
2929
// arrange & act
30-
const wrapper = mountComponent({ modelValue: false, deepMount: true });
30+
const wrapper = mountComponent({ modelValue: false });
3131

3232
// assert
33-
const modalContainerWrapper = wrapper.findComponent(ModalContainer);
33+
const modalContainerWrapper = wrapper.findComponent({ name: MODAL_CONTAINER_COMPONENT_NAME });
3434
expect(modalContainerWrapper.props('modelValue')).to.equal(false);
3535
});
3636
});
@@ -65,17 +65,20 @@ describe('ModalDialog.vue', () => {
6565
function mountComponent(options?: {
6666
readonly modelValue?: boolean,
6767
readonly slotHtml?: string,
68-
readonly deepMount?: boolean,
6968
}) {
70-
const mountFunction = options?.deepMount === true ? mount : shallowMount;
71-
const wrapper = mountFunction(ModalDialog, {
72-
props: options?.modelValue !== undefined ? { modelValue: options?.modelValue } : undefined,
69+
const wrapper = shallowMount(ModalDialog, {
70+
props: {
71+
modelValue: options?.modelValue !== undefined
72+
? options?.modelValue
73+
: true /* provide default value to required property */,
74+
},
7375
slots: options?.slotHtml !== undefined ? { default: options?.slotHtml } : undefined,
7476
global: {
75-
stubs: options?.deepMount === true ? undefined : {
77+
stubs: {
7678
[MODAL_CONTAINER_COMPONENT_NAME]: {
7779
name: MODAL_CONTAINER_COMPONENT_NAME,
7880
template: '<slot />',
81+
props: ['modelValue'],
7982
},
8083
},
8184
},

0 commit comments

Comments
 (0)