forked from qunitjs/qunit
-
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.
Docs: Write
assert.propContain()
and notPropContains()
pages
Also cross-link with propEqual pages, and improve those as well, in particular to clarify that these too are recursive in regards to nested objects being comparable using plain objects. Ref qunitjs#1668.
- Loading branch information
Showing
4 changed files
with
176 additions
and
13 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,79 @@ | ||
--- | ||
layout: page-api | ||
title: assert.notPropContains() | ||
excerpt: Check that an object does not contain certain properties. | ||
groups: | ||
- assert | ||
version_added: "unreleased" | ||
--- | ||
|
||
`notPropContains( actual, expected, message = "" )` | ||
|
||
Check that an object does not contain certain properties. | ||
|
||
| name | description | | ||
|------|-------------| | ||
| `actual` | Expression being tested | | ||
| `expected` | Known comparison value | | ||
| `message` (string) | Short description of the actual value | | ||
|
||
|
||
The `notPropContains` assertion compares the subset of properties in the expected object, and tests that these keys are either absent or hold a value that is different according to a strict equality comparison. | ||
|
||
This method is recursive and allows partial comparison of nested objects as well. | ||
|
||
## See also | ||
|
||
* Use [`assert.propContain()`](./propContain.md) to test for the presence and equality of properties instead. | ||
|
||
## Examples | ||
|
||
```js | ||
QUnit.test( "example", assert => { | ||
|
||
const result = { | ||
foo: 0, | ||
vehicle: { | ||
timeCircuits: "on", | ||
fluxCapacitor: "fluxing", | ||
engine: "running" | ||
}, | ||
quux: 1 | ||
}; | ||
|
||
// succeeds, property "timeCircuits" is actually "on" | ||
assert.notPropContains( result, { | ||
vehicle: { | ||
timeCircuits: "off" | ||
} | ||
} ); | ||
|
||
// succeeds, property "wings" is not in the object | ||
assert.notPropContains( result, { | ||
vehicle: { | ||
wings: "flapping" | ||
} | ||
} ); | ||
|
||
function Point( x, y ) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
assert.notPropContains( | ||
new Point( 10, 20 ), | ||
{ z: 30 } | ||
); | ||
|
||
const nested = { | ||
north: [ /* ... */ ], | ||
east: new Point( 10, 20 ), | ||
south: [ /* ... */ ], | ||
west: [ /* ... */ ] | ||
}; | ||
|
||
assert.notPropContains( nested, { east: new Point( 88, 42 ) } ); | ||
assert.notPropContains( nested, { east: { x: 88 } } ); | ||
|
||
} ); | ||
``` |
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,78 @@ | ||
--- | ||
layout: page-api | ||
title: assert.propContain() | ||
excerpt: Check that an object contains certain properties. | ||
groups: | ||
- assert | ||
version_added: "unreleased" | ||
--- | ||
|
||
`propContain( actual, expected, message = "" )` | ||
|
||
Check that an object contains certain properties. | ||
|
||
| name | description | | ||
|------|-------------| | ||
| `actual` | Expression being tested | | ||
| `expected` | Known comparison value | | ||
| `message` (string) | Short description of the actual value | | ||
|
||
|
||
The `propContain` assertion compares only the **subset** of properties in the expected object, | ||
and tests that these keys exist as own properties with strictly equal values. | ||
|
||
This method is recursive and allows partial comparison of nested objects as well. | ||
|
||
## See also | ||
|
||
* Use [`assert.propEqual()`](./propEqual.md) to compare all properties, considering extra properties as unexpected. | ||
* Use [`assert.notPropContain()`](./notPropContain.md) to test for the absence or inequality of certain properties. | ||
|
||
## Examples | ||
|
||
```js | ||
QUnit.test( "example", assert => { | ||
|
||
const result = { | ||
foo: 0, | ||
vehicle: { | ||
timeCircuits: "on", | ||
fluxCapacitor: "fluxing", | ||
engine: "running" | ||
}, | ||
quux: 1 | ||
}; | ||
|
||
assert.propContains( result, { | ||
foo: 0, | ||
vehicle: { fluxCapacitor: "fluxing" } | ||
} ); | ||
|
||
function Point( x, y ) { | ||
this.x = x; | ||
this.y = y; | ||
} | ||
|
||
assert.propContains( | ||
new Point( 10, 20 ), | ||
{ y: 20 } | ||
); | ||
|
||
assert.propContains( | ||
[ "a", "b" ], | ||
{ 1: "b" } | ||
); | ||
|
||
const nested = { | ||
north: [ /* ... */ ], | ||
east: new Point( 10, 20 ), | ||
south: [ /* ... */ ], | ||
west: [ /* ... */ ] | ||
}; | ||
|
||
assert.propContains( nested, { east: new Point( 10, 20 ) } ); | ||
assert.propContains( nested, { east: { x: 10, y: 20 } } ); | ||
assert.propContains( nested, { east: { x: 10 } } ); | ||
|
||
} ); | ||
``` |
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