| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
) |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
describe("Truncate.truncate.TruncateSmart", function() { |
| 10 |
|
| 11 |
it("Will not truncate a URL which is shorter than the specified length", function() { |
| 12 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 999, ".."); |
| 13 |
expect(truncatedUrl).toBe("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee"); |
| 14 |
expect(truncatedUrl.length).toBe(72); |
| 15 |
}); |
| 16 |
|
| 17 |
|
| 18 |
it("Will remove malformed query section 1st", function() { |
| 19 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 61, ".."); |
| 20 |
expect(truncatedUrl).toBe("http://www.yahoo.com/some/long/path/to/a/file?foo=bar#baz=bee"); |
| 21 |
expect(truncatedUrl.length).toBe(61); |
| 22 |
}); |
| 23 |
|
| 24 |
|
| 25 |
it("Will remove 'www' 2nd", function() { |
| 26 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 57, ".."); |
| 27 |
expect(truncatedUrl).toBe("http://yahoo.com/some/long/path/to/a/file?foo=bar#baz=bee"); |
| 28 |
expect(truncatedUrl.length).toBe(57); |
| 29 |
}); |
| 30 |
|
| 31 |
|
| 32 |
it("Will remove scheme ('http') 3rd", function() { |
| 33 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 53, ".."); |
| 34 |
expect(truncatedUrl).toBe("yahoo.com/some/long/path/to/a/file?foo=bar#baz=bee"); |
| 35 |
expect(truncatedUrl.length).toBe(50); |
| 36 |
}); |
| 37 |
|
| 38 |
|
| 39 |
it("Will truncate fragment ('#') 4th", function() { |
| 40 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 45, ".."); |
| 41 |
expect(truncatedUrl).toBe("yahoo.com/some/long/path/to/a/file?foo=bar#.."); |
| 42 |
expect(truncatedUrl.length).toBe(45); |
| 43 |
}); |
| 44 |
|
| 45 |
|
| 46 |
it("Will truncate path ('/') 5th", function() { |
| 47 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorezthis#baz=bee", 40, ".."); |
| 48 |
expect(truncatedUrl).toBe("yahoo.com/some/long/path..a/file?foo=bar"); |
| 49 |
expect(truncatedUrl.length).toBe(40); |
| 50 |
}); |
| 51 |
|
| 52 |
|
| 53 |
it("Will keep fragment when there is room for it", function() { |
| 54 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 50, ".."); |
| 55 |
expect(truncatedUrl).toBe("yahoo.com/some/long/path/to/a/file?foo=bar#baz=bee"); |
| 56 |
expect(truncatedUrl.length).toBe(50); |
| 57 |
}); |
| 58 |
|
| 59 |
|
| 60 |
it("Will remove/truncate 'path' before 'host'", function() { |
| 61 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 12, ".."); |
| 62 |
expect(truncatedUrl).toBe("yahoo.com/.."); |
| 63 |
expect(truncatedUrl.length).toBe(12); |
| 64 |
}); |
| 65 |
|
| 66 |
|
| 67 |
it("Will truncate 'host' when 'truncateLen' is very short", function() { |
| 68 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 7, ".."); |
| 69 |
expect(truncatedUrl).toBe("yah..om"); |
| 70 |
expect(truncatedUrl.length).toBe(7); |
| 71 |
}); |
| 72 |
|
| 73 |
|
| 74 |
it("Will truncate 'path' when no 'query' exists", function() { |
| 75 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file#baz=bee", 17, ".."); |
| 76 |
expect(truncatedUrl).toBe("yahoo.com/so..ile"); |
| 77 |
expect(truncatedUrl.length).toBe(17); |
| 78 |
}); |
| 79 |
|
| 80 |
|
| 81 |
it("Will truncate 'query' when no 'path' exists", function() { |
| 82 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com?foo=bar?ignorethis#baz=bee", 17, ".."); |
| 83 |
expect(truncatedUrl).toBe("yahoo.com?foo=bar"); |
| 84 |
expect(truncatedUrl.length).toBe(17); |
| 85 |
}); |
| 86 |
|
| 87 |
|
| 88 |
it("Works when no 'scheme' or 'host' exists", function() { |
| 89 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 17, ".."); |
| 90 |
expect(truncatedUrl).toBe("/some/lo..foo=bar"); |
| 91 |
expect(truncatedUrl.length).toBe(17); |
| 92 |
}); |
| 93 |
|
| 94 |
|
| 95 |
it("Works when only 'query' exists", function() { |
| 96 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("?foo=bar?ignorethis#baz=bee", 15, ".."); |
| 97 |
expect(truncatedUrl).toBe("?foo=bar#ba..ee"); |
| 98 |
expect(truncatedUrl.length).toBe(15); |
| 99 |
}); |
| 100 |
|
| 101 |
|
| 102 |
it("Works when only 'fragment' exists", function() { |
| 103 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("#baz=bee", 5, ".."); |
| 104 |
expect(truncatedUrl).toBe("#b..e"); |
| 105 |
expect(truncatedUrl.length).toBe(5); |
| 106 |
}); |
| 107 |
|
| 108 |
|
| 109 |
it("Works with a standard Google search URL", function() { |
| 110 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("https://www.google.com/search?q=cake&oq=cake&aqs=chrome..69i57j69i60l5.573j0j7&sourceid=chrome&es_sm=93&ie=UTF-8", 80, ".."); |
| 111 |
expect(truncatedUrl).toBe("google.com/search?q=cake&oq=cake&aqs=chrome...&sourceid=chrome&es_sm=93&ie=UTF-8"); |
| 112 |
expect(truncatedUrl.length).toBe(80); |
| 113 |
}); |
| 114 |
|
| 115 |
|
| 116 |
it("Works with a long URL", function() { |
| 117 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("https://www.google.com/search?q=cake&safe=off&es_sm=93&tbas=0&tbs=qdr:d,itp:photo,ic:specific,isc:red,isz:l&tbm=isch&source=lnt&sa=X&ved=0CBQQpwVqFQoTCMCUxfOErMgCFeFrcgodUDwD1w&dpr=1&biw=1920&bih=955", 80, ".."); |
| 118 |
expect(truncatedUrl).toBe("google.com/search?q=cake&safe=off&es_sm=93&t..rcgodUDwD1w&dpr=1&biw=1920&bih=955"); |
| 119 |
expect(truncatedUrl.length).toBe(80); |
| 120 |
}); |
| 121 |
|
| 122 |
|
| 123 |
it("Will start with a character from the URL and then append the ellipsis character(s)", function() { |
| 124 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 3, ".."); |
| 125 |
expect(truncatedUrl).toBe("y.."); |
| 126 |
expect(truncatedUrl.length).toBe(3); |
| 127 |
}); |
| 128 |
|
| 129 |
|
| 130 |
it("Will write only the ellipsis character(s) ('..') when truncate length is 2", function() { |
| 131 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 2, ".."); |
| 132 |
expect(truncatedUrl).toBe(".."); |
| 133 |
expect(truncatedUrl.length).toBe(2); |
| 134 |
}); |
| 135 |
|
| 136 |
|
| 137 |
it("Will write only the ellipsis character ('.') when truncate length is 1", function() { |
| 138 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 1, ".."); |
| 139 |
expect(truncatedUrl).toBe("."); |
| 140 |
expect(truncatedUrl.length).toBe(1); |
| 141 |
}); |
| 142 |
|
| 143 |
|
| 144 |
it("Will write nothing (empty string) when truncate length is 0", function() { |
| 145 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("http://www.yahoo.com/some/long/path/to/a/file?foo=bar?ignorethis#baz=bee", 0, ".."); |
| 146 |
expect(truncatedUrl).toBe(""); |
| 147 |
expect(truncatedUrl.length).toBe(0); |
| 148 |
}); |
| 149 |
|
| 150 |
|
| 151 |
it("Will truncate 'hashtag routing' nicely", function() { |
| 152 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("/app#my/little/hashtag/route", 20, ".."); |
| 153 |
expect(truncatedUrl).toBe("/app#my/lit..g/route"); |
| 154 |
expect(truncatedUrl.length).toBe(20); |
| 155 |
}); |
| 156 |
|
| 157 |
|
| 158 |
it("Allows a 'query' section in 'hashtag routing'", function() { |
| 159 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("/app#my/little/hashtag/route?foo=bar", 20, ".."); |
| 160 |
expect(truncatedUrl).toBe("/app#my/lit..foo=bar"); |
| 161 |
expect(truncatedUrl.length).toBe(20); |
| 162 |
}); |
| 163 |
|
| 164 |
|
| 165 |
it("Removes 'hashtag routing' when truncate length is very small", function() { |
| 166 |
var truncatedUrl = Autolinker.truncate.TruncateSmart("/app#my/little/hashtag/route?foo=bar", 4, ".."); |
| 167 |
expect(truncatedUrl).toBe("/app"); |
| 168 |
expect(truncatedUrl.length).toBe(4); |
| 169 |
}); |
| 170 |
|
| 171 |
}); |