source: trip-planner-front/node_modules/qs/test/utils.js@ 6fe77af

Last change on this file since 6fe77af was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.0 KB
Line 
1'use strict';
2
3var test = require('tape');
4var inspect = require('object-inspect');
5var SaferBuffer = require('safer-buffer').Buffer;
6var forEach = require('for-each');
7var utils = require('../lib/utils');
8
9test('merge()', function (t) {
10 t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null');
11
12 t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array');
13
14 t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key');
15
16 var oneMerged = utils.merge({ foo: 'bar' }, { foo: { first: '123' } });
17 t.deepEqual(oneMerged, { foo: ['bar', { first: '123' }] }, 'merges a standalone and an object into an array');
18
19 var twoMerged = utils.merge({ foo: ['bar', { first: '123' }] }, { foo: { second: '456' } });
20 t.deepEqual(twoMerged, { foo: { 0: 'bar', 1: { first: '123' }, second: '456' } }, 'merges a standalone and two objects into an array');
21
22 var sandwiched = utils.merge({ foo: ['bar', { first: '123', second: '456' }] }, { foo: 'baz' });
23 t.deepEqual(sandwiched, { foo: ['bar', { first: '123', second: '456' }, 'baz'] }, 'merges an object sandwiched by two standalones into an array');
24
25 var nestedArrays = utils.merge({ foo: ['baz'] }, { foo: ['bar', 'xyzzy'] });
26 t.deepEqual(nestedArrays, { foo: ['baz', 'bar', 'xyzzy'] });
27
28 var noOptionsNonObjectSource = utils.merge({ foo: 'baz' }, 'bar');
29 t.deepEqual(noOptionsNonObjectSource, { foo: 'baz', bar: true });
30
31 t.test(
32 'avoids invoking array setters unnecessarily',
33 { skip: typeof Object.defineProperty !== 'function' },
34 function (st) {
35 var setCount = 0;
36 var getCount = 0;
37 var observed = [];
38 Object.defineProperty(observed, 0, {
39 get: function () {
40 getCount += 1;
41 return { bar: 'baz' };
42 },
43 set: function () { setCount += 1; }
44 });
45 utils.merge(observed, [null]);
46 st.equal(setCount, 0);
47 st.equal(getCount, 1);
48 observed[0] = observed[0]; // eslint-disable-line no-self-assign
49 st.equal(setCount, 1);
50 st.equal(getCount, 2);
51 st.end();
52 }
53 );
54
55 t.end();
56});
57
58test('assign()', function (t) {
59 var target = { a: 1, b: 2 };
60 var source = { b: 3, c: 4 };
61 var result = utils.assign(target, source);
62
63 t.equal(result, target, 'returns the target');
64 t.deepEqual(target, { a: 1, b: 3, c: 4 }, 'target and source are merged');
65 t.deepEqual(source, { b: 3, c: 4 }, 'source is untouched');
66
67 t.end();
68});
69
70test('combine()', function (t) {
71 t.test('both arrays', function (st) {
72 var a = [1];
73 var b = [2];
74 var combined = utils.combine(a, b);
75
76 st.deepEqual(a, [1], 'a is not mutated');
77 st.deepEqual(b, [2], 'b is not mutated');
78 st.notEqual(a, combined, 'a !== combined');
79 st.notEqual(b, combined, 'b !== combined');
80 st.deepEqual(combined, [1, 2], 'combined is a + b');
81
82 st.end();
83 });
84
85 t.test('one array, one non-array', function (st) {
86 var aN = 1;
87 var a = [aN];
88 var bN = 2;
89 var b = [bN];
90
91 var combinedAnB = utils.combine(aN, b);
92 st.deepEqual(b, [bN], 'b is not mutated');
93 st.notEqual(aN, combinedAnB, 'aN + b !== aN');
94 st.notEqual(a, combinedAnB, 'aN + b !== a');
95 st.notEqual(bN, combinedAnB, 'aN + b !== bN');
96 st.notEqual(b, combinedAnB, 'aN + b !== b');
97 st.deepEqual([1, 2], combinedAnB, 'first argument is array-wrapped when not an array');
98
99 var combinedABn = utils.combine(a, bN);
100 st.deepEqual(a, [aN], 'a is not mutated');
101 st.notEqual(aN, combinedABn, 'a + bN !== aN');
102 st.notEqual(a, combinedABn, 'a + bN !== a');
103 st.notEqual(bN, combinedABn, 'a + bN !== bN');
104 st.notEqual(b, combinedABn, 'a + bN !== b');
105 st.deepEqual([1, 2], combinedABn, 'second argument is array-wrapped when not an array');
106
107 st.end();
108 });
109
110 t.test('neither is an array', function (st) {
111 var combined = utils.combine(1, 2);
112 st.notEqual(1, combined, '1 + 2 !== 1');
113 st.notEqual(2, combined, '1 + 2 !== 2');
114 st.deepEqual([1, 2], combined, 'both arguments are array-wrapped when not an array');
115
116 st.end();
117 });
118
119 t.end();
120});
121
122test('isBuffer()', function (t) {
123 forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) {
124 t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer');
125 });
126
127 var fakeBuffer = { constructor: Buffer };
128 t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer');
129
130 var saferBuffer = SaferBuffer.from('abc');
131 t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer');
132
133 var buffer = Buffer.from ? Buffer.from('abc') : new Buffer('abc');
134 t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer');
135 t.end();
136});
Note: See TracBrowser for help on using the repository browser.