xref: /openbmc/webui-vue/tests/unit/views/Settings/Network/ModalMacAddress.spec.js (revision 313d15cf9f14e8a264f97a3370c3be4e494e5e0d)
1import { mount } from '@vue/test-utils';
2import ModalMacAddress from '@/views/Settings/Network/ModalMacAddress';
3import { bootstrapStubs, createModalStub } from '../../../testUtils';
4
5describe('ModalMacAddress.vue', () => {
6  let wrapper;
7
8  beforeEach(() => {
9    wrapper = mount(ModalMacAddress, {
10      props: {
11        modelValue: true,
12        macAddress: '',
13      },
14      global: {
15        stubs: {
16          ...bootstrapStubs,
17          'b-modal': createModalStub(),
18        },
19      },
20    });
21  });
22
23  afterEach(() => {
24    wrapper.unmount();
25  });
26
27  it('should exist', () => {
28    expect(wrapper.exists()).toBe(true);
29  });
30
31  describe('Vuelidate v2 integration', () => {
32    it('should have v$ vuelidate object defined', () => {
33      expect(wrapper.vm.v$).toBeDefined();
34    });
35
36    it('should have form.macAddress validations defined', () => {
37      expect(wrapper.vm.v$.form).toBeDefined();
38      expect(wrapper.vm.v$.form.macAddress).toBeDefined();
39    });
40
41    it('should have required validator for macAddress', () => {
42      expect(wrapper.vm.v$.form.macAddress.required).toBeDefined();
43    });
44
45    it('should have custom macAddress validator', () => {
46      expect(wrapper.vm.v$.form.macAddress.macAddress).toBeDefined();
47    });
48
49    it('should start with clean validation state', () => {
50      expect(wrapper.vm.v$.form.macAddress.$dirty).toBe(false);
51    });
52
53    it('should become dirty after $touch is called', async () => {
54      wrapper.vm.v$.form.macAddress.$touch();
55      await wrapper.vm.$nextTick();
56      expect(wrapper.vm.v$.form.macAddress.$dirty).toBe(true);
57    });
58
59    it('should reset dirty state when $reset is called', async () => {
60      wrapper.vm.v$.form.macAddress.$touch();
61      await wrapper.vm.$nextTick();
62
63      wrapper.vm.v$.$reset();
64      await wrapper.vm.$nextTick();
65      expect(wrapper.vm.v$.form.macAddress.$dirty).toBe(false);
66    });
67  });
68
69  describe('VuelidateMixin integration', () => {
70    it('should have getValidationState method from mixin', () => {
71      expect(typeof wrapper.vm.getValidationState).toBe('function');
72    });
73
74    it('getValidationState should return null when not dirty', () => {
75      const result = wrapper.vm.getValidationState(
76        wrapper.vm.v$.form.macAddress,
77      );
78      expect(result).toBe(null);
79    });
80  });
81
82  describe('Form methods', () => {
83    it('should have handleSubmit method', () => {
84      expect(typeof wrapper.vm.handleSubmit).toBe('function');
85    });
86
87    it('should have resetForm method', () => {
88      expect(typeof wrapper.vm.resetForm).toBe('function');
89    });
90
91    it('resetForm should reset validation state', async () => {
92      wrapper.vm.form.macAddress = '00:1A:2B:3C:4D:5E';
93      wrapper.vm.v$.form.macAddress.$touch();
94      await wrapper.vm.$nextTick();
95
96      wrapper.vm.resetForm();
97      await wrapper.vm.$nextTick();
98
99      expect(wrapper.vm.v$.form.macAddress.$dirty).toBe(false);
100    });
101  });
102
103  describe('Custom MAC address validator (helpers.regex)', () => {
104    it('should use helpers.regex for custom validation', () => {
105      // Verify the validator is a function created by helpers.regex
106      const validator = wrapper.vm.v$.form.macAddress.macAddress;
107      expect(validator).toBeDefined();
108      expect(validator.$params).toBeDefined();
109    });
110  });
111});
112