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

describe('Item Create', () => {
    let browser;
    let page;
    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('buyer', 'item');
    });

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

    it(`should search for the item Infinity Gauntlet to confirm it isn't created yet`, async() => {
        await page.doSearch('Infinity Gauntlet');
        const resultsCount = await page.countElement(selectors.itemsIndex.searchResult);

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

    it('should access to the create item view by clicking the create floating button', async() => {
        await page.waitToClick(selectors.itemsIndex.createItemButton);
        await page.waitForState('item.create');
    });

    it('should return to the item index by clickig the cancel button', async() => {
        await page.waitToClick(selectors.itemCreateView.cancelButton);
        await page.waitForState('item.index');
    });

    it('should now access to the create item view by clicking the create floating button', async() => {
        await page.waitToClick(selectors.itemsIndex.createItemButton);
        await page.waitForState('item.create');
    });

    it('should create the Infinity Gauntlet item', async() => {
        await page.write(selectors.itemCreateView.temporalName, 'Infinity Gauntlet');
        await page.autocompleteSearch(selectors.itemCreateView.type, 'Crisantemo');
        await page.autocompleteSearch(selectors.itemCreateView.intrastat, 'Coral y materiales similares');
        await page.autocompleteSearch(selectors.itemCreateView.origin, 'Holand');
        await page.waitToClick(selectors.itemCreateView.createButton);
        const message = await page.waitForSnackbar();

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

    it('should confirm Infinity Gauntlet item was created', async() => {
        let result = await page
            .waitToGetProperty(selectors.itemBasicData.name, 'value');

        expect(result).toEqual('Infinity Gauntlet');

        result = await page
            .waitToGetProperty(selectors.itemBasicData.type, 'value');

        expect(result).toEqual('Crisantemo');

        result = await page
            .waitToGetProperty(selectors.itemBasicData.intrastat, 'value');

        expect(result).toEqual('5080000 Coral y materiales similares');

        result = await page
            .waitToGetProperty(selectors.itemBasicData.origin, 'value');

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