-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
57 lines (45 loc) · 1.73 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* global it */
const assert = require('assert');
const flatOptions = process.env.TEST_LIB ? require('./lib') : require('./index');
it('should be a function', function () {
assert.equal(typeof flatOptions, 'function');
});
it('should return options without defaults', function () {
const options = flatOptions({a: 1, b: 2});
assert.deepEqual(options, {a: 1, b: 2});
});
it('should merge default values', function () {
const options = flatOptions({a: 1}, {a: 2, c: 3});
assert.deepEqual(options, {a: 1, c: 3});
});
it('should merge default values (with null proto)', function () {
const defaults = Object.assign(Object.create(null), {a: 2, c: 3});
const options = flatOptions({a: 1}, defaults);
assert.deepEqual(options, {a: 1, c: 3});
});
it('should use default value for undefined', function () {
const options = flatOptions({a: undefined}, {a: 2});
assert.deepEqual(options, {a: 2});
});
it('should keep value for null/false/""', function () {
const options = flatOptions({a: null, b: false, c: ''}, {a: 1, b: 2, c: 3});
assert.deepEqual(options, {a: null, b: false, c: ''});
});
it('should keep value for NaN', function () {
const options = flatOptions({a: NaN}, {a: 1});
assert.ok(Number.isNaN(options.a));
});
it('should not change defaults', function () {
const defaults = {a: 1, b: undefined};
flatOptions({a: 2}, defaults);
assert.deepEqual(defaults, {a: 1, b: undefined});
});
it('should not change original options', function () {
const options = {a: 1, b: undefined};
flatOptions(options, {a: 2, b: 1});
assert.deepEqual(options, {a: 1, b: undefined});
});
it('should throw for unknown option', function () {
const fn = () => flatOptions({a: 1, b: 2}, {a: 1});
assert.throws(fn, /Unknown option: b/);
});