-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: create unit tests for defined
- Loading branch information
1 parent
6c8d41d
commit 2113e67
Showing
3 changed files
with
74 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The defined() function returns the first non-undefined value from a list of arguments, evaluating functions if needed. | ||
If only undefined values, return undefined. | ||
|
||
```js | ||
import defined from '@xen-orchestra/defined/index.js' | ||
|
||
defined(undefined, 'foo', 42) | ||
// Returns 'foo' | ||
|
||
defined([undefined, null, 10]) | ||
// Returns [undefined, null, 10] | ||
|
||
defined([undefined, undefined], [undefined, undefined, 10]) | ||
// Returns [undefined, undefined] | ||
|
||
defined(() => 'bar', 42) | ||
// Returns 'bar' | ||
|
||
defined(undefined, undefined) | ||
// Returns undefined | ||
``` |
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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import defined from '@xen-orchestra/defined/index.js' | ||
|
||
describe('Utilities to help handling (possibly) undefined values', () => { | ||
it('should return first non undefined value in args', () => { | ||
const expected = 'foo' | ||
assert.deepStrictEqual(defined(undefined, 'foo', 42), expected) | ||
}) | ||
it('should return first non undefined value in array', () => { | ||
const expected = [undefined, null, 10] | ||
assert.deepStrictEqual(defined([undefined, null, 10]), expected) | ||
}) | ||
it('should return first non undefined value in arrays', () => { | ||
const expected = [undefined, undefined, undefined] | ||
assert.deepStrictEqual(defined([undefined, undefined, undefined], [undefined, undefined, 10]), expected) | ||
}) | ||
it('should return first non undefined value in function', () => { | ||
const expected = 'bar' | ||
assert.deepStrictEqual( | ||
defined(() => 'bar', 42), | ||
expected | ||
) | ||
}) | ||
it('should return undefined if only undefined values', () => { | ||
const expected = undefined | ||
assert.equal(defined(undefined, undefined), expected) | ||
}) | ||
}) |