-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58f12e0
commit fc5fe74
Showing
1 changed file
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,154 @@ | ||
// | ||
import assert from 'node:assert'; | ||
import test from 'node:test'; | ||
import { | ||
reduceConfigs, | ||
reduceConfigsMergeContext, | ||
reduceConfigsWithContext, | ||
} from '../dist/index.js'; | ||
|
||
test('reduceConfigs should return initial config', () => { | ||
assert.deepStrictEqual(reduceConfigs({ initial: { value: 'a' } }), { | ||
value: 'a', | ||
}); | ||
}); | ||
|
||
test('reduceConfigs should merge initial config', () => { | ||
assert.deepStrictEqual( | ||
reduceConfigs({ | ||
initial: { name: 'a' }, | ||
config: { | ||
name: 'b', | ||
custom: 'c', | ||
}, | ||
}), | ||
{ | ||
name: 'b', | ||
custom: 'c', | ||
}, | ||
); | ||
}); | ||
|
||
test('reduceConfigs should support custom merge function', () => { | ||
const merge = (target, source) => { | ||
for (const key in source) { | ||
if (Object.hasOwn(target, key)) { | ||
target[key] += source[key]; | ||
} else { | ||
target[key] = source[key]; | ||
} | ||
} | ||
return target; | ||
}; | ||
|
||
assert.deepStrictEqual( | ||
reduceConfigs({ | ||
initial: { | ||
a: 1, | ||
b: 'b', | ||
}, | ||
config: { | ||
a: 2, | ||
b: 'b', | ||
c: 'c', | ||
}, | ||
mergeFn: merge, | ||
}), | ||
{ | ||
a: 3, | ||
b: 'bb', | ||
c: 'c', | ||
}, | ||
); | ||
}); | ||
|
||
test('reduceConfigs should support function or object array', () => { | ||
const initial = { a: 'a' }; | ||
|
||
const config = [ | ||
{ b: 'b' }, | ||
(o, { add }) => { | ||
o.c = add(1, 2); | ||
}, | ||
(o) => ({ | ||
...o, | ||
d: 'd', | ||
}), | ||
{ e: 'e' }, | ||
]; | ||
|
||
assert.deepStrictEqual( | ||
reduceConfigsWithContext({ | ||
initial, | ||
config, | ||
ctx: { | ||
add: (a, b) => a + b, | ||
}, | ||
}), | ||
{ | ||
a: 'a', | ||
b: 'b', | ||
c: 3, | ||
d: 'd', | ||
e: 'e', | ||
}, | ||
); | ||
}); | ||
|
||
test('reduceConfigs should support function and merge context', () => { | ||
const initial = { a: 'a' }; | ||
|
||
const config = [ | ||
{ b: 'b' }, | ||
({ value, add }) => { | ||
value.c = add(1, 2); | ||
}, | ||
({ value }) => ({ | ||
...value, | ||
d: 'd', | ||
}), | ||
{ e: 'e' }, | ||
]; | ||
|
||
assert.deepStrictEqual( | ||
reduceConfigsMergeContext({ | ||
initial, | ||
config, | ||
ctx: { | ||
add: (a, b) => a + b, | ||
}, | ||
}), | ||
{ | ||
a: 'a', | ||
b: 'b', | ||
c: 3, | ||
d: 'd', | ||
e: 'e', | ||
}, | ||
); | ||
}); | ||
|
||
test('reduceConfigs should allow false as config', () => { | ||
assert.strictEqual( | ||
reduceConfigs({ | ||
initial: 'head', | ||
config: false, | ||
}), | ||
false, | ||
); | ||
|
||
assert.strictEqual( | ||
reduceConfigs({ | ||
initial: 'head', | ||
config: () => false, | ||
}), | ||
false, | ||
); | ||
|
||
assert.strictEqual( | ||
reduceConfigs({ | ||
initial: 'head', | ||
config: ['head', 'head', () => false], | ||
}), | ||
false, | ||
); | ||
}); |