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

describe('InvoiceIn serial path', () => {
    let browser;
    let page;
    let httpRequest;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('administrative', 'invoiceIn');
        await page.accessToSection('invoiceIn.serial');
        page.on('request', req => {
            if (req.url().includes(`InvoiceIns/getSerial`))
                httpRequest = req.url();
        });
    });

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

    it('should check that passes the correct params to back', async() => {
        await page.overwrite(selectors.invoiceInSerial.daysAgo, '30');
        await page.keyboard.press('Enter');

        expect(httpRequest).toContain('daysAgo=30');

        await page.overwrite(selectors.invoiceInSerial.serial, 'R');
        await page.keyboard.press('Enter');

        expect(httpRequest).toContain('serial=R');
        await page.click(selectors.invoiceInSerial.chip);
    });

    it('should go to index and check if the search-panel has the correct params', async() => {
        await page.waitToClick(selectors.invoiceInSerial.goToIndex);
        const params = await page.$$(selectors.invoiceInIndex.topbarSearchParams);
        const serial = await params[0].getProperty('title');
        const isBooked = await params[1].getProperty('title');
        const from = await params[2].getProperty('title');

        expect(await serial.jsonValue()).toContain('serial');
        expect(await isBooked.jsonValue()).toContain('not isBooked');
        expect(await from.jsonValue()).toContain('from');
    });
});