cloudflare/cloudflare-typescript
Publicmirrored fromhttps://github.com/cloudflare/cloudflare-typescriptAvailable
tests/qs/utils.test.ts
169lines · modecode
| 1 | import { combine, merge, is_buffer, assign_single_source } from 'cloudflare/internal/qs/utils'; |
| 2 | |
| 3 | describe('merge()', function () { |
| 4 | // t.deepEqual(merge(null, true), [null, true], 'merges true into null'); |
| 5 | expect(merge(null, true)).toEqual([null, true]); |
| 6 | |
| 7 | // t.deepEqual(merge(null, [42]), [null, 42], 'merges null into an array'); |
| 8 | expect(merge(null, [42])).toEqual([null, 42]); |
| 9 | |
| 10 | // t.deepEqual( |
| 11 | // merge({ a: 'b' }, { a: 'c' }), |
| 12 | // { a: ['b', 'c'] }, |
| 13 | // 'merges two objects with the same key', |
| 14 | // ); |
| 15 | expect(merge({ a: 'b' }, { a: 'c' })).toEqual({ a: ['b', 'c'] }); |
| 16 | |
| 17 | var oneMerged = merge({ foo: 'bar' }, { foo: { first: '123' } }); |
| 18 | // t.deepEqual( |
| 19 | // oneMerged, |
| 20 | // { foo: ['bar', { first: '123' }] }, |
| 21 | // 'merges a standalone and an object into an array', |
| 22 | // ); |
| 23 | expect(oneMerged).toEqual({ foo: ['bar', { first: '123' }] }); |
| 24 | |
| 25 | var twoMerged = merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } }); |
| 26 | // t.deepEqual( |
| 27 | // twoMerged, |
| 28 | // { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, |
| 29 | // 'merges a standalone and two objects into an array', |
| 30 | // ); |
| 31 | expect(twoMerged).toEqual({ foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }); |
| 32 | |
| 33 | var sandwiched = merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' }); |
| 34 | // t.deepEqual( |
| 35 | // sandwiched, |
| 36 | // { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, |
| 37 | // 'merges an object sandwiched by two standalones into an array', |
| 38 | // ); |
| 39 | expect(sandwiched).toEqual({ foo: ['bar', { first: '123', second: '456' }, 'baz'] }); |
| 40 | |
| 41 | var nestedArrays = merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] }); |
| 42 | // t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] }); |
| 43 | expect(nestedArrays).toEqual({ foo: ['baz', 'bar', 'xyzzy'] }); |
| 44 | |
| 45 | var noOptionsNonObjectSource = merge({ foo: 'baz' }, 'bar'); |
| 46 | // t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true }); |
| 47 | expect(noOptionsNonObjectSource).toEqual({ foo: 'baz', bar: true }); |
| 48 | |
| 49 | (typeof Object.defineProperty !== 'function' ? test.skip : test)( |
| 50 | 'avoids invoking array setters unnecessarily', |
| 51 | function () { |
| 52 | var setCount = 0; |
| 53 | var getCount = 0; |
| 54 | var observed: any[] = []; |
| 55 | Object.defineProperty(observed, 0, { |
| 56 | get: function () { |
| 57 | getCount += 1; |
| 58 | return { bar: 'baz' }; |
| 59 | }, |
| 60 | set: function () { |
| 61 | setCount += 1; |
| 62 | }, |
| 63 | }); |
| 64 | merge(observed, [null]); |
| 65 | // st.equal(setCount, 0); |
| 66 | // st.equal(getCount, 1); |
| 67 | expect(setCount).toEqual(0); |
| 68 | expect(getCount).toEqual(1); |
| 69 | observed[0] = observed[0]; // eslint-disable-line no-self-assign |
| 70 | // st.equal(setCount, 1); |
| 71 | // st.equal(getCount, 2); |
| 72 | expect(setCount).toEqual(1); |
| 73 | expect(getCount).toEqual(2); |
| 74 | }, |
| 75 | ); |
| 76 | }); |
| 77 | |
| 78 | test('assign()', function () { |
| 79 | var target = { a: 1, b: 2 }; |
| 80 | var source = { b: 3, c: 4 }; |
| 81 | var result = assign_single_source(target, source); |
| 82 | |
| 83 | expect(result).toEqual(target); |
| 84 | expect(target).toEqual({ a: 1, b: 3, c: 4 }); |
| 85 | expect(source).toEqual({ b: 3, c: 4 }); |
| 86 | }); |
| 87 | |
| 88 | describe('combine()', function () { |
| 89 | test('both arrays', function () { |
| 90 | var a = [1]; |
| 91 | var b = [2]; |
| 92 | var combined = combine(a, b); |
| 93 | |
| 94 | // st.deepEqual(a, [1], 'a is not mutated'); |
| 95 | // st.deepEqual(b, [2], 'b is not mutated'); |
| 96 | // st.notEqual(a, combined, 'a !== combined'); |
| 97 | // st.notEqual(b, combined, 'b !== combined'); |
| 98 | // st.deepEqual(combined, [1, 2], 'combined is a + b'); |
| 99 | expect(a).toEqual([1]); |
| 100 | expect(b).toEqual([2]); |
| 101 | expect(combined).toEqual([1, 2]); |
| 102 | expect(a).not.toEqual(combined); |
| 103 | expect(b).not.toEqual(combined); |
| 104 | }); |
| 105 | |
| 106 | test('one array, one non-array', function () { |
| 107 | var aN = 1; |
| 108 | var a = [aN]; |
| 109 | var bN = 2; |
| 110 | var b = [bN]; |
| 111 | |
| 112 | var combinedAnB = combine(aN, b); |
| 113 | // st.deepEqual(b, [bN], 'b is not mutated'); |
| 114 | // st.notEqual(aN, combinedAnB, 'aN + b !== aN'); |
| 115 | // st.notEqual(a, combinedAnB, 'aN + b !== a'); |
| 116 | // st.notEqual(bN, combinedAnB, 'aN + b !== bN'); |
| 117 | // st.notEqual(b, combinedAnB, 'aN + b !== b'); |
| 118 | // st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array'); |
| 119 | expect(b).toEqual([bN]); |
| 120 | expect(combinedAnB).not.toEqual(aN); |
| 121 | expect(combinedAnB).not.toEqual(a); |
| 122 | expect(combinedAnB).not.toEqual(bN); |
| 123 | expect(combinedAnB).not.toEqual(b); |
| 124 | expect(combinedAnB).toEqual([1, 2]); |
| 125 | |
| 126 | var combinedABn = combine(a, bN); |
| 127 | // st.deepEqual(a, [aN], 'a is not mutated'); |
| 128 | // st.notEqual(aN, combinedABn, 'a + bN !== aN'); |
| 129 | // st.notEqual(a, combinedABn, 'a + bN !== a'); |
| 130 | // st.notEqual(bN, combinedABn, 'a + bN !== bN'); |
| 131 | // st.notEqual(b, combinedABn, 'a + bN !== b'); |
| 132 | // st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array'); |
| 133 | expect(a).toEqual([aN]); |
| 134 | expect(combinedABn).not.toEqual(aN); |
| 135 | expect(combinedABn).not.toEqual(a); |
| 136 | expect(combinedABn).not.toEqual(bN); |
| 137 | expect(combinedABn).not.toEqual(b); |
| 138 | expect(combinedABn).toEqual([1, 2]); |
| 139 | }); |
| 140 | |
| 141 | test('neither is an array', function () { |
| 142 | var combined = combine(1, 2); |
| 143 | // st.notEqual(1, combined, '1 + 2 !== 1'); |
| 144 | // st.notEqual(2, combined, '1 + 2 !== 2'); |
| 145 | // st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array'); |
| 146 | expect(combined).not.toEqual(1); |
| 147 | expect(combined).not.toEqual(2); |
| 148 | expect(combined).toEqual([1, 2]); |
| 149 | }); |
| 150 | }); |
| 151 | |
| 152 | test('is_buffer()', function () { |
| 153 | for (const x of [null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g]) { |
| 154 | // t.equal(is_buffer(x), false, inspect(x) + ' is not a buffer'); |
| 155 | expect(is_buffer(x)).toEqual(false); |
| 156 | } |
| 157 | |
| 158 | var fakeBuffer = { constructor: Buffer }; |
| 159 | // t.equal(is_buffer(fakeBuffer), false, 'fake buffer is not a buffer'); |
| 160 | expect(is_buffer(fakeBuffer)).toEqual(false); |
| 161 | |
| 162 | var saferBuffer = Buffer.from('abc'); |
| 163 | // t.equal(is_buffer(saferBuffer), true, 'SaferBuffer instance is a buffer'); |
| 164 | expect(is_buffer(saferBuffer)).toEqual(true); |
| 165 | |
| 166 | var buffer = Buffer.from('abc'); |
| 167 | // t.equal(is_buffer(buffer), true, 'real Buffer instance is a buffer'); |
| 168 | expect(is_buffer(buffer)).toEqual(true); |
| 169 | }); |
| 170 | |