import selectors from '../../helpers/selectors.js';
import createNightmare from '../../helpers/nightmare';

describe('Item create niche path', () => {
    const nightmare = createNightmare();

    beforeAll(() => {
        return nightmare
            .waitForLogin('buyer');
    });

    it('should access to the items index by clicking the items button', async () => {
        const url = await nightmare
            .click(selectors.moduleAccessView.itemsSectionButton)
            .wait(selectors.itemsIndex.createItemButton)
            .parsedUrl();

        expect(url.hash).toEqual('#!/item/index');
    });

    it('should search for the item Gem of Time', async () => {
        const resultCount = await nightmare
            .wait(selectors.itemsIndex.searchResult)
            .type(selectors.itemsIndex.searchItemInput, 'Gem of Time')
            .click(selectors.itemsIndex.searchButton)
            .waitForNumberOfElements(selectors.itemsIndex.searchResult, 1)
            .countElement(selectors.itemsIndex.searchResult);

        expect(resultCount).toEqual(1);
    });

    it(`should click on the search result to access to the item niches`, async () => {
        const url = await nightmare
            .waitForTextInElement(selectors.itemsIndex.searchResult, 'Gem of Time')
            .waitToClick(selectors.itemsIndex.searchResult)
            .waitToClick(selectors.itemNiches.nicheButton)
            .waitForURL('niche')
            .url();

        expect(url).toContain('niche');
    });

    it(`should click create a new niche and delete a former one`, async () => {
        const result = await nightmare
            .waitToClick(selectors.itemNiches.addNicheButton)
            .waitToClick(selectors.itemNiches.secondNicheRemoveButton)
            .waitToClick(selectors.itemNiches.thirdWarehouseSelect)
            .waitToClick(selectors.itemNiches.thirdWarehouseSelectFourthOption)
            .type(selectors.itemNiches.thirdCodeInput, 'A4')
            .click(selectors.itemNiches.submitNichesButton)
            .waitForLastSnackbar();

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

    it(`should confirm the first niche is the expected one`, async () => {
        let result = await nightmare
            .click(selectors.itemBasicData.basicDataButton)
            .wait(selectors.itemBasicData.nameInput)
            .click(selectors.itemNiches.nicheButton)
            .waitForTextInInput(selectors.itemNiches.firstWarehouseSelect, 'Warehouse One')
            .getInputValue(selectors.itemNiches.firstWarehouseSelect);

        expect(result).toEqual('Warehouse One');
        result = await nightmare
            .getInputValue(selectors.itemNiches.firstCodeInput);

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

    it(`should confirm the second niche is the expected one`, async () => {
        let result = await nightmare
            .getInputValue(selectors.itemNiches.secondWarehouseSelect);

        expect(result).toEqual('Warehouse Three');
        result = await nightmare
            .getInputValue(selectors.itemNiches.secondCodeInput);


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

    it(`should confirm the third niche is the expected one`, async () => {
        let result = await nightmare
            .getInputValue(selectors.itemNiches.thirdWarehouseSelect);

        expect(result).toEqual('Warehouse Two');
        result = await nightmare
            .getInputValue(selectors.itemNiches.thirdCodeInput);

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