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

describe('WagonCreate', () => {
    let vmEdit, vmCreate;
    const entityId = 1;

    beforeAll(() => {
        vmEdit = createWrapper(WagonCreate, {
            propsData: {
                id: entityId,
            },
        }).vm;
        vmCreate = createWrapper(WagonCreate).vm;
    });

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

    describe('onSubmit()', () => {
        it('should create a wagon', async () => {
            vi.spyOn(axios, 'patch').mockResolvedValue({ data: true });
            vmCreate.wagon = {
                label: 1234,
                plate: 'MOCK PLATE',
                volume: 50,
                typeFk: 1,
            };

            await vmCreate.onSubmit();

            expect(axios.patch).toHaveBeenCalledWith(`Wagons`, vmCreate.wagon);
        });

        it('should update a wagon', async () => {
            vi.spyOn(axios, 'patch').mockResolvedValue({ data: true });
            vmEdit.wagon = {
                id: entityId,
                label: 1234,
                plate: 'MOCK PLATE',
                volume: 50,
                typeFk: 1,
            };

            await vmEdit.onSubmit();

            expect(axios.patch).toHaveBeenCalledWith(`Wagons`, vmEdit.wagon);
        });
    });

    describe('onReset()', () => {
        it('should reset wagon if have id', async () => {
            vmEdit.originalData = {
                label: 1234,
                plate: 'Original',
                volume: 200,
                typeFk: 1,
            };
            vmEdit.wagon = {
                label: 4321,
                plate: 'Edited',
                volume: 50,
                typeFk: 2,
            };

            await vmEdit.onReset();

            expect(vmEdit.wagon).toEqual(vmEdit.originalData);
        });

        it('should reset wagon if not have id', async () => {
            vmCreate.wagon = {
                label: 4321,
                plate: 'Edited',
                volume: 50,
                typeFk: 2,
            };

            await vmCreate.onReset();

            expect(vmCreate.wagon).toEqual({});
        });
    });

    describe('fetch()', () => {
        it('should fetch data', async () => {
            vi.spyOn(axios, 'get').mockResolvedValue({ data: [] });

            await vmEdit.fetch();

            expect(axios.get).toHaveBeenCalledWith(`WagonTypes`);
            expect(axios.get).toHaveBeenCalledWith(`Wagons/${entityId}`);
        });
    });
});
