Matteo Scandolo | 46b5610 | 2015-12-16 14:23:08 -0800 | [diff] [blame] | 1 | describe('typeaheadHighlight', function () { |
| 2 | |
| 3 | var highlightFilter, $log, $sce, logSpy; |
| 4 | |
| 5 | beforeEach(module('ui.bootstrap.typeahead')); |
| 6 | |
| 7 | beforeEach(inject(function(_$log_, _$sce_) { |
| 8 | $log = _$log_; |
| 9 | $sce = _$sce_; |
| 10 | logSpy = spyOn($log, 'warn'); |
| 11 | })); |
| 12 | |
| 13 | beforeEach(inject(function(uibTypeaheadHighlightFilter) { |
| 14 | highlightFilter = uibTypeaheadHighlightFilter; |
| 15 | })); |
| 16 | |
| 17 | it('should higlight a match', function() { |
| 18 | expect($sce.getTrustedHtml(highlightFilter('before match after', 'match'))).toEqual('before <strong>match</strong> after'); |
| 19 | }); |
| 20 | |
| 21 | it('should higlight a match with mixed case', function() { |
| 22 | expect($sce.getTrustedHtml(highlightFilter('before MaTch after', 'match'))).toEqual('before <strong>MaTch</strong> after'); |
| 23 | }); |
| 24 | |
| 25 | it('should higlight all matches', function() { |
| 26 | expect($sce.getTrustedHtml(highlightFilter('before MaTch after match', 'match'))).toEqual('before <strong>MaTch</strong> after <strong>match</strong>'); |
| 27 | }); |
| 28 | |
| 29 | it('should do nothing if no match', function() { |
| 30 | expect($sce.getTrustedHtml(highlightFilter('before match after', 'nomatch'))).toEqual('before match after'); |
| 31 | }); |
| 32 | |
| 33 | it('should do nothing if no or empty query', function() { |
| 34 | expect($sce.getTrustedHtml(highlightFilter('before match after', ''))).toEqual('before match after'); |
| 35 | expect($sce.getTrustedHtml(highlightFilter('before match after', null))).toEqual('before match after'); |
| 36 | expect($sce.getTrustedHtml(highlightFilter('before match after', undefined))).toEqual('before match after'); |
| 37 | }); |
| 38 | |
| 39 | it('issue 316 - should work correctly for regexp reserved words', function() { |
| 40 | expect($sce.getTrustedHtml(highlightFilter('before (match after', '(match'))).toEqual('before <strong>(match</strong> after'); |
| 41 | }); |
| 42 | |
| 43 | it('issue 1777 - should work correctly with numeric values', function() { |
| 44 | expect($sce.getTrustedHtml(highlightFilter(123, '2'))).toEqual('1<strong>2</strong>3'); |
| 45 | }); |
| 46 | |
| 47 | it('should show a warning when this component is being used unsafely', function() { |
| 48 | highlightFilter('<i>before</i> match after', 'match'); |
| 49 | expect(logSpy).toHaveBeenCalled(); |
| 50 | }); |
| 51 | }); |
| 52 | |
| 53 | /* Deprecation tests below */ |
| 54 | |
| 55 | describe('typeahead highlightFilter deprecated', function(){ |
| 56 | var highlightFilter, $log, $sce, logSpy; |
| 57 | |
| 58 | beforeEach(module('ui.bootstrap.typeahead')); |
| 59 | |
| 60 | it('should supress the warning by default', function(){ |
| 61 | module(function($provide) { |
| 62 | $provide.value('$typeaheadSuppressWarning', true); |
| 63 | }); |
| 64 | |
| 65 | inject(function($compile, $log, $rootScope, typeaheadHighlightFilter, $sce){ |
| 66 | spyOn($log, 'warn'); |
| 67 | var highlightFilter = typeaheadHighlightFilter; |
| 68 | $sce.getTrustedHtml(highlightFilter('before match after', 'match')); |
| 69 | expect($log.warn.calls.count()).toBe(0); |
| 70 | }); |
| 71 | }); |
| 72 | |
| 73 | it('should decrecate typeaheadHighlightFilter', inject(function($compile, $log, $rootScope, typeaheadHighlightFilter, $sce){ |
| 74 | spyOn($log, 'warn'); |
| 75 | var highlightFilter = typeaheadHighlightFilter; |
| 76 | $sce.getTrustedHtml(highlightFilter('before match after', 'match')); |
| 77 | expect($log.warn.calls.count()).toBe(1); |
| 78 | expect($log.warn.calls.argsFor(0)).toEqual(['typeaheadHighlight is now deprecated. Use uibTypeaheadHighlight instead.']); |
| 79 | })); |
| 80 | }); |