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

describe('Item', () => {
    describe('Component vnItemDiary', () => {
        let $stateParams;
        let $scope;
        let controller;

        beforeEach(ngModule('item'));

        beforeEach(angular.mock.inject(($componentController, $rootScope, _$stateParams_) => {
            $stateParams = _$stateParams_;
            $stateParams.id = 1;
            $scope = $rootScope.$new();
            controller = $componentController('vnItemDiary', {$scope, $stateParams});
            controller.$scope.model = crudModel;
        }));

        describe('isToday()', () => {
            it(`should call isToday() an return true if an specified date is the current date`, () => {
                let date = new Date();

                let result = controller.isToday(date);

                expect(result).toBeTruthy();
            });

            it(`should call isToday() an return false if an specified date is the current date`, () => {
                let date = '2018-07-03';

                let result = controller.isToday(date);

                expect(result).toBeFalsy();
            });
        });

        describe('freeLineIndex()', () => {
            it(`should call freeLineIndex() and return an index from line with alertLevel 0 and current date`, () => {
                let currentDate = new Date();
                currentDate.setDate(currentDate.getDate() + 1);

                controller.$scope.model = {data: [
                    {name: 'My item 1', alertLevel: 3, date: '2018-05-02'},
                    {name: 'My item 2', alertLevel: 1, date: '2018-05-03'},
                    {name: 'My item 3', alertLevel: 0, date: currentDate}]
                };
                let result = controller.freeLineIndex;

                expect(result).toEqual(2);
            });
        });

        describe('onPreparationLineIndex()', () => {
            it(`should call onPreparationLineIndex() and return an index from line with alertLevel 1 and isPicked true`, () => {
                let currentDate = new Date();
                currentDate.setDate(currentDate.getDate() + 1);
                controller.$scope.model = {data: [
                    {name: 'My item 1', alertLevel: 3, isPicked: true, date: currentDate},
                    {name: 'My item 3', alertLevel: 1, isPicked: true, date: currentDate},
                    {name: 'My item 4', alertLevel: 1, isPicked: false, date: currentDate},
                    {name: 'My item 5', alertLevel: 0, isPicked: false, date: currentDate}]
                };
                let result = controller.onPreparationLineIndex;

                expect(result).toEqual(1);
            });
        });

        describe('set item()', () => {
            it(`should set warehouseFk property based on itemType warehouseFk`, () => {
                spyOn(controller.$scope, '$applyAsync').and.callThrough();
                controller.item = {id: 1, itemType: {warehouseFk: 1}};

                expect(controller.$scope.$applyAsync).toHaveBeenCalledWith(jasmine.any(Function));
                $scope.$apply();

                expect(controller.warehouseFk).toEqual(1);
                expect(controller.item.id).toEqual(1);
            });

            it(`should set warehouseFk property based on url query warehouseFk`, () => {
                spyOn(controller.$scope, '$applyAsync').and.callThrough();
                controller.$stateParams.warehouseFk = 4;
                controller.item = {id: 1, itemType: {warehouseFk: 1}};

                expect(controller.$scope.$applyAsync).toHaveBeenCalledWith(jasmine.any(Function));
                $scope.$apply();

                expect(controller.warehouseFk).toEqual(4);
                expect(controller.item.id).toEqual(1);
            });
        });

        describe('givenTicketIndex() setter', () => {
            it(`should return the index position of the line of a given ticket fk`, () => {
                controller.$scope.model = {data: [
                    {name: 'My item 1', origin: 1, alertLevel: 3, isPicked: true, date: '2018-05-02'},
                    {name: 'My item 3', origin: 2, alertLevel: 1, isPicked: true, date: '2018-05-03'},
                    {name: 'My item 4', origin: 3, alertLevel: 1, isPicked: false, date: '2018-05-03'}]
                };
                controller.ticketFk = 2;

                let index = controller.givenTicketIndex;

                expect(controller.$scope.model.data[index].origin).toEqual(2);
            });
        });
    });
});