import { vi, describe, expect, it, beforeAll, afterEach } from 'vitest';
import { createWrapper, axios } from 'app/test/vitest/helper';
import Login from 'pages/Login/LoginMain.vue';

describe('Login', () => {
    let vm;
    beforeAll(() => {
        vm = createWrapper(Login).vm;
    });

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

    it('should successfully set the token into session', async () => {
        const expectedUser = {
            id: 999,
            name: `T'Challa`,
            nickname: 'Black Panther',
            lang: 'en',
            userConfig: {
                darkMode: false,
            },
        };
        vi.spyOn(axios, 'post').mockResolvedValueOnce({ data: { token: 'token' } });
        vi.spyOn(axios, 'get').mockResolvedValue({
            data: { roles: [], user: expectedUser , multimediaToken: {id:'multimediaToken' }},
        });
        vi.spyOn(vm.quasar, 'notify');

        expect(vm.session.getToken()).toEqual('');

        await vm.onSubmit();

        expect(vm.session.getToken()).toEqual('token');
        expect(vm.quasar.notify).toHaveBeenCalledWith(
            expect.objectContaining({ type: 'positive' })
        );
        await vm.session.destroy();
    });

    it('should not set the token into session if any error occurred', async () => {
        vi.spyOn(axios, 'post').mockReturnValue({ data: null });
        vi.spyOn(vm.quasar, 'notify');

        await vm.onSubmit();

        expect(vm.quasar.notify).not.toHaveBeenCalled();
    });
});
