Skip to content

Commit

Permalink
api(toHaveGeometryCount): improve error messages and description for …
Browse files Browse the repository at this point in the history
…nested GeometryCollections behavior
  • Loading branch information
M-Scott-Lassiter committed Jun 11, 2022
1 parent c71c21a commit 01fe745
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 24 deletions.
18 changes: 14 additions & 4 deletions docs/Matchers.Geometries.html
Original file line number Diff line number Diff line change
Expand Up @@ -1513,7 +1513,8 @@ <h5>Parameters:</h5>
<h4 class="name" id=".toHaveGeometryCount">
<span class="type-signature">(static) </span>toHaveGeometryCount<span
class="signature"
>(geometryObject, Range1, Range2)</span
>(geometryObject, Range1<span class="signature-attributes">opt</span>,
Range2<span class="signature-attributes">opt</span>)</span
><span class="type-signature"></span>
</h4>

Expand All @@ -1525,8 +1526,8 @@ <h4 class="name" id=".toHaveGeometryCount">
<a href="matchers_geometries_toHaveGeometryCount.js.html"
>matchers/geometries/toHaveGeometryCount.js</a
>,
<a href="matchers_geometries_toHaveGeometryCount.js.html#line78"
>line 78</a
<a href="matchers_geometries_toHaveGeometryCount.js.html#line80"
>line 80</a
>
</li>
</ul>
Expand All @@ -1553,7 +1554,8 @@ <h4 class="name" id=".toHaveGeometryCount">
Range1 and Range2 are specified, it checks that the geometry count is
between those values. Decimals get truncated on both Range1 and Range2. Will
fail if Range1 or Range2 less than 0, Range2 less than Range1, or Range2 is
defined and Range1 is not.
defined and Range1 is not. Nested GeometryCollections are only counted as a
single geometry object.
</div>

<h5>Examples</h5>
Expand Down Expand Up @@ -1625,6 +1627,8 @@ <h5>Parameters:</h5>

<th>Type</th>

<th>Attributes</th>

<th class="last">Description</th>
</tr>
</thead>
Expand All @@ -1637,6 +1641,8 @@ <h5>Parameters:</h5>
<span class="param-type">object</span>
</td>

<td class="attributes"></td>

<td class="description last">
A GeoJSON GeometryCollection object
</td>
Expand All @@ -1649,6 +1655,8 @@ <h5>Parameters:</h5>
<span class="param-type">number</span>
</td>

<td class="attributes">&lt;optional><br /></td>

<td class="description last">
Minimum geometry object count, or exact count if omitting Range2
</td>
Expand All @@ -1661,6 +1669,8 @@ <h5>Parameters:</h5>
<span class="param-type">number</span>
</td>

<td class="attributes">&lt;optional><br /></td>

<td class="description last">Maximum geometry object count</td>
</tr>
</tbody>
Expand Down
18 changes: 11 additions & 7 deletions docs/matchers_geometries_toHaveGeometryCount.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,13 @@ <h1 class="page-title">matchers/geometries/toHaveGeometryCount.js</h1>
*
* Will fail if Range1 or Range2 less than 0, Range2 less than Range1, or Range2 is defined and Range1 is not.
*
* Nested GeometryCollections are only counted as a single geometry object.
*
* @memberof Matchers.Geometries
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/48
* @param {object} geometryObject A GeoJSON GeometryCollection object
* @param {number} Range1 Minimum geometry object count, or exact count if omitting Range2
* @param {number} Range2 Maximum geometry object count
* @param {number} [Range1] Minimum geometry object count, or exact count if omitting Range2
* @param {number} [Range2] Maximum geometry object count
* @example
* const testCollection = {
* "type": "GeometryCollection",
Expand Down Expand Up @@ -429,14 +431,16 @@ <h1 class="page-title">matchers/geometries/toHaveGeometryCount.js</h1>
if (Range1 &lt; 0) {
return {
pass: false,
message: () => failMessage(`Range1 (${printExpected(Range1)}) must be greater than 0.`)
message: () =>
failMessage(`Range1 must be greater than 0. Provided: ${printExpected(Range1)}`)
}
}

if (Range2 &lt; 0) {
return {
pass: false,
message: () => failMessage(`Range2 (${printExpected(Range2)}) must be greater than 0.`)
message: () =>
failMessage(`Range2 must be greater than 0. Provided: ${printExpected(Range2)}`)
}
}

Expand All @@ -447,7 +451,7 @@ <h1 class="page-title">matchers/geometries/toHaveGeometryCount.js</h1>
failMessage(
`Range2 (${printExpected(
Range2
)}) must be greater than or equal to Range1 (${printExpected(Range1)})`
)}) must be greater than or equal to Range1 (${printExpected(Range1)}).`
)
}
}
Expand Down Expand Up @@ -487,7 +491,7 @@ <h1 class="page-title">matchers/geometries/toHaveGeometryCount.js</h1>
}
return {
pass: false,
message: () => failMessage('Expected no objects in the "geometries" property')
message: () => failMessage('Expected no objects in the "geometries" property.')
}
} else {
// Check for an exact value
Expand All @@ -500,7 +504,7 @@ <h1 class="page-title">matchers/geometries/toHaveGeometryCount.js</h1>
// eslint-disable-next-line prefer-template, no-unused-expressions
matcherHint('.toHaveGeometryCount', 'GeometryObject', 'Range1') +
'\n\n' +
'Geometries2 has count of ' +
'Geometries has count of ' +
printReceived(geometryObject.geometries.length) +
', expected ' +
printExpected(Range1) +
Expand Down
4 changes: 3 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ declare global {
* Decimals get truncated on both Range1 and Range2.
*
* Will fail if Range1 or Range2 less than 0, Range2 less than Range1, or Range2 is defined and Range1 is not.
*
* Nested GeometryCollections are only counted as a single geometry object.
*/
toHaveGeometryCount<E = Object>(Range1: Number, Range2: Number): R

Expand All @@ -224,7 +226,7 @@ declare global {
*
* Nested GeometryCollections are only counted as a single geometry object.
*/
toHaveGeometryCount<E = Object>(MinCount: Number): R
toHaveMinGeometryCount<E = Object>(MinCount: Number): R
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions src/matchers/geometries/toHaveGeometryCount.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ const { geometryCollection } = require('../../core/geometries/geometryCollection
*
* Will fail if Range1 or Range2 less than 0, Range2 less than Range1, or Range2 is defined and Range1 is not.
*
* Nested GeometryCollections are only counted as a single geometry object.
*
* @memberof Matchers.Geometries
* @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/48
* @param {object} geometryObject A GeoJSON GeometryCollection object
* @param {number} Range1 Minimum geometry object count, or exact count if omitting Range2
* @param {number} Range2 Maximum geometry object count
* @param {number} [Range1] Minimum geometry object count, or exact count if omitting Range2
* @param {number} [Range2] Maximum geometry object count
* @example
* const testCollection = {
* "type": "GeometryCollection",
Expand Down Expand Up @@ -112,14 +114,16 @@ function toHaveGeometryCount(geometryObject, Range1, Range2) {
if (Range1 < 0) {
return {
pass: false,
message: () => failMessage(`Range1 (${printExpected(Range1)}) must be greater than 0.`)
message: () =>
failMessage(`Range1 must be greater than 0. Provided: ${printExpected(Range1)}`)
}
}

if (Range2 < 0) {
return {
pass: false,
message: () => failMessage(`Range2 (${printExpected(Range2)}) must be greater than 0.`)
message: () =>
failMessage(`Range2 must be greater than 0. Provided: ${printExpected(Range2)}`)
}
}

Expand All @@ -130,7 +134,7 @@ function toHaveGeometryCount(geometryObject, Range1, Range2) {
failMessage(
`Range2 (${printExpected(
Range2
)}) must be greater than or equal to Range1 (${printExpected(Range1)})`
)}) must be greater than or equal to Range1 (${printExpected(Range1)}).`
)
}
}
Expand Down Expand Up @@ -170,7 +174,7 @@ function toHaveGeometryCount(geometryObject, Range1, Range2) {
}
return {
pass: false,
message: () => failMessage('Expected no objects in the "geometries" property')
message: () => failMessage('Expected no objects in the "geometries" property.')
}
} else {
// Check for an exact value
Expand All @@ -183,7 +187,7 @@ function toHaveGeometryCount(geometryObject, Range1, Range2) {
// eslint-disable-next-line prefer-template, no-unused-expressions
matcherHint('.toHaveGeometryCount', 'GeometryObject', 'Range1') +
'\n\n' +
'Geometries2 has count of ' +
'Geometries has count of ' +
printReceived(geometryObject.geometries.length) +
', expected ' +
printExpected(Range1) +
Expand Down
10 changes: 5 additions & 5 deletions tests/geometries/__snapshots__/toHaveGeometryCount.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Received: false"
exports[`Error Snapshot Testing. Throws error: No objects in geometries 1`] = `
"expect(GeometryObject).toHaveGeometryCount(Range1, Range2)
Expected no objects in the \\"geometries\\" property
Expected no objects in the \\"geometries\\" property.
Received: {\\"geometries\\": [], \\"type\\": \\"GeometryCollection\\"}"
`;
Expand All @@ -35,7 +35,7 @@ Received: {\\"geometries\\": [{\\"coordinates\\": [100, 0], \\"type\\": \\
exports[`Error Snapshot Testing. Throws error: Range1 is negative 1`] = `
"expect(GeometryObject).toHaveGeometryCount(Range1, Range2)
Range1 ([32m-1[39m) must be greater than 0.
Range1 must be greater than 0. Provided: [32m-1[39m
Received: {\\"geometries\\": [{\\"coordinates\\": [100, 0], \\"type\\": \\"Point\\"}, {\\"coordinates\\": [[101, 0], [102, 1]], \\"type\\": \\"LineString\\"}, {\\"coordinates\\": [[[102, 2], [103, 2], [103, 3], [102, 3], [102, 2]]], \\"type\\": \\"Polygon\\"}, {\\"coordinates\\": [150, 73], \\"type\\": \\"Point\\"}], \\"type\\": \\"GeometryCollection\\"}"
`;
Expand All @@ -51,7 +51,7 @@ Received: {\\"geometries\\": [{\\"coordinates\\": [100, 0], \\"type\\": \\
exports[`Error Snapshot Testing. Throws error: Range2 is negative 1`] = `
"expect(GeometryObject).toHaveGeometryCount(Range1, Range2)
Range2 ([32m-1[39m) must be greater than 0.
Range2 must be greater than 0. Provided: [32m-1[39m
Received: {\\"geometries\\": [{\\"coordinates\\": [100, 0], \\"type\\": \\"Point\\"}, {\\"coordinates\\": [[101, 0], [102, 1]], \\"type\\": \\"LineString\\"}, {\\"coordinates\\": [[[102, 2], [103, 2], [103, 3], [102, 3], [102, 2]]], \\"type\\": \\"Polygon\\"}, {\\"coordinates\\": [150, 73], \\"type\\": \\"Point\\"}], \\"type\\": \\"GeometryCollection\\"}"
`;
Expand All @@ -67,7 +67,7 @@ Received: {\\"geometries\\": [{\\"coordinates\\": [100, 0], \\"type\\": \\
exports[`Error Snapshot Testing. Throws error: Searching for wrong number of geometries 1`] = `
"expect(GeometryObject).toHaveGeometryCount(Range1)
Geometries2 has count of [31m4[39m, expected [32m3[39m.
Geometries has count of [31m4[39m, expected [32m3[39m.
Received: {\\"geometries\\": [{\\"coordinates\\": [100, 0], \\"type\\": \\"Point\\"}, {\\"coordinates\\": [[101, 0], [102, 1]], \\"type\\": \\"LineString\\"}, {\\"coordinates\\": [[[102, 2], [103, 2], [103, 3], [102, 3], [102, 2]]], \\"type\\": \\"Polygon\\"}, {\\"coordinates\\": [150, 73], \\"type\\": \\"Point\\"}], \\"type\\": \\"GeometryCollection\\"}"
`;
Expand All @@ -83,7 +83,7 @@ Received: {\\"geometries\\": [{\\"coordinates\\": [100, 0], \\"type\\": \\
exports[`Error Snapshot Testing. Throws error: Valid Range2 less than valid Range1 1`] = `
"expect(GeometryObject).toHaveGeometryCount(Range1, Range2)
Range2 ([32m1[39m) must be greater than or equal to Range1 ([32m2[39m)
Range2 ([32m1[39m) must be greater than or equal to Range1 ([32m2[39m).
Received: {\\"geometries\\": [{\\"coordinates\\": [100, 0], \\"type\\": \\"Point\\"}, {\\"coordinates\\": [[101, 0], [102, 1]], \\"type\\": \\"LineString\\"}, {\\"coordinates\\": [[[102, 2], [103, 2], [103, 3], [102, 3], [102, 2]]], \\"type\\": \\"Polygon\\"}, {\\"coordinates\\": [150, 73], \\"type\\": \\"Point\\"}], \\"type\\": \\"GeometryCollection\\"}"
`;
Expand Down

0 comments on commit 01fe745

Please sign in to comment.