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

toHaveStringID #37

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

toHaveStringID #37

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 string. To check for either number only, use toHaveNumericID. 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 string, 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 string. Otherwise, it will check that the ID member exactly matches either the single input or any of the array values.

Passing a number type to this optional argument will not pass the test, even if the id exactly matches.

Valid GeoJSON Examples

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

Example Matcher Usage

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

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

const testFeatureNumID = {
    type: 'Feature',
    id: 2,
    geometry: {...},
    properties: {...}
}

expect(testFeature).toHaveStringID()
expect(testFeature).toHaveStringID('f1')
expect(testFeature).toHaveStringID(['1', 'F', 'F12', /[a-z]+[0-9]+/])

expect(testFeatureNoID).not.toHaveStringID()
expect(testFeatureNumID).not.toHaveStringID()
expect(testFeatureNumID).not.toHaveStringID(2)

Passing Tests

String IDs

Input:

  • '1'
  • 'Test 123'
  • 'Random ID String'
  • Empty string: ''
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: 'Test ID',
    geometry: null,
    properties: null
}

expect(feature).toHaveStringID([])

Multiple Array Value Checking for String ID

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

Only one element passes:

  • ['1', '#719', 'TestID', 'Some String']
  • ['A String ID', /Some/, '2']
  • [/SomeString/, /123/, /\bString\b/]

More than one element passes:

  • [/72[0-9]/, /\bString\b/, /71[0-9]/, /Some/, 'Some String']

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 Numeric IDs Fail

Input:

  • 0
  • -200
  • 200
  • Infinity
  • -Infinity
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
  • 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
  • Array of String IDs that do not match
  • RegExp does not match
  • Array of RegExp does not match
  • Numeric ID that does 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 added this to To do in Features via automation Jun 7, 2022
@M-Scott-Lassiter M-Scott-Lassiter self-assigned this Jun 7, 2022
@M-Scott-Lassiter M-Scott-Lassiter moved this from To do to In progress in Features Jun 8, 2022
Features automation moved this from In progress to Done Jun 8, 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
Features
  
Done
Development

No branches or pull requests

1 participant