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

describe('updateMailState()', () => {
    const developerId = 9;
    const employeeId = 1;
    it('should update WorkerTimeControlMail if exist record', async() => {
        const tx = await models.WorkerTimeControlMail.beginTransaction({});
        const args = {
            week: 50,
            year: 2000,
            state: 'CONFIRMED'
        };
        const ctx = {args};

        try {
            const options = {transaction: tx};
            const beforeMail = await models.WorkerTimeControlMail.findOne({
                where: {
                    workerFk: developerId,
                    year: args.year,
                    week: args.week,
                }
            }, options);
            await models.WorkerTimeControl.updateMailState(ctx, developerId, options);
            const afterMail = await models.WorkerTimeControlMail.findOne({
                where: {
                    workerFk: developerId,
                    year: args.year,
                    week: args.week,
                }
            }, options);

            expect(beforeMail.state).toEqual('SENDED');
            expect(afterMail.state).toEqual(args.state);

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

    it('should insert WorkerTimeControlMail if exist record', async() => {
        const tx = await models.WorkerTimeControlMail.beginTransaction({});
        const args = {
            week: 51,
            year: 2000,
            state: 'SENDED'
        };
        const ctx = {args};

        try {
            const options = {transaction: tx};
            const beforeMail = await models.WorkerTimeControlMail.find({
                where: {
                    workerFk: employeeId,
                    year: args.year,
                    week: args.week,
                }
            }, options);
            await models.WorkerTimeControl.updateMailState(ctx, employeeId, options);
            const afterMail = await models.WorkerTimeControlMail.find({
                where: {
                    workerFk: employeeId,
                    year: args.year,
                    week: args.week,
                }
            }, options);

            expect(beforeMail).toEqual([]);
            expect(afterMail.length).toEqual(1);

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

    it('should throw error if not exist any record in this week', async() => {
        const tx = await models.WorkerTimeControlMail.beginTransaction({});
        const ctx = {args: {
            workerId: employeeId,
            week: 1,
            year: 0,
            state: 'SENDED'
        }};

        let error;
        try {
            const options = {transaction: tx};
            await models.WorkerTimeControl.updateMailState(ctx, employeeId, options);

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

        expect(error.message).toEqual(`There aren't records for this week`);
    });
});