import { createWrapper, axios } from 'app/test/vitest/helper';
import VnChangePassword from 'src/components/common/VnChangePassword.vue';
import { vi, beforeEach, afterEach, beforeAll, describe, expect, it } from 'vitest';
import { Notify } from 'quasar';

describe('VnSmsDialog', () => {
    let vm;

    beforeAll(() => {
        vi.spyOn(axios, 'get').mockResolvedValue({
            data: [],
        });
        vm = createWrapper(VnChangePassword, {
            propsData: {
                submitFn: vi.fn(),
            },
        }).vm;
    });

    beforeEach(() => {
        Notify.create = vi.fn();
    });

    afterEach(() => {
        vi.clearAllMocks();
    });

    it('should notify when new password is empty', async () => {
        vm.passwords.newPassword = '';
        vm.passwords.repeatPassword = 'password';

        await vm.validate();
        expect(Notify.create).toHaveBeenCalledWith(
            expect.objectContaining({
                message: 'You must enter a new password',
                type: 'negative',
            })
        );
    });

    it("should notify when passwords don't match", async () => {
        vm.passwords.newPassword = 'password1';
        vm.passwords.repeatPassword = 'password2';
        await vm.validate();
        expect(Notify.create).toHaveBeenCalledWith(
            expect.objectContaining({
                message: `Passwords don't match`,
                type: 'negative',
            })
        );
    });

    describe('if passwords match', () => {
        it('should call submitFn and emit password', async () => {
            vm.passwords.newPassword = 'password';
            vm.passwords.repeatPassword = 'password';
            await vm.validate();
            expect(vm.props.submitFn).toHaveBeenCalledWith('password', undefined);
        });

        it('should call submitFn and emit password and old password', async () => {
            vm.passwords.newPassword = 'password';
            vm.passwords.repeatPassword = 'password';
            vm.passwords.oldPassword = 'oldPassword';

            await vm.validate();
            expect(vm.props.submitFn).toHaveBeenCalledWith('password', 'oldPassword');
        });
    });
});
