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

const $ = {
    firstPackage: 'vn-autocomplete[label="Package"]',
    firstQuantity: 'vn-ticket-package vn-horizontal:nth-child(1) vn-input-number[ng-model="package.quantity"]',
    firstRemovePackageButton: 'vn-icon-button[vn-tooltip="Remove package"]',
    addPackageButton: 'vn-icon-button[vn-tooltip="Add package"]',
    savePackagesButton: `button[type=submit]`
};

describe('Ticket Create packages path', () => {
    let browser;
    let page;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('employee', 'ticket');
        await page.accessToSearchResult('1');
        await page.accessToSection('ticket.card.package');
    });

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

    it(`should attempt create a new package but receive an error if package is blank`, async() => {
        await page.waitToClick($.firstRemovePackageButton);
        await page.waitToClick($.addPackageButton);
        await page.write($.firstQuantity, '99');
        await page.waitToClick($.savePackagesButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('Package cannot be blank');
    });

    it(`should delete the first package and receive and error to save a new one with blank quantity`, async() => {
        await page.clearInput($.firstQuantity);
        await page.autocompleteSearch($.firstPackage, 'Container medical box 100cm');
        await page.waitToClick($.savePackagesButton);
        const message = await page.waitForSnackbar();

        expect(message.text).toContain('Some fields are invalid');
    });

    it(`should confirm the quantity input isn't invalid yet`, async() => {
        const result = await page
            .evaluate(selector => {
                return document.querySelector(`${selector} input`).checkValidity();
            }, $.firstQuantity);

        expect(result).toBeTruthy();
    });

    it(`should create a new package with correct data`, async() => {
        await page.clearInput($.firstQuantity);
        await page.write($.firstQuantity, '-99');
        await page.waitToClick($.savePackagesButton);
        const message = await page.waitForSnackbar();

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

    it(`should confirm the first select is the expected one`, async() => {
        await page.reloadSection('ticket.card.package');
        await page.waitForTextInField($.firstPackage, 'Container medical box 100cm');
        const result = await page.waitToGetProperty($.firstPackage, 'value');

        expect(result).toEqual('Container medical box 100cm');
    });

    it(`should confirm quantity is just a number and the string part was ignored by the imput number`, async() => {
        await page.waitForTextInField($.firstQuantity, '-99');
        const result = await page.waitToGetProperty($.firstQuantity, 'value');

        expect(result).toEqual('-99');
    });
});