import selectors from '../../helpers/selectors';
import getBrowser from '../../helpers/puppeteer';

describe('Client Edit basicData path', () => {
    let browser;
    let page;
    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('employee', 'client');
        await page.accessToSearchResult('Bruce Wayne');
        await page.accessToSection('client.card.basicData');
    });

    afterAll(async() => {
        await browser.close();
    });

    describe('as employee', () => {
        it('should not be able to change the salesPerson', async() => {
            await page.waitForSelector(selectors.clientBasicData.name);
            const result = await page.evaluate(selector => {
                return document.querySelector(selector).disabled;
            }, `${selectors.clientBasicData.salesPerson} input`);

            expect(result).toBeTruthy();
        });

        it('should edit the client basic data but leave salesPerson untainted', async() => {
            await page.clearInput(selectors.clientBasicData.name);
            await page.write(selectors.clientBasicData.name, 'Ptonomy Wallace');
            await page.clearInput(selectors.clientBasicData.contact);
            await page.write(selectors.clientBasicData.contact, 'David Haller');
            await page.clearInput(selectors.clientBasicData.email);
            await page.write(selectors.clientBasicData.email, 'PWallace@verdnatura.es');
            await page.autocompleteSearch(selectors.clientBasicData.channel, 'Rumors on the streets');
            await page.autocompleteSearch(selectors.clientBasicData.transferor, 'Max Eisenhardt');
            await page.autocompleteSearch(selectors.clientBasicData.businessType, 'Eventos');
            await page.waitToClick(selectors.clientBasicData.saveButton);
            const message = await page.waitForSnackbar();

            expect(message.text).toContain('Data saved!');
        });

        it('should confirm the name have been edited', async() => {
            await page.reloadSection('client.card.basicData');
            const result = await page.waitToGetProperty(selectors.clientBasicData.name, 'value');

            expect(result).toEqual('Ptonomy Wallace');
        });

        it('should confirm the contact name have been edited', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.contact, 'value');

            expect(result).toEqual('David Haller');
        });

        it('should confirm the business type have been edited', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.businessType, 'value');

            expect(result).toEqual('Eventos');
        });

        it('should confirm the email have been edited', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.email, 'value');

            expect(result).toEqual('PWallace@verdnatura.es');
        });

        it('should confirm the channel have been selected', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.channel, 'value');

            expect(result).toEqual('Rumors on the streets');
        });

        it('should confirm the previous client have been selected', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.transferor, 'value');

            expect(result).toEqual('Max Eisenhardt');
        });
    });

    describe('as salesAssistant', () => {
        it('should navigate to a client basic data', async() => {
            await page.loginAndModule('salesASsistant', 'client');
            await page.accessToSearchResult('Ptonomy Wallace');
            await page.accessToSection('client.card.basicData');
        });

        it('should be able to change the salesPerson', async() => {
            await page.waitForSelector(selectors.clientBasicData.name);
            const result = await page.evaluate(selector => {
                return document.querySelector(selector).disabled;
            }, `${selectors.clientBasicData.salesPerson} input`);

            expect(result).toBeFalsy();
        });

        it('should edit the client basic data including salesPerson', async() => {
            await page.clearInput(selectors.clientBasicData.name);
            await page.write(selectors.clientBasicData.name, 'Ororo Munroe');
            await page.clearInput(selectors.clientBasicData.contact);
            await page.write(selectors.clientBasicData.contact, 'Black Panther');
            await page.clearInput(selectors.clientBasicData.email);
            await page.write(selectors.clientBasicData.email, 'Storm@verdnatura.es');
            await page.clearInput(selectors.clientBasicData.phone);
            await page.write(selectors.clientBasicData.phone, '333333333');
            await page.clearInput(selectors.clientBasicData.mobile);
            await page.write(selectors.clientBasicData.mobile, '444444444');
            await page.autocompleteSearch(selectors.clientBasicData.salesPerson, 'salesPerson');
            await page.autocompleteSearch(selectors.clientBasicData.channel, 'Metropolis newspaper');
            await page.waitToClick(selectors.clientBasicData.saveButton);
            const message = await page.waitForSnackbar();

            expect(message.text).toContain('Data saved!');
        });

        it('should now confirm the name have been edited', async() => {
            await page.reloadSection('client.card.basicData');
            const result = await page.waitToGetProperty(selectors.clientBasicData.name, 'value');

            expect(result).toEqual('Ororo Munroe');
        });

        it('should now confirm the contact name have been edited', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.contact, 'value');

            expect(result).toEqual('Black Panther');
        });

        it('should now confirm the email have been edited', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.email, 'value');

            expect(result).toEqual('Storm@verdnatura.es');
        });

        it('should now confirm the phone have been edited', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.phone, 'value');

            expect(result).toEqual('333333333');
        });

        it('should now confirm the mobile have been edited', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.mobile, 'value');

            expect(result).toEqual('444444444');
        });

        it('should confirm the sales person have been selected', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.salesPerson, 'value');

            expect(result).toEqual('salesPersonNick');
        });

        it('should now confirm the channel have been selected', async() => {
            const result = await page
                .waitToGetProperty(selectors.clientBasicData.channel, 'value');

            expect(result).toEqual('Metropolis newspaper');
        });
    });
});