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

describe('Travel create path', () => {
    let browser;
    let page;
    const date = new Date();
    const day = 15;
    date.setDate(day);

    beforeAll(async() => {
        browser = await getBrowser();
        page = browser.page;
        await page.loginAndModule('buyer', 'travel');
    });

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

    it('should open the create travel form by clicking on the "new" button', async() => {
        await page.waitToClick(selectors.travelIndex.newTravelButton);
        await page.waitForState('travel.create');
    });

    it('should fill the reference, agency and ship date then save the form', async() => {
        await page.write(selectors.travelIndex.reference, 'Testing reference');
        await page.autocompleteSearch(selectors.travelIndex.agency, 'inhouse pickup');
        await page.pickDate(selectors.travelIndex.shipDate, date); // this line autocompletes another 3 fields
        await page.waitForTimeout(1000);
        await page.waitToClick(selectors.travelIndex.save);

        const message = await page.waitForSnackbar();

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

    it('should check the user was redirected to the travel basic data upon creation', async() => {
        await page.waitForState('travel.card.basicData');
    });

    it('should check the travel was created with the correct reference', async() => {
        const reference = await page.waitToGetProperty(selectors.travelBasicData.reference, 'value');

        expect(reference).toContain('Testing reference');
    });

    it('should check the travel was created with the correct agency', async() => {
        const agency = await page.waitToGetProperty(selectors.travelBasicData.agency, 'value');

        expect(agency).toContain('inhouse pickup');
    });

    it('should check the travel was created with the correct shiping date', async() => {
        const shipDate = await page.waitToGetProperty(selectors.travelBasicData.shippedDate, 'value');

        expect(shipDate).toContain(day);
    });

    it('should check the travel was created with the correct landing date', async() => {
        const landingDate = await page.waitToGetProperty(selectors.travelBasicData.deliveryDate, 'value');

        expect(landingDate).toContain(day);
    });

    it('should check the travel was created with the correct warehouseOut', async() => {
        const warehouseOut = await page.waitToGetProperty(selectors.travelBasicData.outputWarehouse, 'value');

        expect(warehouseOut).toContain('Warehouse One');
    });

    it('should check the travel was created with the correct warehouseIn', async() => {
        const warehouseIn = await page.waitToGetProperty(selectors.travelBasicData.inputWarehouse, 'value');

        expect(warehouseIn).toContain('Warehouse Five');
    });
});