|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +describe('errors', function() { |
| 4 | + // Mock `ngSanitize` module |
| 5 | + angular. |
| 6 | + module('ngSanitize', []). |
| 7 | + value('$sanitize', jasmine.createSpy('$sanitize').andCallFake(angular.identity)); |
| 8 | + |
| 9 | + beforeEach(module('errors')); |
| 10 | + |
| 11 | + |
| 12 | + describe('errorDisplay', function() { |
| 13 | + var $sanitize; |
| 14 | + var errorLinkFilter; |
| 15 | + |
| 16 | + beforeEach(inject(function(_$sanitize_, _errorLinkFilter_) { |
| 17 | + $sanitize = _$sanitize_; |
| 18 | + errorLinkFilter = _errorLinkFilter_; |
| 19 | + })); |
| 20 | + |
| 21 | + |
| 22 | + it('should return empty input unchanged', function() { |
| 23 | + var inputs = [undefined, null, false, 0, '']; |
| 24 | + var remaining = inputs.length; |
| 25 | + |
| 26 | + inputs.forEach(function(falsyValue) { |
| 27 | + expect(errorLinkFilter(falsyValue)).toBe(falsyValue); |
| 28 | + remaining--; |
| 29 | + }); |
| 30 | + |
| 31 | + expect(remaining).toBe(0); |
| 32 | + }); |
| 33 | + |
| 34 | + |
| 35 | + it('should recognize URLs and convert them to `<a>`', function() { |
| 36 | + var urls = [ |
| 37 | + ['ftp://foo/bar?baz#qux'], |
| 38 | + ['http://foo/bar?baz#qux'], |
| 39 | + ['https://foo/bar?baz#qux'], |
| 40 | + |
| 41 | + |
| 42 | + ]; |
| 43 | + var remaining = urls.length; |
| 44 | + |
| 45 | + urls.forEach(function(values) { |
| 46 | + var actualUrl = values[0]; |
| 47 | + var expectedUrl = values[1] || actualUrl; |
| 48 | + var expectedText = values[2] || expectedUrl; |
| 49 | + var anchor = '<a href="' + expectedUrl + '">' + expectedText + '</a>'; |
| 50 | + |
| 51 | + var input = 'start ' + actualUrl + ' end'; |
| 52 | + var output = 'start ' + anchor + ' end'; |
| 53 | + |
| 54 | + expect(errorLinkFilter(input)).toBe(output); |
| 55 | + remaining--; |
| 56 | + }); |
| 57 | + |
| 58 | + expect(remaining).toBe(0); |
| 59 | + }); |
| 60 | + |
| 61 | + |
| 62 | + it('should not recognize stack-traces as URLs', function() { |
| 63 | + var urls = [ |
| 64 | + 'ftp://foo/bar?baz#qux:4:2', |
| 65 | + 'http://foo/bar?baz#qux:4:2', |
| 66 | + 'https://foo/bar?baz#qux:4:2', |
| 67 | + |
| 68 | + |
| 69 | + ]; |
| 70 | + var remaining = urls.length; |
| 71 | + |
| 72 | + urls.forEach(function(url) { |
| 73 | + var input = 'start ' + url + ' end'; |
| 74 | + |
| 75 | + expect(errorLinkFilter(input)).toBe(input); |
| 76 | + remaining--; |
| 77 | + }); |
| 78 | + |
| 79 | + expect(remaining).toBe(0); |
| 80 | + }); |
| 81 | + |
| 82 | + |
| 83 | + it('should should set `[target]` if specified', function() { |
| 84 | + var url = 'https://foo/bar?baz#qux'; |
| 85 | + var target = '_blank'; |
| 86 | + var outputWithoutTarget = '<a href="' + url + '">' + url + '</a>'; |
| 87 | + var outputWithTarget = '<a target="' + target + '" href="' + url + '">' + url + '</a>'; |
| 88 | + |
| 89 | + expect(errorLinkFilter(url)).toBe(outputWithoutTarget); |
| 90 | + expect(errorLinkFilter(url, target)).toBe(outputWithTarget); |
| 91 | + }); |
| 92 | + |
| 93 | + |
| 94 | + it('should truncate the contents of the generated `<a>` to 60 characters', function() { |
| 95 | + var looongUrl = 'https://foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo'; |
| 96 | + var truncatedUrl = 'https://foooooooooooooooooooooooooooooooooooooooooooooooo...'; |
| 97 | + var output = '<a href="' + looongUrl + '">' + truncatedUrl + '</a>'; |
| 98 | + |
| 99 | + expect(looongUrl.length).toBeGreaterThan(60); |
| 100 | + expect(truncatedUrl.length).toBe(60); |
| 101 | + expect(errorLinkFilter(looongUrl)).toBe(output); |
| 102 | + }); |
| 103 | + |
| 104 | + |
| 105 | + it('should pass the final string through `$sanitize`', function() { |
| 106 | + $sanitize.reset(); |
| 107 | + |
| 108 | + var input = 'start https://foo/bar?baz#qux end'; |
| 109 | + var output = errorLinkFilter(input); |
| 110 | + |
| 111 | + expect($sanitize.callCount).toBe(1); |
| 112 | + expect($sanitize).toHaveBeenCalledWith(output); |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + |
| 117 | + describe('errorDisplay', function() { |
| 118 | + var $compile; |
| 119 | + var $location; |
| 120 | + var $rootScope; |
| 121 | + var errorLinkFilter; |
| 122 | + |
| 123 | + beforeEach(module(function($provide) { |
| 124 | + $provide.decorator('errorLinkFilter', function() { |
| 125 | + errorLinkFilter = jasmine.createSpy('errorLinkFilter'); |
| 126 | + errorLinkFilter.andCallFake(angular.identity); |
| 127 | + |
| 128 | + return errorLinkFilter; |
| 129 | + }); |
| 130 | + })); |
| 131 | + beforeEach(inject(function(_$compile_, _$location_, _$rootScope_) { |
| 132 | + $compile = _$compile_; |
| 133 | + $location = _$location_; |
| 134 | + $rootScope = _$rootScope_; |
| 135 | + })); |
| 136 | + |
| 137 | + |
| 138 | + it('should set the element\s HTML', function() { |
| 139 | + var elem = $compile('<span error-display="bar">foo</span>')($rootScope); |
| 140 | + expect(elem.html()).toBe('bar'); |
| 141 | + }); |
| 142 | + |
| 143 | + |
| 144 | + it('should interpolate the contents against `$location.search()`', function() { |
| 145 | + spyOn($location, 'search').andReturn({p0: 'foo', p1: 'bar'}); |
| 146 | + |
| 147 | + var elem = $compile('<span error-display="foo = {0}, bar = {1}"></span>')($rootScope); |
| 148 | + expect(elem.html()).toBe('foo = foo, bar = bar'); |
| 149 | + }); |
| 150 | + |
| 151 | + |
| 152 | + it('should pass the interpolated text through `errorLinkFilter`', function() { |
| 153 | + $location.search = jasmine.createSpy('search').andReturn({p0: 'foo'}); |
| 154 | + |
| 155 | + var elem = $compile('<span error-display="foo = {0}"></span>')($rootScope); |
| 156 | + expect(errorLinkFilter.callCount).toBe(1); |
| 157 | + expect(errorLinkFilter).toHaveBeenCalledWith('foo = foo', '_blank'); |
| 158 | + }); |
| 159 | + |
| 160 | + |
| 161 | + it('should encode `<` and `>`', function() { |
| 162 | + var elem = $compile('<span error-display="<xyz>"></span>')($rootScope); |
| 163 | + expect(elem.text()).toBe('<xyz>'); |
| 164 | + }); |
| 165 | + }); |
| 166 | +}); |
0 commit comments