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

describe('Supplier contact path', () => {
    let browser;
    let page;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('administrative', 'supplier');
        await page.accessToSearchResult('1');
        await page.accessToSection('supplier.card.contact');
    });

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

    it('should create a new contact', async() => {
        await page.waitToClick(selectors.supplierContact.addNewContact);
        await page.write(selectors.supplierContact.thirdContactName, 'The tester');
        await page.write(selectors.supplierContact.thirdContactPhone, '99 999 99 99');
        await page.write(selectors.supplierContact.thirdContactMobile, '555 55 55 55');
        await page.write(selectors.supplierContact.thirdContactEmail, 'testing@puppeteer.com');
        await page.write(selectors.supplierContact.thirdContactNotes, 'the end to end integration tester');
        await page.waitToClick(selectors.supplierContact.saveButton);
        const message = await page.waitForSnackbar();

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

    it(`should reload the section and count the contacts`, async() => {
        await page.reloadSection('supplier.card.contact');
        const result = await page.countElement(selectors.supplierContact.anyContact);

        expect(result).toEqual(3);
    });

    it(`should check the new contact name was saved correctly`, async() => {
        const result = await page.waitToGetProperty(selectors.supplierContact.thirdContactName, 'value');

        expect(result).toContain('The tester');
    });

    it(`should check the new contact phone was saved correctly`, async() => {
        const result = await page.waitToGetProperty(selectors.supplierContact.thirdContactPhone, 'value');

        expect(result).toContain('99 999 99 99');
    });

    it(`should check the new contact mobile was saved correctly`, async() => {
        const result = await page.waitToGetProperty(selectors.supplierContact.thirdContactMobile, 'value');

        expect(result).toContain('555 55 55 55');
    });

    it(`should check the new contact email was saved correctly`, async() => {
        const result = await page.waitToGetProperty(selectors.supplierContact.thirdContactEmail, 'value');

        expect(result).toContain('testing@puppeteer.com');
    });

    it(`should check the new contact note was saved correctly`, async() => {
        await page.waitForTextInField(selectors.supplierContact.thirdContactNotes, 'the end to end integration tester');
        const result = await page.waitToGetProperty(selectors.supplierContact.thirdContactNotes, 'value');

        expect(result).toContain('the end to end integration tester');
    });

    it(`should remove the created contact`, async() => {
        await page.waitToClick(selectors.supplierContact.thirdContactDeleteButton, 'value');
        const result = await page.countElement(selectors.supplierContact.anyContact);

        expect(result).toEqual(2);

        await page.waitToClick(selectors.supplierContact.saveButton);
        const message = await page.waitForSnackbar();

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

    it(`should reload the section and count the amount of contacts went back to 2`, async() => {
        await page.reloadSection('supplier.card.contact');
        const result = await page.countElement(selectors.supplierContact.anyContact);

        expect(result).toEqual(2);
    });
});