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

describe('Item Create botanical path', () => {
    const nightmare = createNightmare();

    beforeAll(() => {
        nightmare
            .loginAndModule('buyer', 'item')
            .accessToSearchResult('Object5 Weapon 50')
            .accessToSection('item.card.botanical');
    });

    it(`should create a new botanical for the item`, async() => {
        const result = await nightmare
            .write(selectors.itemBotanical.botanicalInput, 'Cicuta maculata')
            .autocompleteSearch(selectors.itemBotanical.genusAutocomplete, 'Abelia')
            .autocompleteSearch(selectors.itemBotanical.speciesAutocomplete, 'dealbata')
            .waitToClick(selectors.itemBotanical.submitBotanicalButton)
            .waitForLastSnackbar();

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

    it(`should confirm the botanical for the item was created`, async() => {
        const result = await nightmare
            .reloadSection('item.card.botanical')
            .waitForTextInInput(selectors.itemBotanical.botanicalInput, 'Cicuta maculata')
            .waitToGetProperty(selectors.itemBotanical.botanicalInput, 'value');

        expect(result).toEqual('Cicuta maculata');
    });

    it(`should confirm the Genus for the item was created`, async() => {
        const result = await nightmare
            .waitForTextInInput(`${selectors.itemBotanical.genusAutocomplete} input`, 'Abelia')
            .waitToGetProperty(`${selectors.itemBotanical.genusAutocomplete} input`, 'value');

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

    it(`should confirm the Species for the item was created`, async() => {
        const result = await nightmare
            .waitToGetProperty(`${selectors.itemBotanical.speciesAutocomplete} input`, 'value');

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

    it(`should edit botanical for the item`, async() => {
        const result = await nightmare
            .clearInput(selectors.itemBotanical.botanicalInput)
            .write(selectors.itemBotanical.botanicalInput, 'Herp Derp')
            .autocompleteSearch(selectors.itemBotanical.genusAutocomplete, 'Abies')
            .autocompleteSearch(selectors.itemBotanical.speciesAutocomplete, 'decurrens')
            .waitToClick(selectors.itemBotanical.submitBotanicalButton)
            .waitForLastSnackbar();

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

    it(`should confirm the botanical for the item was edited`, async() => {
        const result = await nightmare
            .reloadSection('item.card.botanical')
            .waitForTextInInput(selectors.itemBotanical.botanicalInput, 'Herp Derp')
            .waitToGetProperty(selectors.itemBotanical.botanicalInput, 'value');

        expect(result).toEqual('Herp Derp');
    });

    it(`should confirm the Genus for the item was edited`, async() => {
        const result = await nightmare
            .waitForTextInInput(`${selectors.itemBotanical.genusAutocomplete} input`, 'Abies')
            .waitToGetProperty(`${selectors.itemBotanical.genusAutocomplete} input`, 'value');

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

    it(`should confirm the Species for the item was edited`, async() => {
        const result = await nightmare
            .waitToGetProperty(`${selectors.itemBotanical.speciesAutocomplete} input`, 'value');

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