-
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.
test(defined): create tests for defined() (#7967)
Co-authored-by: Stephane MEYER <[email protected]> Co-authored-by: Julien Fontanet <[email protected]>
- Loading branch information
1 parent
28709b6
commit b365e4a
Showing
6 changed files
with
94 additions
and
2 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,29 @@ | ||
### `defined()` | ||
|
||
Returns the first non-`undefined` value from a list of arguments, evaluating functions if needed. | ||
|
||
There is two ways to use this function: | ||
|
||
- either with a single array argument: it should return the first non-undefined item | ||
- or with multiple arguments: it should return the first non-undefined argument | ||
|
||
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 null | ||
|
||
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
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,30 @@ | ||
import { describe, it } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
import defined_ from '@xen-orchestra/defined/index.js' | ||
|
||
// for each test, use the flat syntax and the array syntax | ||
for (const [title, defined] of Object.entries({ | ||
'defined(...values)': defined_, | ||
'defined([ ...values ])': (...args) => defined_(args), | ||
})) { | ||
describe(title, () => { | ||
it('returns the first non undefined value', () => { | ||
assert.deepEqual(defined(undefined, 'foo', 42), 'foo') | ||
}) | ||
|
||
it('returns undefined if only undefined values', () => { | ||
assert.equal(defined(undefined, undefined), undefined) | ||
}) | ||
|
||
it('resolves functions with `get(fn)`', () => { | ||
const o = { foo: undefined, baz: 'baz' } | ||
assert.deepEqual( | ||
defined( | ||
() => o.foo.bar, | ||
() => o.baz | ||
), | ||
'baz' | ||
) | ||
}) | ||
}) | ||
} |
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