Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toHaveNumericID #38

Closed
M-Scott-Lassiter opened this issue Jun 7, 2022 · 1 comment
Closed

toHaveNumericID #38

M-Scott-Lassiter opened this issue Jun 7, 2022 · 1 comment

Comments

@M-Scott-Lassiter
Copy link
Owner

M-Scott-Lassiter commented Jun 7, 2022

Description

GeoJSON Features have an optional ID member that can be any string or number. This matcher only validates IDs with type number. To check for either string only, use toHaveStringID. To check for either string or number, use toHaveID.

If a Feature has a commonly used identifier, that identifier SHOULD be included as a member of the Feature object with the name "id", and the value of this member is either a JSON string or number.

~ https://datatracker.ietf.org/doc/html/rfc7946#section-3.2

An "id" member on a non-feature object gets treated as a foreign member instead of an ID.

This matcher will take an optional argument of either a number, RegExp, or array of any combination of these. If no argument is provided, the matcher will check if the Feature object has an ID member of any value with type number. Otherwise, it will check that the ID member exactly matches either the single input or any of the array values.

Passing a string type to SearchID will not pass the test, even if the ID exactly matches.

Valid GeoJSON Examples

{
    "type": "Feature",
    "id": 123,
    "geometry": {...},
    "properties": {...}
}

Example Matcher Usage

const testFeature = {
    type: 'Feature',
    id: 456,
    geometry: {...},
    properties: {...}
}

const testFeatureNoID = {
    type: 'Feature',
    geometry: {...},
    properties: {...}
}

const testFeatureStringID = {
    type: 'Feature',
    id: 'f1',
    geometry: {...},
    properties: {...}
}

expect(testFeature).toHaveNumericID()
expect(testFeature).toHaveNumericID(456)
expect(testFeature).toHaveNumericID([1, 123, 345, /[a-z]+[0-9]+/])

expect(testFeatureNoID).not.toHaveNumericID()
expect(testFeatureStringID).not.toHaveNumericID()
expect(testFeatureStringID).not.toHaveNumericID('f1')

Passing Tests

Numeric IDs

Input:

  • 1
  • 0
  • 5000
  • -Infinity
  • Infinity
const testFeature = {
    type: 'Feature',
    id: <input>,
    geometry: null,
    properties: null
}
  • Using the matcher without an optional argument
  • Using the matcher with the input argument
  • Using the matcher with the input argument as a single element array
  • Using the matcher with the input argument as a RegExp
  • Using the matcher with the input argument as a single element array as a RegExp

Empty Array for Optional Argument

const testFeature = {
    type: 'Feature',
    id: 719,
    geometry: null,
    properties: null
}

expect(feature).toHaveNumericID([])

Multiple Array Value Checking for Numeric ID

const testFeature = {
    type: 'Feature',
    id: 719,
    geometry: null,
    properties: null
}

Only one element passes:

  • [1, 719, 0, 123]
  • [-5, /7/, 15]
  • [/719/, /123/, /\bString\b/]

More than one element passes:

  • [/72[0-9]/, /\bString\b/, /71[0-9]/, 719]

Failing Tests

Invalid Inputs To Matcher

Rejects each of the following:

  • Each of the seven Geometry objects
  • FeatureCollection object
  • undefined, null, false, true, 0
  • { someProp: 'I am not GeoJSON', id: 4 }
  • '',
  • 'Random Feature',
  • JSON.stringify({
          type: 'Feature',
          geometry: null,
          properties: null
      })
    

Otherwise Valid String IDs Fail

Input:

  • '1'
  • 'Test 123'
  • 'Random ID String'
  • ''
const testFeature = {
    type: 'Feature',
    id: <input>,
    geometry: null,
    properties: null
}
  • Using the matcher without an optional argument
  • With an input argument of an empty array
  • Using the matcher with the input argument
  • Using the matcher with the input argument as a single element array
  • Using the matcher with the input argument as a RegExp
  • Using the matcher with the input argument as a single element array as a RegExp

Invalid Inputs To Optional Argument

Rejects when the optional ID to check is

  • Each of the seven Geometry objects
  • Feature and FeatureCollection object
  • String values: 'Some String', '', '1'
  • undefined, null, false, true
  • { someProp: 'I am not GeoJSON', id: 4 }
  • an empty object: {}
  • NaN

Rejects when ID Does Not Match Optional Input Value

  • String ID that does not match
  • String ID that does match
  • Array of String IDs that do not match
  • RegExp does not match
  • Array of RegExp does not match
@M-Scott-Lassiter M-Scott-Lassiter added the new matcher proposal Proposal for a new GeoJSON matcher label Jun 7, 2022
@M-Scott-Lassiter M-Scott-Lassiter self-assigned this Jun 7, 2022
@M-Scott-Lassiter M-Scott-Lassiter added matchers/features and removed new matcher proposal Proposal for a new GeoJSON matcher labels Jun 8, 2022
github-actions bot pushed a commit that referenced this issue Jun 8, 2022
## [1.3.0](v1.2.0...v1.3.0) (2022-06-08)

### 🎁 Feature Changes

* **toHaveNumericID:** add new matcher ([56e3e4c](56e3e4c)), closes [#38](#38)
* **toHaveStringID:** add new matcher ([cefddd6](cefddd6)), closes [#37](#37)
@M-Scott-Lassiter
Copy link
Owner Author

🎉 This issue has been resolved in version 1.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant