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

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

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

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

        expect(nResults).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 create the Knowledge artifact item', async() => {
        await page.write(selectors.itemCreateView.temporalName, 'Knowledge artifact');
        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 return to the items index by clicking the return to items button', async() => {
        await page.waitToClick(selectors.itemBasicData.goToItemIndexButton);
        await page.waitForSelector(selectors.itemsIndex.createItemButton);
        await page.waitForState('item.index');
    });

    it(`should search for the created item and navigate to it's log section`, async() => {
        await page.accessToSearchResult('Knowledge artifact');
        await page.accessToSection('item.card.log');
    });

    it(`should confirm the log is showing 5 entries`, async() => {
        await page.waitForSelector(selectors.itemLog.anyLineCreated);
        const anyLineCreatedCount = await page.countElement(selectors.itemLog.anyLineCreated);

        expect(anyLineCreatedCount).toEqual(5);
    });

    it(`should confirm the log is showing the intrastat for the created item`, async() => {
        const fifthLineCreatedProperty = await page
            .waitToGetProperty(selectors.itemLog.fifthLineCreatedProperty, 'innerText');

        expect(fifthLineCreatedProperty).toEqual('Coral y materiales similares');
    });
});