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

const $ = selectors.itemFixedPrice;

describe('Item fixed prices path', () => {
    let browser;
    let page;
    let httpRequest;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('buyer', 'item');
        await page.accessToSection('item.fixedPrice');
        page.on('request', req => {
            if (req.url().includes(`FixedPrices/filter`))
                httpRequest = req.url();
        });
    });

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

    it('should filter using all the fields', async() => {
        await page.write($.generalSearchFilter, 'item');
        await page.keyboard.press('Enter');

        expect(httpRequest).toContain('search=item');

        await page.click($.chip);
        await page.click($.reignFilter);

        expect(httpRequest).toContain('categoryFk');

        await page.autocompleteSearch($.typeFilter, 'Alstroemeria');

        expect(httpRequest).toContain('typeFk');

        await page.click($.chip);
        await page.autocompleteSearch($.buyerFilter, 'buyerNick');

        expect(httpRequest).toContain('buyerFk');

        await page.click($.chip);
        await page.autocompleteSearch($.warehouseFilter, 'Algemesi');

        expect(httpRequest).toContain('warehouseFk');

        await page.click($.chip);
        await page.click($.mineFilter);

        expect(httpRequest).toContain('mine=true');

        await page.click($.chip);
        await page.click($.hasMinPriceFilter);

        expect(httpRequest).toContain('hasMinPrice=true');

        await page.click($.chip);
        await page.click($.addTag);
        await page.autocompleteSearch($.tagFilter, 'Color');
        await page.autocompleteSearch($.tagValueFilter, 'Brown');

        expect(httpRequest).toContain('tags');

        await page.click($.chip);
    });

    it('should click on the add new fixed price button', async() => {
        await page.waitToClick($.add);
        await page.waitForSelector($.fourthFixedPrice);
    });

    it('should fill the fixed price data', async() => {
        const now = Date.vnNew();
        await page.autocompleteSearch($.fourthWarehouse, 'Warehouse one');
        await page.writeOnEditableTD($.fourthGroupingPrice, '1');
        await page.writeOnEditableTD($.fourthPackingPrice, '1');
        await page.write($.fourthMinPrice, '1');
        await page.pickDate($.fourthStarted, now);
        await page.pickDate($.fourthEnded, now);
        const message = await page.waitForSnackbar();

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

    it('should reload the section and check the created price has the expected ID', async() => {
        await page.goto(`http://localhost:5000/#!/item/fixed-price`);

        const result = await page.waitToGetProperty($.fourthItemID, 'value');

        expect(result).toContain('13');
    });
});