0
0
Fork 0

chore: refs #7702 add tests

This commit is contained in:
Jorge Penadés 2024-09-20 17:38:02 +02:00
parent fc23f9a8dd
commit 6cc8ca6731
2 changed files with 25 additions and 7 deletions

View File

@ -19,6 +19,8 @@ const isLoading = ref(false);
const validate = async () => { const validate = async () => {
const { newPassword, repeatPassword } = passwords.value; const { newPassword, repeatPassword } = passwords.value;
console.log('Validating passwords:', newPassword, repeatPassword);
if (!newPassword) { if (!newPassword) {
notify(t('You must enter a new password'), 'negative'); notify(t('You must enter a new password'), 'negative');
return; return;
@ -29,11 +31,15 @@ const validate = async () => {
} }
try { try {
isLoading.value = true; isLoading.value = true;
console.log('Calling submitFn with:', newPassword);
await props.submitFn(newPassword); await props.submitFn(newPassword);
emit('onSubmit'); emit('onSubmit');
} catch (e) { } catch (e) {
console.error('submitFn failed:', e);
notify('errors.writeRequest', 'negative'); notify('errors.writeRequest', 'negative');
} finally { } finally {
console.log('Entering finally block');
changePassDialog.value.hide(); changePassDialog.value.hide();
isLoading.value = false; isLoading.value = false;
} }

View File

@ -1,11 +1,15 @@
import { createWrapper } from 'app/test/vitest/helper'; import { createWrapper, axios } from 'app/test/vitest/helper';
import VnChangePassword from 'src/components/common/VnChangePassword.vue'; import VnChangePassword from 'src/components/common/VnChangePassword.vue';
import { vi, afterEach, beforeAll, describe, expect, it } from 'vitest'; import { vi, beforeEach, afterEach, beforeAll, describe, expect, it } from 'vitest';
import { Notify } from 'quasar';
describe('VnSmsDialog', () => { describe('VnSmsDialog', () => {
let vm; let vm;
beforeAll(() => { beforeAll(() => {
vi.spyOn(axios, 'get').mockResolvedValue({
data: [],
});
vm = createWrapper(VnChangePassword, { vm = createWrapper(VnChangePassword, {
propsData: { propsData: {
submitFn: vi.fn(), submitFn: vi.fn(),
@ -13,6 +17,10 @@ describe('VnSmsDialog', () => {
}).vm; }).vm;
}); });
beforeEach(() => {
Notify.create = vi.fn();
});
afterEach(() => { afterEach(() => {
vi.clearAllMocks(); vi.clearAllMocks();
}); });
@ -20,10 +28,10 @@ describe('VnSmsDialog', () => {
it('should notify when new password is empty', async () => { it('should notify when new password is empty', async () => {
vm.passwords.newPassword = ''; vm.passwords.newPassword = '';
vm.passwords.repeatPassword = 'password'; vm.passwords.repeatPassword = 'password';
vi.spyOn(vm, 'notify');
await vm.validate(); await vm.validate();
expect(vm.notify).toHaveBeenCalledWith( expect(Notify.create).toHaveBeenCalledWith(
expect.arguments({ expect.objectContaining({
message: 'You must enter a new password', message: 'You must enter a new password',
type: 'negative', type: 'negative',
}) })
@ -33,9 +41,13 @@ describe('VnSmsDialog', () => {
it("should notify when passwords don't match", async () => { it("should notify when passwords don't match", async () => {
vm.passwords.newPassword = 'password1'; vm.passwords.newPassword = 'password1';
vm.passwords.repeatPassword = 'password2'; vm.passwords.repeatPassword = 'password2';
vi.spyOn(vm, 'notify');
await vm.validate(); await vm.validate();
expect(vm.notify).toHaveBeenCalledWith("Passwords don't match", 'negative'); expect(Notify.create).toHaveBeenCalledWith(
expect.objectContaining({
message: `Passwords don't match`,
type: 'negative',
})
);
}); });
it('should call submitFn and emit onSubmit when passwords match', async () => { it('should call submitFn and emit onSubmit when passwords match', async () => {