xref: /openbmc/webui-vue/tests/unit/views/SecurityAndAccess/UserManagement/ModalUser.spec.js (revision 313d15cf9f14e8a264f97a3370c3be4e494e5e0d)
1import { mount } from '@vue/test-utils';
2import { createStore } from 'vuex';
3import ModalUser from '@/views/SecurityAndAccess/UserManagement/ModalUser';
4import { bootstrapStubs, createModalStub } from '../../../testUtils';
5
6describe('ModalUser.vue', () => {
7  let wrapper;
8  let store;
9
10  const passwordRequirements = {
11    minLength: 8,
12    maxLength: 20,
13  };
14
15  beforeEach(() => {
16    store = createStore({
17      modules: {
18        global: {
19          namespaced: true,
20          getters: {
21            username: () => 'admin',
22          },
23        },
24        userManagement: {
25          namespaced: true,
26          getters: {
27            accountSettings: () => ({
28              accountLockoutDuration: 0,
29            }),
30            accountRoles: () => [
31              { value: 'Administrator', text: 'Administrator' },
32              { value: 'Operator', text: 'Operator' },
33              { value: 'ReadOnly', text: 'ReadOnly' },
34            ],
35          },
36        },
37      },
38    });
39
40    wrapper = mount(ModalUser, {
41      props: {
42        passwordRequirements,
43        modelValue: true,
44      },
45      global: {
46        plugins: [store],
47        stubs: {
48          ...bootstrapStubs,
49          'b-modal': createModalStub(),
50          Alert: true,
51          InputPasswordToggle: {
52            template: '<div><slot /></div>',
53          },
54        },
55      },
56    });
57  });
58
59  afterEach(() => {
60    wrapper.unmount();
61  });
62
63  it('should exist', () => {
64    expect(wrapper.exists()).toBe(true);
65  });
66
67  describe('Vuelidate v2 integration', () => {
68    it('should have v$ vuelidate object defined', () => {
69      expect(wrapper.vm.v$).toBeDefined();
70    });
71
72    it('should have form validations defined', () => {
73      expect(wrapper.vm.v$.form).toBeDefined();
74    });
75
76    it('should have username validation with required', () => {
77      expect(wrapper.vm.v$.form.username).toBeDefined();
78      expect(wrapper.vm.v$.form.username.required).toBeDefined();
79    });
80
81    it('should have username validation with maxLength', () => {
82      expect(wrapper.vm.v$.form.username.maxLength).toBeDefined();
83    });
84
85    it('should have username validation with pattern (helpers.regex)', () => {
86      expect(wrapper.vm.v$.form.username.pattern).toBeDefined();
87    });
88
89    it('should have password validation', () => {
90      expect(wrapper.vm.v$.form.password).toBeDefined();
91    });
92
93    it('should have password minLength validation', () => {
94      expect(wrapper.vm.v$.form.password.minLength).toBeDefined();
95    });
96
97    it('should have password maxLength validation', () => {
98      expect(wrapper.vm.v$.form.password.maxLength).toBeDefined();
99    });
100
101    it('should have passwordConfirmation validation', () => {
102      expect(wrapper.vm.v$.form.passwordConfirmation).toBeDefined();
103    });
104
105    it('should have sameAsPassword validator for passwordConfirmation', () => {
106      expect(
107        wrapper.vm.v$.form.passwordConfirmation.sameAsPassword,
108      ).toBeDefined();
109    });
110
111    it('should have privilege validation', () => {
112      expect(wrapper.vm.v$.form.privilege).toBeDefined();
113      expect(wrapper.vm.v$.form.privilege.required).toBeDefined();
114    });
115  });
116
117  describe('VuelidateMixin integration', () => {
118    it('should have getValidationState method from mixin', () => {
119      expect(typeof wrapper.vm.getValidationState).toBe('function');
120    });
121
122    it('getValidationState should return null when not dirty', () => {
123      const result = wrapper.vm.getValidationState(wrapper.vm.v$.form.username);
124      expect(result).toBe(null);
125    });
126
127    it('getValidationState should return boolean when dirty', async () => {
128      wrapper.vm.v$.form.username.$touch();
129      await wrapper.vm.$nextTick();
130
131      const result = wrapper.vm.getValidationState(wrapper.vm.v$.form.username);
132      expect(typeof result).toBe('boolean');
133    });
134  });
135
136  describe('Validation dirty state tracking', () => {
137    it('should start with clean validation state', () => {
138      expect(wrapper.vm.v$.form.username.$dirty).toBe(false);
139      expect(wrapper.vm.v$.form.password.$dirty).toBe(false);
140    });
141
142    it('should become dirty after $touch is called', async () => {
143      wrapper.vm.v$.form.username.$touch();
144      await wrapper.vm.$nextTick();
145      expect(wrapper.vm.v$.form.username.$dirty).toBe(true);
146    });
147
148    it('should reset dirty state when $reset is called', async () => {
149      wrapper.vm.v$.form.username.$touch();
150      wrapper.vm.v$.form.password.$touch();
151      await wrapper.vm.$nextTick();
152
153      wrapper.vm.v$.$reset();
154      await wrapper.vm.$nextTick();
155
156      expect(wrapper.vm.v$.form.username.$dirty).toBe(false);
157      expect(wrapper.vm.v$.form.password.$dirty).toBe(false);
158    });
159  });
160
161  describe('Form methods', () => {
162    it('should have handleSubmit method', () => {
163      expect(typeof wrapper.vm.handleSubmit).toBe('function');
164    });
165
166    it('should have resetForm method', () => {
167      expect(typeof wrapper.vm.resetForm).toBe('function');
168    });
169
170    it('resetForm should reset validation state', async () => {
171      wrapper.vm.form.username = 'testuser';
172      wrapper.vm.v$.form.username.$touch();
173      await wrapper.vm.$nextTick();
174
175      wrapper.vm.resetForm();
176      await wrapper.vm.$nextTick();
177
178      expect(wrapper.vm.v$.form.username.$dirty).toBe(false);
179      expect(wrapper.vm.form.username).toBe('');
180    });
181  });
182
183  describe('Validators use @vuelidate/validators', () => {
184    it('required validator should have $params', () => {
185      expect(wrapper.vm.v$.form.username.required.$params).toBeDefined();
186    });
187
188    it('maxLength validator should have $params with max value', () => {
189      expect(wrapper.vm.v$.form.username.maxLength.$params).toBeDefined();
190      expect(wrapper.vm.v$.form.username.maxLength.$params.max).toBe(16);
191    });
192
193    it('minLength validator should have $params with min value', () => {
194      expect(wrapper.vm.v$.form.password.minLength.$params).toBeDefined();
195      expect(wrapper.vm.v$.form.password.minLength.$params.min).toBe(8);
196    });
197  });
198});
199