const app = require(`${servicesDir}/client/server/server`);

describe('Client updateFiscalData', () => {
    afterAll(async() => {
        let ctxOfAdmin = {req: {accessToken: {userId: 5}}};
        let validparams = {postcode: 46460};
        let idWithDataChecked = 101;

        await app.models.Client.updateFiscalData(ctxOfAdmin, validparams, idWithDataChecked);
    });

    it('should return an error if the user is not administrative and the isTaxDataChecked value is true', async() => {
        let error;

        let ctxOfNoAdmin = {req: {accessToken: {userId: 1}}};
        let params = [];
        let idWithDataChecked = 101;

        await app.models.Client.updateFiscalData(ctxOfNoAdmin, params, idWithDataChecked)
            .catch(e => {
                error = e;
            });

        expect(error.toString()).toContain(`You don't have enough privileges to do that`);
    });

    it('should return an error if the user is administrative and the isTaxDataChecked value is true BUT the params aint valid', async() => {
        let error;

        let ctxOfAdmin = {req: {accessToken: {userId: 5}}};
        let invalidparams = {invalid: 'param for update'};
        let idWithDataChecked = 101;

        await app.models.Client.updateFiscalData(ctxOfAdmin, invalidparams, idWithDataChecked)
            .catch(e => {
                error = e;
            });

        expect(error.toString()).toContain(`You don't have enough privileges to do that`);
    });

    it('should update the client fiscal data and return the count if changes made', async() => {
        let ctxOfAdmin = {req: {accessToken: {userId: 5}}};
        let validparams = {postcode: 12345};
        let idWithDataChecked = 101;

        let client = await app.models.Client.findById(idWithDataChecked);

        expect(client.postcode).toEqual('46460');

        let result = await app.models.Client.updateFiscalData(ctxOfAdmin, validparams, idWithDataChecked);

        expect(result).toEqual({count: 1});

        let updatedClient = await app.models.Client.findById(idWithDataChecked);

        expect(updatedClient.postcode).toEqual('12345');
    });
});