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

describe('Ticket purchase request path', () => {
    let browser;
    let page;

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

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

    it('should add a new request', async() => {
        await page.waitToClick(selectors.ticketRequests.addRequestButton);
        await page.write(selectors.ticketRequests.descriptionInput, 'New stuff');
        await page.write(selectors.ticketRequests.quantity, '9');
        await page.autocompleteSearch(selectors.ticketRequests.atender, 'buyerNick');
        await page.write(selectors.ticketRequests.price, '999');
        await page.waitToClick(selectors.ticketRequests.saveButton);
        const message = await page.waitForSnackbar();

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

    it('should have been redirected to the request index', async() => {
        await page.waitForState('ticket.card.request.index');
    });

    it(`should edit the third request quantity as it's state is still new`, async() => {
        await page.write(selectors.ticketRequests.thirdRequestQuantity, '9');
        await page.keyboard.press('Enter');
        const message = await page.waitForSnackbar();

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

    it('should check the new request was added', async() => {
        await page.reloadSection('ticket.card.request.index');
        const result = await page.waitToGetProperty(selectors.ticketRequests.thirdRequestQuantity, 'value');

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

    it(`should check the first request can't be edited as its state is different to new`, async() => {
        await page.waitForClassPresent(selectors.ticketRequests.firstRequestQuantity, 'disabled');
        const result = await page.isDisabled(selectors.ticketRequests.firstRequestQuantity);

        expect(result).toBe(true);
    });

    it(`should check the second request can't be edited as its state is different to new`, async() => {
        await page.waitForClassPresent(selectors.ticketRequests.secondRequestQuantity, 'disabled');
        const result = await page.isDisabled(selectors.ticketRequests.secondRequestQuantity);

        expect(result).toBe(true);
    });

    it('should delete the added request', async() => {
        await page.waitToClick(selectors.ticketRequests.thirdRemoveRequestButton);
        const message = await page.waitForSnackbar();

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

    it('should check the request was deleted', async() => {
        await page.reloadSection('ticket.card.request.index');
        await page.waitForSelector(selectors.ticketRequests.addRequestButton);
        await page.waitForSelector(selectors.ticketRequests.thirdDescription, {hidden: true});
    });
});