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

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

    beforeAll(() => {
        vm = createWrapper(ClaimLines, {
            global: {
                stubs: ['FetchData', 'VnPaginate'],
                mocks: {
                    fetch: vi.fn(),
                },
            },
        }).vm;
    });

    beforeEach(() => {
        vm.claim = {
            id: 1,
            ticketFk: 1,
        };
        vm.store.data = [
            {
                id: 1,
                quantity: 10,
                sale: {
                    id: 1,
                    discount: 0,
                },
            },
        ];
    });

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

    describe('updateDiscount()', () => {
        it('should make a POST request to endpoint "updateDiscount"', async () => {
            vi.spyOn(axios, 'post').mockResolvedValue({ data: true });
            vi.spyOn(vm.quasar, 'notify');

            const canceller = new AbortController();
            await vm.updateDiscount({ saleFk: 1, discount: 5, canceller });

            const expectedData = { salesIds: [1], newDiscount: 5 };
            expect(axios.post).toHaveBeenCalledWith(
                'Tickets/1/updateDiscount',
                expectedData,
                {
                    signal: canceller.signal,
                }
            );
        });
    });

    describe('onUpdateDiscount()', () => {
        it('should make a POST request and then set the discount on the original row', async () => {
            vi.spyOn(vm.quasar, 'notify');

            vm.onUpdateDiscount({ discount: 5, rowIndex: 0 });
            const firstRow = vm.store.data[0];

            expect(firstRow.sale.discount).toEqual(5);
            expect(vm.quasar.notify).toHaveBeenCalledWith(
                expect.objectContaining({
                    message: 'Discount updated',
                    type: 'positive',
                })
            );
        });
    });
});
