Skip to content

Commit 9f337a8

Browse files
committed
chore: split up test suites for composing
1 parent 5aed5d1 commit 9f337a8

8 files changed

Lines changed: 374 additions & 315 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
},
3131
"scripts": {
3232
"build": "bundt",
33-
"test": "uvu -r esm test"
33+
"test": "uvu test -r esm -i suites"
3434
},
3535
"files": [
3636
"*.d.ts",

test/index.js

Lines changed: 15 additions & 314 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { suite } from 'uvu';
22
import * as assert from 'uvu/assert';
33
import { dset } from '../src';
44

5+
import arrays from './suites/arrays';
6+
import assigns from './suites/assigns';
7+
import pollution from './suites/pollution';
8+
import preserve from './suites/preserve';
9+
import objects from './suites/objects';
10+
import basics from './suites/basics';
11+
12+
// ---
13+
514
const API = suite('API');
615

716
API('should export a function', () => {
@@ -12,317 +21,9 @@ API.run();
1221

1322
// ---
1423

15-
const usage = suite('usage');
16-
17-
usage('should not give return value', () => {
18-
let output = dset({}, 'c', 3); // add c
19-
assert.is(output, undefined);
20-
});
21-
22-
usage('should mutate original object', () => {
23-
let item = { foo: 1 };
24-
dset(item, 'bar', 123);
25-
assert.ok(item === item);
26-
assert.equal(item, {
27-
foo: 1,
28-
bar: 123
29-
});
30-
});
31-
32-
usage.run();
33-
34-
// ---
35-
36-
const keys = suite('keys');
37-
38-
keys('should add value to key path :: shallow :: string', () => {
39-
let input = {};
40-
dset(input, 'abc', 123);
41-
assert.equal(input, { abc: 123 });
42-
});
43-
44-
keys('should add value to key path :: shallow :: array', () => {
45-
let input = {};
46-
dset(input, ['abc'], 123);
47-
assert.equal(input, { abc: 123 });
48-
});
49-
50-
keys('should add value to key path :: nested :: string', () => {
51-
let input = {};
52-
dset(input, 'a.b.c', 123);
53-
assert.equal(input, {
54-
a: {
55-
b: {
56-
c: 123
57-
}
58-
}
59-
});
60-
});
61-
62-
keys('should add value to key path :: nested :: array', () => {
63-
let input = {};
64-
dset(input, ['a', 'b', 'c'], 123);
65-
assert.equal(input, {
66-
a: {
67-
b: {
68-
c: 123
69-
}
70-
}
71-
});
72-
});
73-
74-
keys('should create Array via integer key :: string', () => {
75-
let input = {};
76-
dset(input, ['foo', '0'], 123);
77-
assert.instance(input.foo, Array);
78-
assert.equal(input, {
79-
foo: [123]
80-
})
81-
});
82-
83-
keys('should create Array via integer key :: number', () => {
84-
let input = {};
85-
dset(input, ['foo', 0], 123);
86-
assert.instance(input.foo, Array);
87-
assert.equal(input, {
88-
foo: [123]
89-
})
90-
});
91-
92-
keys.run();
93-
94-
// ---
95-
96-
const arrays = suite('arrays');
97-
98-
arrays('should create array instead of object via numeric key :: simple', () => {
99-
let input = { a: 1 };
100-
dset(input, 'e.0', 2);
101-
assert.instance(input.e, Array);
102-
assert.is(input.e[0], 2);
103-
assert.equal(input, {
104-
a: 1,
105-
e: [2]
106-
});
107-
});
108-
109-
arrays('should create array instead of object via numeric key :: nested', () => {
110-
let input = { a: 1 };
111-
dset(input, 'e.0.0', 123);
112-
assert.instance(input.e, Array);
113-
assert.is(input.e[0][0], 123);
114-
assert.equal(input, {
115-
a: 1,
116-
e: [ [123] ]
117-
});
118-
});
119-
120-
arrays('should be able to create object inside of array', () => {
121-
let input = {};
122-
dset(input, ['x', '0', 'z'], 123);
123-
assert.instance(input.x, Array);
124-
assert.equal(input, {
125-
x: [{ z:123 }]
126-
});
127-
});
128-
129-
arrays('should create arrays with hole(s) if needed', () => {
130-
let input = {};
131-
dset(input, ['x', '1', 'z'], 123);
132-
assert.instance(input.x, Array);
133-
assert.equal(input, {
134-
x: [, { z:123 }]
135-
});
136-
});
137-
138-
arrays('should create object from decimal-like key :: array :: zero :: string', () => {
139-
let input = {};
140-
dset(input, ['x', '10.0', 'z'], 123);
141-
assert.not.instance(input.x, Array);
142-
assert.equal(input, {
143-
x: {
144-
'10.0': {
145-
z: 123
146-
}
147-
}
148-
});
149-
});
150-
151-
arrays('should create array from decimal-like key :: array :: zero :: number', () => {
152-
let input = {};
153-
dset(input, ['x', 10.0, 'z'], 123);
154-
assert.instance(input.x, Array);
155-
156-
let x = Array(10);
157-
x.push({ z: 123 });
158-
assert.equal(input, { x });
159-
});
160-
161-
arrays('should create object from decimal-like key :: array :: nonzero', () => {
162-
let input = {};
163-
dset(input, ['x', '10.2', 'z'], 123);
164-
assert.not.instance(input.x, Array);
165-
assert.equal(input, {
166-
x: {
167-
'10.2': {
168-
z: 123
169-
}
170-
}
171-
});
172-
});
173-
174-
arrays.run();
175-
176-
// ---
177-
178-
const preserves = suite('preserves');
179-
180-
preserves('should preserve existing object structure', () => {
181-
let input = {
182-
a: {
183-
b: {
184-
c: 123
185-
}
186-
}
187-
};
188-
189-
dset(input, 'a.b.x.y', 456);
190-
191-
assert.equal(input, {
192-
a: {
193-
b: {
194-
c: 123,
195-
x: {
196-
y:456
197-
}
198-
}
199-
}
200-
});
201-
});
202-
203-
preserves('should overwrite existing non-object values as object', () => {
204-
let input = {
205-
a: {
206-
b: 123
207-
}
208-
};
209-
210-
dset(input, 'a.b.c', 'hello');
211-
212-
assert.equal(input, {
213-
a: {
214-
b: {
215-
c: 'hello'
216-
}
217-
}
218-
});
219-
});
220-
221-
preserves('should preserve existing object tree w/ array value', () => {
222-
let input = {
223-
a: {
224-
b: {
225-
c: 123,
226-
d: {
227-
e: 5
228-
}
229-
}
230-
}
231-
};
232-
233-
dset(input, 'a.b.d.z', [1,2,3,4]);
234-
235-
assert.equal(input.a.b.d, {
236-
e: 5,
237-
z: [1,2,3,4]
238-
});
239-
});
240-
241-
preserves.run();
242-
243-
// ---
244-
245-
const pollution = suite('pollution');
246-
247-
pollution('should protect against "__proto__" assignment', () => {
248-
let input = { abc: 123 };
249-
let before = input.__proto__;
250-
dset(input, '__proto__.hello', 123);
251-
252-
assert.equal(input.__proto__, before);
253-
assert.equal(input, {
254-
abc: 123
255-
});
256-
257-
assert.is.not({}.hello, 123);
258-
assert.is.not((new Object).hello, 123);
259-
assert.is.not(Object.create(null).hello, 123);
260-
});
261-
262-
pollution('should protect against "__proto__" assignment :: nested', () => {
263-
let input = { abc: 123 };
264-
let before = input.__proto__;
265-
dset(input, ['xyz', '__proto__', 'hello'], 123);
266-
267-
assert.equal(input.__proto__, before);
268-
assert.equal(input, {
269-
abc: 123,
270-
xyz: {
271-
// empty
272-
}
273-
});
274-
275-
assert.is({}.hello, undefined);
276-
assert.is(input.hello, undefined);
277-
assert.is((new Object).hello, undefined);
278-
assert.is(Object.create(null).hello, undefined);
279-
});
280-
281-
pollution('should ignore "prototype" assignment', () => {
282-
let input = { a: 123 };
283-
dset(input, 'a.prototype.hello', 'world');
284-
285-
assert.is(input.a.prototype, undefined);
286-
assert.is(input.a.hello, undefined);
287-
288-
assert.equal(input, {
289-
a: {
290-
// converted, then aborted
291-
}
292-
});
293-
294-
assert.is(
295-
JSON.stringify(input),
296-
'{"a":{}}'
297-
);
298-
});
299-
300-
pollution('should ignore "constructor" assignment :: direct', () => {
301-
let input = { a: 123 };
302-
303-
function Custom() {
304-
//
305-
}
306-
307-
dset(input, 'a.constructor', Custom);
308-
assert.is.not(input.a.constructor, Custom);
309-
assert.not.instance(input.a, Custom);
310-
311-
assert.instance(input.a.constructor, Object, '~> 123 -> {}');
312-
assert.is(input.a.hasOwnProperty('constructor'), false);
313-
assert.equal(input, { a: {} });
314-
});
315-
316-
pollution('should ignore "constructor" assignment :: nested', () => {
317-
let input = {};
318-
319-
dset(input, 'constructor.prototype.hello', 'world');
320-
assert.is(input.hasOwnProperty('constructor'), false);
321-
assert.is(input.hasOwnProperty('hello'), false);
322-
323-
assert.equal(input, {
324-
// empty
325-
});
326-
});
327-
328-
pollution.run();
24+
basics(dset);
25+
assigns(dset);
26+
preserve(dset);
27+
pollution(dset);
28+
objects(dset);
29+
arrays(dset);

0 commit comments

Comments
 (0)