<commit_before>describe('ConvertToOrderCtrl', function(){

    var vm, rootScope, $state, $q, stateParams, requisitionService, notification, requisitions;

    beforeEach( function() {
        module('openlmis.requisitions');

        inject(function ($controller, $rootScope, _$state_, _$q_, _RequisitionService_, _Notification_) {
        rootScope = $rootScope;
        $state = _$state_;
        requisitionService = _RequisitionService_;
        notification = _Notification_;
        $q = _$q_;

        stateParams = {
            filterBy: 'all',
            filterValue: ''
        };
        requisitions = [
            {
                id: 'requisitionId1',
                facility: {
                    name: 'facility1',
                    code: 'code1'
                },
                program: {
                    name: 'program1'
                }
            },
            {
                id: 'requisitonId2',
                facility: {
                    name: 'facility2',
                    code: 'code2'
                },
                program: {
                    name: 'program2'
                }
            }
        ];

        vm = $controller('ConvertToOrderCtrl', {$stateParams: stateParams, requisitions: requisitions,
            RequisitionService: requisitionService, Notification: notification});
        });
    });

    it('should show all requisitions if default filter is applied', function () {
        expect(vm.searchParams.filterBy).toEqual('all');
        expect(vm.searchParams.filterValue).toEqual('');
        expect(vm.requisitions).toEqual(requisitions);
    });

    it('should get all selected requisitions', function() {
        vm.requisitions[0].$selected = true;

        var selectedRequisitions = vm.getSelected();

        expect(selectedRequisitions).toEqual([requisitions[0]]);
    });

    it('should get an empty array if no requisition is selected', function() {
        var selectedRequisitions = vm.getSelected();

        expect(selectedRequisitions).toEqual([]);
    });

    it('should convert to order selected requisitions', function() {
        vm.requisitions[0].$selected = true;
        spyOn(requisitionService, 'convertToOrder').andReturn($q.when());

        vm.convertToOrder();

        expect(requisitionService.convertToOrder).toHaveBeenCalled();
    });

    it('should show error when trying to convert to order with no requisition selected', function() {
        spyOn(requisitionService, 'convertToOrder').andReturn($q.when());
        spyOn(notification, 'error').andCallThrough();

        vm.convertToOrder();

        expect(requisitionService.convertToOrder).not.toHaveBeenCalled();
        expect(notification.error).toHaveBeenCalledWith('msg.select.at.least.one.rnr');
    });

    it('should select all requisitions', function() {
       vm.toggleSelectAll(true);

       expect(vm.requisitions[0].$selected).toBe(true);
       expect(vm.requisitions[1].$selected).toBe(true);
    });

    it('should deselect all requisitions', function() {
        vm.toggleSelectAll(false);

        expect(vm.requisitions[0].$selected).toBe(false);
        expect(vm.requisitions[1].$selected).toBe(false);<commit_msg>OLMIS-1534: Added more unit tests to convertToOrder controller<commit_after>describe('ConvertToOrderCtrl', function(){

    var vm, rootScope, state, q, stateParams, requisitionService, notification, requisitions;

    beforeEach( function() {
        module('openlmis.requisitions');

        inject(function ($controller, $rootScope, _$state_, _$q_, _RequisitionService_, _Notification_) {
        rootScope = $rootScope;
        state = _$state_;
        requisitionService = _RequisitionService_;
        notification = _Notification_;
        q = _$q_;

        stateParams = {
            filterBy: 'all',
            filterValue: ''
        };
        requisitions = [
            {
                id: 'requisitionId1',
                facility: {
                    name: 'facility1',
                    code: 'code1'
                },
                program: {
                    name: 'program1'
                }
            },
            {
                id: 'requisitonId2',
                facility: {
                    name: 'facility2',
                    code: 'code2'
                },
                program: {
                    name: 'program2'
                }
            }
        ];

        vm = $controller('ConvertToOrderCtrl', {$stateParams: stateParams, requisitions: requisitions});
        });
    });

    it('should show all requisitions if default filter is applied', function() {
        expect(vm.searchParams.filterBy).toEqual('all');
        expect(vm.searchParams.filterValue).toEqual('');
        expect(vm.requisitions).toEqual(requisitions);
    });

    it('should reload state with proper params to filter requisitions by facility code', function() {
        vm.searchParams.filterBy = 'facilityCode';
        vm.searchParams.filterValue = 'code1';
        state.current = {name: 'requisitions.convertToOrder'};
        spyOn(state, 'go').andCallThrough();

        vm.reload();

        expect(state.go).toHaveBeenCalledWith('requisitions.convertToOrder', vm.searchParams, {reload: true});
    });

    it('should reload state with proper params to filter requisitions by facility name', function() {
        vm.searchParams.filterBy = 'facilityName';
        vm.searchParams.filterValue = 'facility1';
        state.current = {name: 'requisitions.convertToOrder'};
        spyOn(state, 'go').andCallThrough();

        vm.reload();

        expect(state.go).toHaveBeenCalledWith('requisitions.convertToOrder', vm.searchParams, {reload: true});
    });

    it('should reload state with proper params to filter requisitions by program name', function() {
        vm.searchParams.filterBy = 'programName';
        vm.searchParams.filterValue = 'program1';
        state.current = {name: 'requisitions.convertToOrder'};
        spyOn(state, 'go').andCallThrough();

        vm.reload();

        expect(state.go).toHaveBeenCalledWith('requisitions.convertToOrder', vm.searchParams, {reload: true});
    });

    it('should get all selected requisitions', function() {
        vm.requisitions[0].$selected = true;

        var selectedRequisitions = vm.getSelected();

        expect(selectedRequisitions).toEqual([requisitions[0]]);
    });

    it('should get an empty array if no requisition is selected', function() {
        var selectedRequisitions = vm.getSelected();

        expect(selectedRequisitions).toEqual([]);
    });

    it('should convert to order selected requisitions', function() {
        vm.requisitions[0].$selected = true;
        spyOn(requisitionService, 'convertToOrder').andReturn(q.when());

        vm.convertToOrder();

        expect(requisitionService.convertToOrder).toHaveBeenCalled();
    });

    it('should show error when trying to convert to order with no requisition selected', function() {
        spyOn(requisitionService, 'convertToOrder').andReturn(q.when());
        spyOn(notification, 'error').andCallThrough();

        vm.convertToOrder();

        expect(requisitionService.convertToOrder).not.toHaveBeenCalled();
        expect(notification.error).toHaveBeenCalledWith('msg.select.at.least.one.rnr');
    });

    it('should select all requisitions', function() {
       vm.toggleSelectAll(true);

       expect(vm.requisitions[0].$selected).toBe(true);
       expect(vm.requisitions[1].$selected).toBe(true);
    });

    it('should deselect all requisitions', function() {
        vm.toggleSelectAll(false);

        expect(vm.requisitions[0].$selected).toBe(false);
        expect(vm.requisitions[1].$selected).toBe(false);
{'fraction_non_alphanumeric': 0.1263537906137184, 'fraction_numerical': 0.004770500257864879, 'mean_word_length': 2.412670479542455, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '682986', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2074, 'n_words': 395}