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

describe('Entry lastest buys path', () => {
    let browser;
    let page;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('buyer', 'entry');
    });

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

    it('should access the latest buys seccion and search not seeing the edit buys button yet', async() => {
        await page.waitToClick(selectors.entryLatestBuys.latestBuysSectionButton);
        await page.waitForSelector(selectors.entryLatestBuys.editBuysButton, {visible: false});
    });

    it('should select all lines but one and then check the edit buys button appears', async() => {
        await page.waitToClick(selectors.entryLatestBuys.allBuysCheckBox);
        await page.waitToClick(selectors.entryLatestBuys.secondBuyCheckBox);
        await page.waitForSelector(selectors.entryLatestBuys.editBuysButton, {visible: true});
    });

    it('should open the edit dialog', async() => {
        await page.waitToClick(selectors.entryLatestBuys.editBuysButton);
        await page.waitForSelector(selectors.entryLatestBuys.fieldAutocomplete, {visible: true});
    });

    it('should search for the "Description" and type a new one for the items in each selected buy', async() => {
        await page.autocompleteSearch(selectors.entryLatestBuys.fieldAutocomplete, 'Description');
        await page.write(selectors.entryLatestBuys.newValueInput, 'Crafted item');
        await page.waitToClick(selectors.entryLatestBuys.acceptEditBuysDialog);
    });

    it('should navigate to the entry.buy section by clicking one of the buys', async() => {
        await page.waitToClick(selectors.entryLatestBuys.firstBuy);
        await page.waitForState('entry.card.buy.index');
    });
});