const models = require('vn-loopback/server/server').models;

describe('sale reserve()', () => {
    const ctx = {
        req: {
            accessToken: {userId: 9},
            headers: {origin: 'localhost:5000'},
            __: () => {}
        }
    };

    it('should throw an error if the ticket can not be modified', async() => {
        const tx = await models.Sale.beginTransaction({});

        let error;
        try {
            const options = {transaction: tx};

            const ticketId = 2;
            const sales = [{id: 5}];
            const reserved = false;

            await models.Sale.reserve(ctx, ticketId, sales, reserved, options);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            error = e;
        }

        expect(error).toEqual(new Error(`The sales of this ticket can't be modified`));
    });

    it('should update the given sales of a ticket to reserved', async() => {
        const tx = await models.Sale.beginTransaction({});

        try {
            const options = {transaction: tx};

            originalTicketSales = await models.Ticket.getSales(11, options);

            expect(originalTicketSales[0].reserved).toEqual(false);
            expect(originalTicketSales[1].reserved).toEqual(false);

            const ticketId = 11;
            const sales = [{id: 7}, {id: 8}];
            const reserved = true;

            await models.Sale.reserve(ctx, ticketId, sales, reserved, options);

            const reservedTicketSales = await models.Ticket.getSales(ticketId, options);

            expect(reservedTicketSales[0].reserved).toEqual(true);
            expect(reservedTicketSales[1].reserved).toEqual(true);

            await tx.rollback();
        } catch (e) {
            await tx.rollback();
            throw e;
        }
    });
});