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

const $ = {
    form: 'vn-item-create form'
};

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 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 throw an error when insert an invalid priority', async() => {
        const values = {
            name: 'Infinity Gauntlet',
            type: 'Crisantemo',
            intrastat: 'Coral y materiales similares',
            origin: 'Holand',
            priority: null
        };
        const message = await page.sendForm($.form, values);

        expect(message.text).toContain('Valid priorities');
    });

    it('should create the Infinity Gauntlet item', async() => {
        const values = {
            name: 'Infinity Gauntlet',
            type: 'Crisantemo',
            intrastat: 'Coral y materiales similares',
            origin: 'Holand',
            priority: '2'
        };

        await page.fillForm($.form, values);
        const formValues = await page.fetchForm($.form, Object.keys(values));
        const message = await page.sendForm($.form);

        expect(message.isSuccess).toBeTrue();
        expect(formValues).toEqual(values);
    });
});