From 735c091090f0a49f1900bf8358ef1d1847b52519 Mon Sep 17 00:00:00 2001 From: Thomas Steur <thomas.steur@googlemail.com> Date: Mon, 31 Mar 2014 00:39:30 +0200 Subject: [PATCH] refs #4691 added more example tests and fixed a bug found because of the tests --- .../common/directives/autocomplete-matched.js | 3 +- .../directives/autocomplete-matched_test.js | 43 +++++++++++++++++++ .../common/filters/startfrom_test.js | 16 +++---- .../angularjs/common/services/piwik_test.js | 37 ++++++++++++++++ tests/angularjs/README.md | 2 + tests/angularjs/karma.conf.js | 7 +++ 6 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 plugins/CoreHome/angularjs/common/directives/autocomplete-matched_test.js create mode 100644 plugins/CoreHome/angularjs/common/services/piwik_test.js diff --git a/plugins/CoreHome/angularjs/common/directives/autocomplete-matched.js b/plugins/CoreHome/angularjs/common/directives/autocomplete-matched.js index a052482ce6..a78154b49a 100644 --- a/plugins/CoreHome/angularjs/common/directives/autocomplete-matched.js +++ b/plugins/CoreHome/angularjs/common/directives/autocomplete-matched.js @@ -30,7 +30,8 @@ angular.module('piwikApp.directive').directive('piwikAutocompleteMatched', funct } var content = element.html(); - var startTerm = content.toLowerCase().indexOf(searchTerm); + var startTerm = content.toLowerCase().indexOf(searchTerm.toLowerCase()); + if (-1 !== startTerm) { var word = content.substr(startTerm, searchTerm.length); content = content.replace(word, '<span class="autocompleteMatched">' + word + '</span>'); diff --git a/plugins/CoreHome/angularjs/common/directives/autocomplete-matched_test.js b/plugins/CoreHome/angularjs/common/directives/autocomplete-matched_test.js new file mode 100644 index 0000000000..2518687b97 --- /dev/null +++ b/plugins/CoreHome/angularjs/common/directives/autocomplete-matched_test.js @@ -0,0 +1,43 @@ +/*! + * Piwik - Web Analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +describe('piwikAutocompleteMatchedDirective', function() { + var $compile; + var $rootScope; + + beforeEach(module('piwikApp.directive')); + beforeEach(inject(function(_$compile_, _$rootScope_){ + $compile = _$compile_; + $rootScope = _$rootScope_; + })); + + function assertRenderedContentIs(query, expectedResult) { + var template = '<div piwik-autocomplete-matched="\'' + query + '\'">My Content</div>'; + var element = $compile(template)($rootScope); + $rootScope.$digest(); + expect(element.html()).to.eql(expectedResult); + } + + describe('#piwikAutocompleteMatched()', function() { + + it('should not change anything if query does not match the text', function() { + assertRenderedContentIs('Whatever', 'My Content'); + }); + + it('should wrap the matching part and find case insensitive', function() { + assertRenderedContentIs('y cont', 'M<span class="autocompleteMatched">y Cont</span>ent'); + }); + + it('should be able to wrap the whole content', function() { + assertRenderedContentIs('my content', '<span class="autocompleteMatched">My Content</span>'); + }); + + it('should find matching content case sensitive', function() { + assertRenderedContentIs('My Co', '<span class="autocompleteMatched">My Co</span>ntent'); + }); + }); +}); \ No newline at end of file diff --git a/plugins/CoreHome/angularjs/common/filters/startfrom_test.js b/plugins/CoreHome/angularjs/common/filters/startfrom_test.js index cd12279c39..d18c99e781 100644 --- a/plugins/CoreHome/angularjs/common/filters/startfrom_test.js +++ b/plugins/CoreHome/angularjs/common/filters/startfrom_test.js @@ -5,18 +5,16 @@ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ -describe('startFromFilter filter', function() { +describe('startFromFilter', function() { var startFrom; - beforeEach(function() { - module('piwikApp.filter'); - inject(function($injector) { - var $filter = $injector.get('$filter'); - startFrom = $filter('startFrom'); - }); - }); + beforeEach(module('piwikApp.filter')); + beforeEach(inject(function($injector) { + var $filter = $injector.get('$filter'); + startFrom = $filter('startFrom'); + })); - describe('startFrom', function() { + describe('#startFrom()', function() { it('should return all entries if index is zero', function() { diff --git a/plugins/CoreHome/angularjs/common/services/piwik_test.js b/plugins/CoreHome/angularjs/common/services/piwik_test.js new file mode 100644 index 0000000000..7aa4ef4eb1 --- /dev/null +++ b/plugins/CoreHome/angularjs/common/services/piwik_test.js @@ -0,0 +1,37 @@ +/*! + * Piwik - Web Analytics + * + * @link http://piwik.org + * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later + */ + +describe('piwikService', function() { + var piwikService; + + beforeEach(module('piwikApp.service')); + beforeEach(inject(function($injector) { + piwikService = $injector.get('piwik'); + })); + + describe('#piwikService', function() { + + it('should be the same as piwik global var', function() { + piwik.should.equal(piwikService); + }); + + it('should mixin broadcast', function() { + expect(piwikService.broadcast).to.be.an('object'); + }); + + it('should mixin piwikHelper', function() { + expect(piwikService.helper).to.be.an('object'); + }); + }); + + describe('#piwik_url', function() { + + it('should contain the piwik url', function() { + expect(piwikService.piwik_url).to.eql('http://localhost/'); + }); + }); +}); \ No newline at end of file diff --git a/tests/angularjs/README.md b/tests/angularjs/README.md index fd8465c4b7..440cc39f5e 100644 --- a/tests/angularjs/README.md +++ b/tests/angularjs/README.md @@ -43,4 +43,6 @@ Just in case you want to write a test for your jQuery code you can do this the s ## Examples * [Testing a filter](plugins/CoreHome/angularjs/common/filters/startfrom_test.js) +* [Testing a directive](plugins/CoreHome/angularjs/common/directives/autocomplete-matched_test.js) +* [Testing a service/provider/factory/model](plugins/CoreHome/angularjs/common/services/piwik_test.js) * See more examples in [AngularJS guide](http://docs.angularjs.org/guide/unit-testing) diff --git a/tests/angularjs/karma.conf.js b/tests/angularjs/karma.conf.js index 228379cc62..a5b3f7943a 100755 --- a/tests/angularjs/karma.conf.js +++ b/tests/angularjs/karma.conf.js @@ -18,8 +18,15 @@ module.exports = function(config) { 'tests/angularjs/node_modules/chai/chai.js', 'tests/angularjs/bootstrap.js', 'libs/angularjs/angular.min.js', + "libs/angularjs/angular-sanitize.min.js", + "libs/angularjs/angular-animate.min.js", 'libs/angularjs/angular-mocks.js', 'libs/jquery/jquery.js', + "libs/jquery/jquery-ui.js", + "plugins/CoreHome/javascripts/require.js", + "plugins/Zeitgeist/javascripts/piwikHelper.js", + "plugins/Zeitgeist/javascripts/ajaxHelper.js", + "plugins/CoreHome/javascripts/broadcast.js", 'plugins/CoreHome/angularjs/common/services/service.js', 'plugins/CoreHome/angularjs/common/filters/filter.js', 'plugins/CoreHome/angularjs/common/directives/directive.js', -- GitLab