import React from 'react';
import { Identity } from '@theorchard/suite-identity';
import { renderInAppContextLite, RenderResult } from '../../../../../lib/test-utils/render';
import * as resendMFA from '../../../../mutations/resendMFA';
import * as resendPassword from '../../../../mutations/resendPassword';
import { MainNavUserModalFooter } from '../mainNavUserModalFooter';

describe('<MainNavUserModalFooter>', () => {
    const notEmployeeIdentity = {
        isEmployee: false,
        email: 'test@noemployee.com',
        authId: '123456798',
    };
    let r: RenderResult;
    const mockResetMFA = vi.fn();
    const mockResetPassword = vi.fn();

    const renderComponent = (identity?: Partial<Identity>) =>
        renderInAppContextLite(<MainNavUserModalFooter />, { identity });

    beforeEach(() => {
        vi.spyOn(resendPassword, 'useResetUserPasswordMutation').mockReturnValue(mockResetPassword);
        vi.spyOn(resendMFA, 'useResetUserMFAMutation').mockReturnValue(mockResetMFA);
    });

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

    describe('identity is not an employee', () => {
        beforeEach(() => {
            r = renderComponent(notEmployeeIdentity);
        });

        test('renders the reset password button and calls mutation on click', () => {
            const btn = r.getByTestId('btn-resetPassword');
            expect(btn).toBeInTheDocument();

            btn.click();
            expect(mockResetPassword).toHaveBeenCalled();
        });

        test('renders the reset MFA button and calls mutation on click', () => {
            const btn = r.getByTestId('btn-resetMFA');
            expect(btn).toBeInTheDocument();

            btn.click();
            expect(mockResetMFA).toHaveBeenCalled();
        });
    });

    test('does not render the reset password button if employee', () => {
        r = renderComponent({
            ...notEmployeeIdentity,
            isEmployee: true,
        });
        expect(r.queryByTestId('btn-resetPassword')).not.toBeInTheDocument();
    });

    test('does not render the reset MFA button if no authId', () => {
        r = renderComponent({
            ...notEmployeeIdentity,
            authId: undefined,
        });
        expect(r.queryByTestId('btn-resetMFA')).not.toBeInTheDocument();
    });
});
