import './index.js';

describe('ticket', () => {
    describe('Component vnTicketBasicDataStepThree', () => {
        let now = Date.now();
        let $state;
        let controller;
        let $httpBackend;
        let vnApp;

        beforeEach(ngModule('ticket'));

        beforeEach(angular.mock.inject(($componentController, _$state_, _$httpBackend_, _vnApp_) => {
            $state = _$state_;
            spyOn($state, 'go');
            vnApp = _vnApp_;
            spyOn(vnApp, 'showError');
            $httpBackend = _$httpBackend_;
            controller = $componentController('vnTicketBasicDataStepThree', {$state});
        }));

        describe('onSubmit()', () => {
            it(`should return an error if the item doesn't have option property in the controller`, () => {
                controller.ticket = {};

                controller.onSubmit();

                expect(vnApp.showError).toHaveBeenCalledWith('Choose an option');
            });

            it('should perform a post query correctly then call two functions()', () => {
                spyOn(controller.vnApp, 'showMessage');
                controller.card = {reload: () => {}};
                spyOn(controller.card, 'reload');

                controller.ticket = {
                    id: 1,
                    agencyModeFk: 1,
                    addressFk: 121,
                    zoneFk: 3,
                    warehouseFk: 1,
                    shipped: now,
                    landed: now,
                    option: 1
                };

                let data = {
                    agencyModeId: 1,
                    addressId: 121,
                    zoneId: 3,
                    warehouseId: 1,
                    shipped: now,
                    landed: now,
                    option: 1
                };

                $httpBackend.whenPOST(`tickets/1/componentUpdate`, data).respond('ok');
                $httpBackend.expectPOST(`tickets/1/componentUpdate`, data);
                controller.onSubmit();
                $httpBackend.flush();

                expect(controller.vnApp.showMessage).toHaveBeenCalledWith('The ticket has been unrouted');
                expect(controller.card.reload).toHaveBeenCalledWith();
                expect(controller.$state.go).toHaveBeenCalledWith('ticket.card.summary', jasmine.any(Object));
            });
        });
    });
});