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

describe('Client createWithInsurance', () => {
    const activeCtx = {
        accessToken: {userId: 19},
        http: {
            req: {
                headers: {origin: 'http://localhost/'},
                __: () => {}
            }
        }
    };

    beforeAll(async() => {
        spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
            active: activeCtx,
        });
    });

    it('should create a new client credit classification with insurance', async() => {
        const tx = await models.Client.beginTransaction({});

        try {
            const options = {transaction: tx};
            const data = {clientFk: 1101, started: Date.vnNow(), credit: 999, grade: 255};

            const result = await models.CreditClassification.createWithInsurance(data, options);

            expect(result.client).toEqual(1101);

            const classifications = await models.CreditClassification.find(null, options);
            const insurances = await models.CreditInsurance.find(null, options);

            expect(classifications.length).toEqual(6);
            expect(insurances.length).toEqual(4);

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