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

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

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('administrative', 'invoiceIn');
        await page.accessToSearchResult('10');
        await page.accessToSection('invoiceIn.card.basicData');
    });

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

    it('should clone the invoiceIn using the descriptor more menu', async() => {
        await page.waitToClick(selectors.invoiceInDescriptor.moreMenu);
        await page.waitToClick(selectors.invoiceInDescriptor.moreMenuCloneInvoiceIn);
        await page.waitToClick(selectors.invoiceInDescriptor.acceptButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('InvoiceIn cloned');
    });

    it('should have been redirected to the created invoiceIn summary', async() => {
        await page.waitForState('invoiceIn.card.summary');
    });

    it('should delete the cloned invoiceIn using the descriptor more menu', async() => {
        await page.waitToClick(selectors.invoiceInDescriptor.moreMenu);
        await page.waitToClick(selectors.invoiceInDescriptor.moreMenuDeleteInvoiceIn);
        await page.waitToClick(selectors.invoiceInDescriptor.acceptButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('InvoiceIn deleted');
    });

    it('should have been relocated to the invoiceOut index', async() => {
        await page.waitForState('invoiceIn.index');
    });

    it(`should search for the deleted invouceOut to find no results`, async() => {
        await page.doSearch('10');
        const nResults = await page.countElement(selectors.invoiceOutIndex.searchResult);

        expect(nResults).toEqual(0);
    });
});