summaryrefslogtreecommitdiff
path: root/tests/unit/views/SecurityAndAccess/UserManagement/ModalUser.spec.js
blob: c92520975b92eeda9e897426979889132397d470 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { mount } from '@vue/test-utils';
import { createStore } from 'vuex';
import ModalUser from '@/views/SecurityAndAccess/UserManagement/ModalUser';
import { bootstrapStubs, createModalStub } from '../../../testUtils';

describe('ModalUser.vue', () => {
  let wrapper;
  let store;

  const passwordRequirements = {
    minLength: 8,
    maxLength: 20,
  };

  beforeEach(() => {
    store = createStore({
      modules: {
        global: {
          namespaced: true,
          getters: {
            username: () => 'admin',
          },
        },
        userManagement: {
          namespaced: true,
          getters: {
            accountSettings: () => ({
              accountLockoutDuration: 0,
            }),
            accountRoles: () => [
              { value: 'Administrator', text: 'Administrator' },
              { value: 'Operator', text: 'Operator' },
              { value: 'ReadOnly', text: 'ReadOnly' },
            ],
          },
        },
      },
    });

    wrapper = mount(ModalUser, {
      props: {
        passwordRequirements,
        modelValue: true,
      },
      global: {
        plugins: [store],
        stubs: {
          ...bootstrapStubs,
          'b-modal': createModalStub(),
          Alert: true,
          InputPasswordToggle: {
            template: '<div><slot /></div>',
          },
        },
      },
    });
  });

  afterEach(() => {
    wrapper.unmount();
  });

  it('should exist', () => {
    expect(wrapper.exists()).toBe(true);
  });

  describe('Vuelidate v2 integration', () => {
    it('should have v$ vuelidate object defined', () => {
      expect(wrapper.vm.v$).toBeDefined();
    });

    it('should have form validations defined', () => {
      expect(wrapper.vm.v$.form).toBeDefined();
    });

    it('should have username validation with required', () => {
      expect(wrapper.vm.v$.form.username).toBeDefined();
      expect(wrapper.vm.v$.form.username.required).toBeDefined();
    });

    it('should have username validation with maxLength', () => {
      expect(wrapper.vm.v$.form.username.maxLength).toBeDefined();
    });

    it('should have username validation with pattern (helpers.regex)', () => {
      expect(wrapper.vm.v$.form.username.pattern).toBeDefined();
    });

    it('should have password validation', () => {
      expect(wrapper.vm.v$.form.password).toBeDefined();
    });

    it('should have password minLength validation', () => {
      expect(wrapper.vm.v$.form.password.minLength).toBeDefined();
    });

    it('should have password maxLength validation', () => {
      expect(wrapper.vm.v$.form.password.maxLength).toBeDefined();
    });

    it('should have passwordConfirmation validation', () => {
      expect(wrapper.vm.v$.form.passwordConfirmation).toBeDefined();
    });

    it('should have sameAsPassword validator for passwordConfirmation', () => {
      expect(
        wrapper.vm.v$.form.passwordConfirmation.sameAsPassword,
      ).toBeDefined();
    });

    it('should have privilege validation', () => {
      expect(wrapper.vm.v$.form.privilege).toBeDefined();
      expect(wrapper.vm.v$.form.privilege.required).toBeDefined();
    });
  });

  describe('VuelidateMixin integration', () => {
    it('should have getValidationState method from mixin', () => {
      expect(typeof wrapper.vm.getValidationState).toBe('function');
    });

    it('getValidationState should return null when not dirty', () => {
      const result = wrapper.vm.getValidationState(wrapper.vm.v$.form.username);
      expect(result).toBe(null);
    });

    it('getValidationState should return boolean when dirty', async () => {
      wrapper.vm.v$.form.username.$touch();
      await wrapper.vm.$nextTick();

      const result = wrapper.vm.getValidationState(wrapper.vm.v$.form.username);
      expect(typeof result).toBe('boolean');
    });
  });

  describe('Validation dirty state tracking', () => {
    it('should start with clean validation state', () => {
      expect(wrapper.vm.v$.form.username.$dirty).toBe(false);
      expect(wrapper.vm.v$.form.password.$dirty).toBe(false);
    });

    it('should become dirty after $touch is called', async () => {
      wrapper.vm.v$.form.username.$touch();
      await wrapper.vm.$nextTick();
      expect(wrapper.vm.v$.form.username.$dirty).toBe(true);
    });

    it('should reset dirty state when $reset is called', async () => {
      wrapper.vm.v$.form.username.$touch();
      wrapper.vm.v$.form.password.$touch();
      await wrapper.vm.$nextTick();

      wrapper.vm.v$.$reset();
      await wrapper.vm.$nextTick();

      expect(wrapper.vm.v$.form.username.$dirty).toBe(false);
      expect(wrapper.vm.v$.form.password.$dirty).toBe(false);
    });
  });

  describe('Form methods', () => {
    it('should have handleSubmit method', () => {
      expect(typeof wrapper.vm.handleSubmit).toBe('function');
    });

    it('should have resetForm method', () => {
      expect(typeof wrapper.vm.resetForm).toBe('function');
    });

    it('resetForm should reset validation state', async () => {
      wrapper.vm.form.username = 'testuser';
      wrapper.vm.v$.form.username.$touch();
      await wrapper.vm.$nextTick();

      wrapper.vm.resetForm();
      await wrapper.vm.$nextTick();

      expect(wrapper.vm.v$.form.username.$dirty).toBe(false);
      expect(wrapper.vm.form.username).toBe('');
    });
  });

  describe('Validators use @vuelidate/validators', () => {
    it('required validator should have $params', () => {
      expect(wrapper.vm.v$.form.username.required.$params).toBeDefined();
    });

    it('maxLength validator should have $params with max value', () => {
      expect(wrapper.vm.v$.form.username.maxLength.$params).toBeDefined();
      expect(wrapper.vm.v$.form.username.maxLength.$params.max).toBe(16);
    });

    it('minLength validator should have $params with min value', () => {
      expect(wrapper.vm.v$.form.password.minLength.$params).toBeDefined();
      expect(wrapper.vm.v$.form.password.minLength.$params.min).toBe(8);
    });
  });
});