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

// #1528 e2e claim/detail
xdescribe('Route basic Data path', () => {
    let browser;
    let page;

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('delivery', 'route');
        await page.accessToSearchResult('3');
        await page.accessToSection('route.card.tickets');
    });

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

    it('should modify the first ticket priority', async() => {
        const result = await nightmare;
        await page.write(selectors.routeTickets.firstTicketPriority, '2');
        await page.keyboard.press('Enter');
        await page.waitForLastSnackbar();

        expect(result).toEqual('Data saved!');
    });

    it('should confirm the buscamanButton is disabled', async() => {
        const result = await nightmare;
        await page.evaluate(selector => {
            return document.querySelector(selector);
        }, `${selectors.routeTickets.buscamanButton} :disabled`);

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

    it('should check the first ticket checkbox and confirm the buscamanButton button is no longer disabled', async() => {
        const result = await nightmare;
        await page.waitToClick(selectors.routeTickets.firstTicketCheckbox);
        await page.evaluate(selector => {
            return document.querySelector(selector);
        }, `${selectors.routeTickets.buscamanButton} :disabled`);

        expect(result).toBeFalsy();
    });

    it('should check the route volume on the descriptor', async() => {
        const result = await nightmare;
        await page.waitToGetProperty(selectors.routeDescriptor.volume, 'innerText');

        expect(result).toEqual('1.1 / 18 m³');
    });

    it('should count how many tickets are in route', async() => {
        const result = await nightmare;
        await page.countElement('vn-route-tickets vn-textfield[ng-model="ticket.priority"]');

        expect(result).toEqual(11);
    });

    it('should delete the first ticket in route', async() => {
        const result = await nightmare;
        await page.waitToClick(selectors.routeTickets.firstTicketDeleteButton);
        await page.waitToClick(selectors.routeTickets.confirmButton);
        await page.waitForLastSnackbar();

        expect(result).toEqual('Ticket removed from route');
    });

    it('should again delete the first ticket in route', async() => {
        const result = await nightmare;
        await page.waitToClick(selectors.routeTickets.firstTicketDeleteButton);
        await page.waitToClick(selectors.routeTickets.confirmButton);
        await page.waitForLastSnackbar();

        expect(result).toEqual('Ticket removed from route');
    });

    it('should now count how many tickets are in route to find one less', async() => {
        const result = await nightmare;
        await page.countElement('vn-route-tickets vn-textfield[ng-model="ticket.priority"]');

        expect(result).toEqual(9);
    });

    it('should confirm the route volume on the descriptor has been updated by the changes made', async() => {
        const result = await nightmare;
        await page.waitToGetProperty(selectors.routeDescriptor.volume, 'innerText');

        expect(result).toEqual('0.9 / 18 m³');
    });
});