import './index.js';

describe('Item', () => {
    describe('Component vnItemTax', () => {
        let $stateParams;
        let controller;
        let $httpBackend;
        let vnApp;

        beforeEach(ngModule('item'));

        beforeEach(angular.mock.inject(($componentController, _$httpBackend_, _$stateParams_, _vnApp_) => {
            $stateParams = _$stateParams_;
            $stateParams.id = 1;
            $httpBackend = _$httpBackend_;
            vnApp = _vnApp_;
            spyOn(vnApp, 'showSuccess');
            controller = $componentController('vnItemTax', {$state: $stateParams});
        }));

        describe('getTaxes()', () => {
            it('should perform a query to set the array of taxes into the controller', () => {
                let filter = {
                    fields: ['id', 'countryFk', 'taxClassFk'],
                    include: [{
                        relation: 'country',
                        scope: {fields: ['country']}
                    }]
                };
                let response = [{id: 1, taxClassFk: 1}];
                filter = encodeURIComponent(JSON.stringify(filter));
                $httpBackend.when('GET', `/item/api/Items/1/taxes?filter=${filter}`).respond(response);
                $httpBackend.expect('GET', `/item/api/Items/1/taxes?filter=${filter}`);
                controller.$onInit();
                $httpBackend.flush();

                expect(controller.taxes).toEqual(response);
            });
        });

        describe('submit()', () => {
            it('should perform a post to update taxes', () => {
                let filter = {
                    fields: ['id', 'countryFk', 'taxClassFk'],
                    include: [{
                        relation: 'country',
                        scope: {fields: ['country']}
                    }]
                };
                let response = [{id: 1, taxClassFk: 1}];
                filter = encodeURIComponent(JSON.stringify(filter));
                $httpBackend.when('GET', `/item/api/Items/1/taxes?filter=${filter}`).respond(response);
                controller.$onInit();
                $httpBackend.flush();

                $httpBackend.when('POST', `/item/api/Items/updateTaxes`).respond('ok');
                $httpBackend.expect('POST', `/item/api/Items/updateTaxes`);
                controller.submit();
                $httpBackend.flush();

                expect(vnApp.showSuccess).toHaveBeenCalledWith('Data saved!');
            });
        });
    });
});