|
15 | 15 | */ |
16 | 16 |
|
17 | 17 | import * as assert from 'assert'; |
| 18 | +import * as sinon from 'sinon'; |
18 | 19 | import { |
19 | 20 | isAttributeValue, |
20 | 21 | sanitizeAttributes, |
| 22 | + truncateValueIfTooLong, |
21 | 23 | } from '../../src/common/attributes'; |
22 | 24 |
|
23 | 25 | describe('attributes', () => { |
@@ -103,4 +105,62 @@ describe('attributes', () => { |
103 | 105 | assert.strictEqual(attributes.arr[0], 'unmodified'); |
104 | 106 | }); |
105 | 107 | }); |
| 108 | + describe('#truncateValueIfTooLong', () => { |
| 109 | + it('should not truncate any given value if the limit is not set', () => { |
| 110 | + assert.strictEqual(truncateValueIfTooLong('a', null), 'a'); |
| 111 | + assert.strictEqual(truncateValueIfTooLong(1, null), 1); |
| 112 | + assert.strictEqual(truncateValueIfTooLong(true, null), true); |
| 113 | + |
| 114 | + const arrayRef: string[] = []; |
| 115 | + assert.strictEqual(truncateValueIfTooLong(arrayRef, null), arrayRef); |
| 116 | + }); |
| 117 | + |
| 118 | + it('passes numbers and bools through', () => { |
| 119 | + assert.strictEqual(truncateValueIfTooLong(true, 32), true); |
| 120 | + assert.strictEqual(truncateValueIfTooLong(false, 32), false); |
| 121 | + assert.strictEqual(truncateValueIfTooLong(1, 32), 1); |
| 122 | + }); |
| 123 | + |
| 124 | + it('truncates strings if they are longer than the limit', () => { |
| 125 | + assert.strictEqual( |
| 126 | + truncateValueIfTooLong('a'.repeat(100), 100), |
| 127 | + 'a'.repeat(100) |
| 128 | + ); |
| 129 | + assert.strictEqual( |
| 130 | + truncateValueIfTooLong('a'.repeat(101), 100), |
| 131 | + 'a'.repeat(100) |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it('serializes and truncates arrays if they are longer than the limit', () => { |
| 136 | + assert.strictEqual( |
| 137 | + truncateValueIfTooLong(['a'.repeat(100)], 32), |
| 138 | + '["' + 'a'.repeat(30) |
| 139 | + ); |
| 140 | + assert.strictEqual( |
| 141 | + truncateValueIfTooLong( |
| 142 | + [...new Array(10).keys()].map(() => 1000), |
| 143 | + 32 |
| 144 | + ), |
| 145 | + '[' + '1000,'.repeat(6) + '1' |
| 146 | + ); |
| 147 | + assert.strictEqual( |
| 148 | + truncateValueIfTooLong( |
| 149 | + [...new Array(10).keys()].map(() => true), |
| 150 | + 32 |
| 151 | + ), |
| 152 | + '[' + 'true,'.repeat(6) + 't' |
| 153 | + ); |
| 154 | + }); |
| 155 | + |
| 156 | + it('executes callback if a value was truncated', () => { |
| 157 | + const fakeCallback = sinon.spy(); |
| 158 | + |
| 159 | + truncateValueIfTooLong('a'.repeat(32), 32, fakeCallback); |
| 160 | + assert.ok(!fakeCallback.called); |
| 161 | + |
| 162 | + truncateValueIfTooLong('a'.repeat(33), 32, fakeCallback); |
| 163 | + assert(fakeCallback.called); |
| 164 | + }); |
| 165 | + }); |
106 | 166 | }); |
0 commit comments