import './index.js';
import watcher from 'core/mocks/watcher';
import crudModel from 'core/mocks/crud-model';

describe('InvoiceIn', () => {
    describe('Component intrastat', () => {
        let controller;
        let $scope;
        let vnApp;

        beforeEach(ngModule('invoiceIn'));

        beforeEach(inject(($componentController, $rootScope, _vnApp_) => {
            vnApp = _vnApp_;
            jest.spyOn(vnApp, 'showError');
            $scope = $rootScope.$new();
            $scope.model = crudModel;
            $scope.watcher = watcher;

            const $element = angular.element('<vn-invoice-in-intrastat></vn-invoice-in-intrastat>');
            controller = $componentController('vnInvoiceInIntrastat', {$element, $scope});
            controller.invoiceIn = {id: 1};
        }));

        describe('calculateTotals()', () => {
            it('should set amountTotal, netTotal and stemsTotal to 0 if salesClaimed has no data', () => {
                controller.invoceInIntrastat = [];
                controller.calculateTotals();

                expect(controller.amountTotal).toEqual(0);
                expect(controller.netTotal).toEqual(0);
                expect(controller.stemsTotal).toEqual(0);
            });

            it('should set amountTotal, netTotal and stemsTotal', () => {
                controller.invoceInIntrastat = [
                    {
                        id: 1,
                        invoiceInFk: 1,
                        net: 30.5,
                        intrastatFk: 5080000,
                        amount: 10,
                        stems: 162,
                        countryFk: 5,
                        statisticalValue: 0
                    },
                    {
                        id: 2,
                        invoiceInFk: 1,
                        net: 10,
                        intrastatFk: 6021010,
                        amount: 20,
                        stems: 205,
                        countryFk: 5,
                        statisticalValue: 0
                    }
                ];
                controller.calculateTotals();

                expect(controller.amountTotal).toEqual(30);
                expect(controller.netTotal).toEqual(40.5);
                expect(controller.stemsTotal).toEqual(367);
            });
        });

        describe('onSubmit()', () => {
            it('should make HTTP POST request to save intrastat values', () => {
                controller.card = {reload: () => {}};
                jest.spyOn($scope.watcher, 'check');
                jest.spyOn($scope.watcher, 'notifySaved');
                jest.spyOn($scope.watcher, 'updateOriginalData');
                jest.spyOn(controller.card, 'reload');
                jest.spyOn($scope.model, 'save');

                controller.onSubmit();

                expect($scope.model.save).toHaveBeenCalledWith();
                expect($scope.watcher.updateOriginalData).toHaveBeenCalledWith();
                expect($scope.watcher.check).toHaveBeenCalledWith();
                expect($scope.watcher.notifySaved).toHaveBeenCalledWith();
                expect(controller.card.reload).toHaveBeenCalledWith();
            });
        });
    });
});