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

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

    const claimMock = {
        claimDms: [
            {
                dmsFk: 1,
                dms: {
                    contentType: 'contentType',
                },
            },
        ],
        client: {
            id: '1',
        },
    };
    beforeAll(() => {
        vm = createWrapper(ClaimPhoto, {
            global: {
                stubs: ['FetchData', 'vue-i18n'],
                mocks: {
                    fetch: vi.fn(),
                },
            },
        }).vm;
    });

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

    describe('deleteDms()', () => {
        it('should delete dms and call quasar notify', async () => {
            vi.spyOn(axios, 'post').mockResolvedValue({ data: true });
            vi.spyOn(vm.quasar, 'notify');

            await vm.deleteDms({ index: 0 });

            expect(axios.post).toHaveBeenCalledWith(
                `ClaimDms/${claimMock.claimDms[0].dmsFk}/removeFile`
            );
            expect(vm.quasar.notify).toHaveBeenCalledWith(
                expect.objectContaining({ type: 'positive' })
            );
        });
    });

    describe('viewDeleteDms()', () => {
        it('should call quasar dialog', async () => {
            vi.spyOn(vm.quasar, 'dialog');

            await vm.viewDeleteDms(1);

            expect(vm.quasar.dialog).toHaveBeenCalledWith(
                expect.objectContaining({
                    componentProps: {
                        title: 'This file will be deleted',
                        icon: 'delete',
                        data: { index: 1 },
                        promise: vm.deleteDms
                    },
                })
            );
        });
    });

    describe('setClaimDms()', () => {
        it('should assign claimDms and client from data', async () => {
            await vm.setClaimDms(claimMock);

            expect(vm.claimDms).toEqual([
                {
                    dmsFk: 1,
                    dms: {
                        contentType: 'contentType',
                    },
                    isVideo: false,
                    url: '/api/Claims/1/downloadFile?access_token=',
                },
            ]);

            expect(vm.client).toEqual(claimMock.client);
        });
    });

    describe('create()', () => {
        it('should upload file and call quasar notify', async () => {
            const files = [{ name: 'firstFile' }];

            vi.spyOn(axios, 'post').mockResolvedValue({ data: true });
            vi.spyOn(vm.quasar, 'notify');
            vi.spyOn(vm.claimDmsRef, 'fetch');

            await vm.create(files);

            expect(axios.post).toHaveBeenCalledWith(
                'claims/1/uploadFile',
                new FormData(),
                expect.objectContaining({
                    params: expect.objectContaining({ hasFile: false }),
                })
            );
            expect(vm.quasar.notify).toHaveBeenCalledWith(
                expect.objectContaining({ type: 'positive' })
            );

            expect(vm.claimDmsRef.fetch).toHaveBeenCalledOnce();
        });
    });
});