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

describe('Order edit basic data path', () => {
    const nightmare = createNightmare();
    describe('when confirmed order', () => {
        beforeAll(() => {
            nightmare
                .loginAndModule('employee', 'order')
                .accessToSearchResult('1')
                .accessToSection('order.card.basicData');
        });

        it('should not be able to change the client', async() => {
            const result = await nightmare
                .autocompleteSearch(selectors.orderBasicData.clientAutocomplete, 'Tony Stark')
                .autocompleteSearch(selectors.orderBasicData.addressAutocomplete, 'Tony Stark')
                .waitToClick(selectors.orderBasicData.saveButton)
                .waitForLastSnackbar();

            expect(result).toEqual(`You can't make changes on the basic data of an confirmed order or with rows`);
        }, 15000);
    });

    describe('when order with rows', () => {
        it('should now navigate to order index', async() => {
            const url = await nightmare
                .waitToClick(selectors.globalItems.returnToModuleIndexButton)
                .waitToClick(selectors.globalItems.acceptVnConfirm)
                .wait(selectors.ordersIndex.createOrderButton)
                .accessToSearchResult('16')
                .accessToSection('order.card.basicData')
                .wait(selectors.orderBasicData.observationInput)
                .parsedUrl();

            expect(url.hash).toEqual('#!/order/16/basic-data');
        });

        it('should not be able to change the company', async() => {
            const result = await nightmare
                .write(selectors.orderBasicData.observationInput, 'observation')
                .waitToClick(selectors.orderBasicData.saveButton)
                .waitForLastSnackbar();

            expect(result).toEqual(`You can't make changes on the basic data of an confirmed order or with rows`);
        });
    });

    describe('when new order', () => {
        it('should once more navigate to order index', async() => {
            const url = await nightmare
                .waitToClick(selectors.globalItems.returnToModuleIndexButton)
                .waitToClick(selectors.globalItems.acceptVnConfirm)
                .wait(selectors.ordersIndex.createOrderButton)
                .accessToSearchResult('18')
                .accessToSection('order.card.basicData')
                .wait(selectors.orderBasicData.observationInput)
                .parsedUrl();

            expect(url.hash).toEqual('#!/order/18/basic-data');
        });

        it('should be able to modify all the properties', async() => {
            const result = await nightmare
                .autocompleteSearch(selectors.orderBasicData.clientAutocomplete, 'Tony Stark')
                .autocompleteSearch(selectors.orderBasicData.addressAutocomplete, 'Tony Stark')
                .autocompleteSearch(selectors.orderBasicData.agencyAutocomplete, 'Silla247')
                .clearInput(selectors.orderBasicData.observationInput)
                .write(selectors.orderBasicData.observationInput, 'Observation modified')
                .waitToClick(selectors.orderBasicData.saveButton)
                .waitForLastSnackbar();

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

        it('should now confirm the client have been edited', async() => {
            const result = await nightmare
                .reloadSection('order.card.basicData')
                .waitToGetProperty(`${selectors.orderBasicData.clientAutocomplete} input`, 'value');

            expect(result).toEqual('104: Tony Stark');
        });

        it('should now confirm the agency have been edited', async() => {
            const result = await nightmare
                .waitToGetProperty(`${selectors.orderBasicData.agencyAutocomplete} input`, 'value');

            expect(result).toEqual('7: Silla247');
        });

        it('should now confirm the observations have been edited', async() => {
            const result = await nightmare
                .waitToGetProperty(selectors.orderBasicData.observationInput, 'value');

            expect(result).toEqual('Observation modified');
        });
    });
});