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

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

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

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

    it('should attempt to edit the fiscal data but fail as the tax number is invalid', async() => {
        await page.clearInput(selectors.supplierFiscalData.city);
        await page.clearInput(selectors.supplierFiscalData.province);
        await page.clearInput(selectors.supplierFiscalData.country);
        await page.clearInput(selectors.supplierFiscalData.postCode);
        await page.write(selectors.supplierFiscalData.city, 'Valencia');
        await page.waitForTimeout(1000); // must repeat this action twice or fails. also #2699 may be a cool solution to this.
        await page.clearInput(selectors.supplierFiscalData.city);
        await page.write(selectors.supplierFiscalData.city, 'Valencia');
        await page.clearInput(selectors.supplierFiscalData.socialName);
        await page.write(selectors.supplierFiscalData.socialName, 'Farmer King SL');
        await page.clearInput(selectors.supplierFiscalData.taxNumber);
        await page.write(selectors.supplierFiscalData.taxNumber, 'Wrong tax number');
        await page.clearInput(selectors.supplierFiscalData.account);
        await page.write(selectors.supplierFiscalData.account, 'edited account number');
        await page.autocompleteSearch(selectors.supplierFiscalData.sageWihholding, 'retencion estimacion objetiva');
        await page.autocompleteSearch(selectors.supplierFiscalData.sageTaxType, 'operaciones no sujetas');

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

        expect(message.text).toContain('Invalid Tax number');
    });

    it('should save the changes as the tax number is valid this time', async() => {
        await page.clearInput(selectors.supplierFiscalData.taxNumber);
        await page.write(selectors.supplierFiscalData.taxNumber, '12345678Z');

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

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

    it('should reload the section', async() => {
        await page.reloadSection('supplier.card.fiscalData');
    });

    it('should check the socialName was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.socialName, 'value');

        expect(result).toEqual('Farmer King SL');
    });

    it('should check the taxNumber was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.taxNumber, 'value');

        expect(result).toEqual('12345678Z');
    });

    it('should check the account was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.account, 'value');

        expect(result).toEqual('edited account number');
    });

    it('should check the sageWihholding was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.sageWihholding, 'value');

        expect(result).toEqual('RETENCION ESTIMACION OBJETIVA');
    });

    it('should check the sageTaxType was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.sageTaxType, 'value');

        expect(result).toEqual('Operaciones no sujetas');
    });

    it('should check the postCode was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.postCode, 'value');

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

    it('should check the city was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.city, 'value');

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

    it('should check the province was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.province, 'value');

        expect(result).toEqual('Province one (España)');
    });

    it('should check the country was edited', async() => {
        const result = await page.waitToGetProperty(selectors.supplierFiscalData.country, 'value');

        expect(result).toEqual('España');
    });
});