<commit_before>'use strict';

describe('autoComplete directive', function() {
    var $compile, $scope, $q, $timeout,
        parentCtrl, element, isolateScope, suggestionList, deferred, tagsInput, eventHandlers;

    beforeEach(function() {
        module('ngTagsInput');

        inject(function($rootScope, _$compile_, _$q_, _$timeout_) {
            $scope = $rootScope;
            $compile = _$compile_;
            $q = _$q_;
            $timeout = _$timeout_;
        });

        deferred = $q.defer();
        eventHandlers = {};
        $scope.loadItems = jasmine.createSpy().and.returnValue(deferred.promise);

        compile();
    });

    function compile() {
        var parent, options;

        tagsInput = {
            changeInputValue: jasmine.createSpy(),
            addTag: jasmine.createSpy(),
            focusInput: jasmine.createSpy(),
            on: jasmine.createSpy().and.callFake(function(names, handler) {
                names.split(' ').forEach(function(name) { eventHandlers[name] = handler; });
                return this;
            }),
            getTags: jasmine.createSpy().and.returnValue([]),
            getCurrentTagText: jasmine.createSpy(),
            getOptions: jasmine.createSpy().and.returnValue({
                displayProperty: 'text'
            })
        };

        parent = $compile('<tags-input ng-model="whatever"></tags-input>')($scope);
        $scope.$digest();

        parentCtrl = parent.controller('tagsInput');
        spyOn(parentCtrl, 'registerAutocomplete').and.returnValue(tagsInput);

        options = jQuery.makeArray(arguments).join(' ');
        element = angular.element('<auto-complete source="loadItems($query)" ' + options + '></auto-complete>');
        parent.append(element);

        $compile(element)($scope);
        $scope.$digest();

        isolateScope = element.isolateScope();
        suggestionList = isolateScope.suggestionList;
    }

    function resolve(items) {
        deferred.resolve(items);
        $scope.$digest();
    }

    function sendKeyDown(keyCode) {
        var event = jQuery.Event('keydown', { keyCode: keyCode });
        eventHandlers['input-keydown'](event);

        return event;
    }

    function changeInputValue(value) {
        eventHandlers['input-change'](value);
        $scope.$digest();
    }

    function getSuggestionsBox() {
        return element.find('div');
    }

    function getSuggestions() {
        return getSuggestionsBox().find('li');
    }

    function getSuggestion(index) {
        return getSuggestions().eq(index);
    }

    function getSuggestionText(index) {
        return getSuggestion(index).html();
    }

    function isSuggestionsBoxVisible() {
        return !getSuggestionsBox().hasClass('ng-hide');
    }

    function generateSuggestions(count) {
        return generateArray(count, function(index) {
            return { text: 'Item' + index };
        });
    }

    function loadSuggestions(countOrItems, text) {
        var items = angular.isNumber(countOrItems) ? generateSuggestions(countOrItems) : countOrItems;
        text = angular.isUndefined(text) ? 'foobar' : text;

        suggestionList.load(text, tagsInput.getTags());
        $timeout.flush();
        resolve(items);
    }

    describe('basic features', function() {
        it('ensures that the suggestions list is hidden by default', function() {
            expect(isSuggestionsBoxVisible()).toBe(false);
        });

        it('loads values from the load function even if the return value is not a promise', function() {
            // Arrange
            $scope.loadItems = jasmine.createSpy().and.returnValue(generateSuggestions(3));
            // Act
            suggestionList.load('', []);
            $timeout.flush();
            // Assert
            expect(getSuggestions().length).toBe(3);
        });

        it('renders all elements returned by the load function that aren\'t already added', function() {
            // Act
            tagsInput.getTags.and.returnValue([{ text: 'Item3' }]);
            loadSuggestions(3);

            // Assert
            expect(getSuggestions().length).toBe(2);
            expect(getSuggestionText(0)).toBe('Item1');
            expect(getSuggestionText(1)).toBe('Item2');
        });

        it('renders all elements returned by the load function that aren\'t already added ($http promise)', function() {
            // Act
            tagsInput.getTags.and.returnValue([{ text: 'Item3' }]);
            loadSuggestions({ data: generateSuggestions(3)});

            // Assert
            expect(getSuggestions().length).toBe(2);
            expect(getSuggestionText(0)).toBe('Item1');
            expect(getSuggestionText(1)).toBe('Item2');
        });

        it('renders all elements returned by the load function that aren\'t already added (non-string items)', function() {
            // Act
            tagsInput.getTags.and.returnValue([{ text: '1' }]);
            loadSuggestions({
                data: [
                    { text: 1 },
                    { text: 1.5 },
                    { text: true },
                    { text: {} },
                    { text: null },
                    { text: undefined }
                ]
            });

            // Assert
            expect(getSuggestions().length).toBe(5);
            expect(getSuggestionText(0)).toBe('1.5');
            expect(getSuggestionText(1)).toBe('true');
            expect(getSuggestionText(2)).toBe('[object Object]');
            expect(getSuggestionText(3)).toBe('');
            expect(getSuggestionText(4)).toBe('');
        });

        it('shows the suggestions list when there are items to show', function() {<commit_msg>test(autocomplete): Refactor test case<commit_after>'use strict';

describe('autoComplete directive', function() {
    var $compile, $scope, $q, $timeout,
        parentCtrl, element, isolateScope, suggestionList, deferred, tagsInput, eventHandlers;

    beforeEach(function() {
        module('ngTagsInput');

        inject(function($rootScope, _$compile_, _$q_, _$timeout_) {
            $scope = $rootScope;
            $compile = _$compile_;
            $q = _$q_;
            $timeout = _$timeout_;
        });

        deferred = $q.defer();
        eventHandlers = {};
        $scope.loadItems = jasmine.createSpy().and.returnValue(deferred.promise);

        compile();
    });

    function compile() {
        var parent, options;

        tagsInput = {
            changeInputValue: jasmine.createSpy(),
            addTag: jasmine.createSpy(),
            focusInput: jasmine.createSpy(),
            on: jasmine.createSpy().and.callFake(function(names, handler) {
                names.split(' ').forEach(function(name) { eventHandlers[name] = handler; });
                return this;
            }),
            getTags: jasmine.createSpy().and.returnValue([]),
            getCurrentTagText: jasmine.createSpy(),
            getOptions: jasmine.createSpy().and.returnValue({
                displayProperty: 'text'
            })
        };

        parent = $compile('<tags-input ng-model="whatever"></tags-input>')($scope);
        $scope.$digest();

        parentCtrl = parent.controller('tagsInput');
        spyOn(parentCtrl, 'registerAutocomplete').and.returnValue(tagsInput);

        options = jQuery.makeArray(arguments).join(' ');
        element = angular.element('<auto-complete source="loadItems($query)" ' + options + '></auto-complete>');
        parent.append(element);

        $compile(element)($scope);
        $scope.$digest();

        isolateScope = element.isolateScope();
        suggestionList = isolateScope.suggestionList;
    }

    function resolve(items) {
        deferred.resolve(items);
        $scope.$digest();
    }

    function sendKeyDown(keyCode) {
        var event = jQuery.Event('keydown', { keyCode: keyCode });
        eventHandlers['input-keydown'](event);

        return event;
    }

    function changeInputValue(value) {
        eventHandlers['input-change'](value);
        $scope.$digest();
    }

    function getSuggestionsBox() {
        return element.find('div');
    }

    function getSuggestions() {
        return getSuggestionsBox().find('li');
    }

    function getSuggestion(index) {
        return getSuggestions().eq(index);
    }

    function getSuggestionText(index) {
        return getSuggestion(index).html();
    }

    function isSuggestionsBoxVisible() {
        return !getSuggestionsBox().hasClass('ng-hide');
    }

    function generateSuggestions(count) {
        return generateArray(count, function(index) {
            return { text: 'Item' + index };
        });
    }

    function loadSuggestions(countOrItems, text) {
        var items = angular.isNumber(countOrItems) ? generateSuggestions(countOrItems) : countOrItems;
        text = angular.isUndefined(text) ? 'foobar' : text;

        suggestionList.load(text, tagsInput.getTags());
        $timeout.flush();
        resolve(items);
    }

    describe('basic features', function() {
        it('ensures that the suggestions list is hidden by default', function() {
            expect(isSuggestionsBoxVisible()).toBe(false);
        });

        it('renders all elements returned by the load function that aren\'t already added', function() {
            // Act
            tagsInput.getTags.and.returnValue([{ text: 'Item3' }]);
            loadSuggestions(3);

            // Assert
            expect(getSuggestions().length).toBe(2);
            expect(getSuggestionText(0)).toBe('Item1');
            expect(getSuggestionText(1)).toBe('Item2');
        });

        it('renders all elements returned by the load function that aren\'t already added ($http promise)', function() {
            // Act
            tagsInput.getTags.and.returnValue([{ text: 'Item3' }]);
            loadSuggestions({ data: generateSuggestions(3)});

            // Assert
            expect(getSuggestions().length).toBe(2);
            expect(getSuggestionText(0)).toBe('Item1');
            expect(getSuggestionText(1)).toBe('Item2');
        });

        it('renders all elements returned by the load function that aren\'t already added (non-string items)', function() {
            // Act
            tagsInput.getTags.and.returnValue([{ text: '1' }]);
            loadSuggestions({
                data: [
                    { text: 1 },
                    { text: 1.5 },
                    { text: true },
                    { text: {} },
                    { text: null },
                    { text: undefined }
                ]
            });

            // Assert
            expect(getSuggestions().length).toBe(5);
            expect(getSuggestionText(0)).toBe('1.5');
            expect(getSuggestionText(1)).toBe('true');
            expect(getSuggestionText(2)).toBe('[object Object]');
            expect(getSuggestionText(3)).toBe('');
            expect(getSuggestionText(4)).toBe('');
        });

        it('renders all elements returned by the load function that aren\'t already added (non-promise)', function() {
            // Arrange
            tagsInput.getTags.and.returnValue([{ text: 'Item3' }]);
            $scope.loadItems = jasmine.createSpy().and.returnValue(generateSuggestions(3));

            // Act
            suggestionList.load('', tagsInput.getTags());
            $timeout.flush();

            // Assert
            expect(getSuggestions().length).toBe(2);
            expect(getSuggestionText(0)).toBe('Item1');
            expect(getSuggestionText(1)).toBe('Item2');
        });

        it('shows the suggestions list when there are items to show', function() {
{'fraction_non_alphanumeric': 0.13975367772836128, 'fraction_numerical': 0.005217242559014711, 'mean_word_length': 2.1534519956850056, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5972130', 'n_tokens_mistral': 3257, 'n_tokens_neox': 3062, 'n_words': 583}