matchers/geometries/toBePolygonGeometry.js

  1. const { polygonGeometry } = require('../../core/geometries/polygonGeometry')
  2. // eslint-disable-next-line jsdoc/require-returns
  3. /**
  4. * Verifies an object is a valid GeoJSON Polygon Geometry. This geometry requires a
  5. * 'type' property that must equal "Polygon", and a 'coordinates' property that contains
  6. * an array of linestring arrays. Each point array must contain at least four valid
  7. * WGS-84 GeoJSON coordinates, and the final coordinate must equal the first.
  8. *
  9. * The coordinates may be an empty array, but may not be an array of empty arrays.
  10. *
  11. * Foreign members are allowed with the exception of 'geometry', 'properties', or 'features'.
  12. * If present, bounding boxes must be valid.
  13. *
  14. * @memberof Matchers.Geometries
  15. * @see https://github.com/M-Scott-Lassiter/jest-geojson/issues/13
  16. * @param {object} geometryObject a GeoJSON Polygon Geometry object
  17. * @example
  18. * const polygon = {
  19. * "type": "Polygon",
  20. * "coordinates": [
  21. * [
  22. * [100.0, 0.0],
  23. * [101.0, 0.0],
  24. * [101.0, 1.0],
  25. * [100.0, 1.0],
  26. * [100.0, 0.0]
  27. * ]
  28. * ]
  29. * }
  30. *
  31. * test('Object is valid GeoJSON', () => {
  32. * expect(polygon).toBePolygonGeometry()
  33. * })
  34. * @example
  35. * const point = {
  36. * type: "Point",
  37. * coordinates: [100.0, 0.0]
  38. * }
  39. * const polygonEndDoesNotEqualStart = {
  40. * "type": "Polygon",
  41. * "coordinates": [
  42. * [
  43. * [100.0, 0.0],
  44. * [101.0, 0.0],
  45. * [101.0, 1.0],
  46. * [100.0, 1.0]
  47. * ]
  48. * ]
  49. * }
  50. *
  51. * test('Object is valid GeoJSON', () => {
  52. * expect(point).not.toBePolygonGeometry()
  53. * expect(polygonEndDoesNotEqualStart).not.toBePolygonGeometry()
  54. * })
  55. */
  56. function toBePolygonGeometry(geometryObject) {
  57. const { printReceived, matcherHint } = this.utils
  58. const passMessage =
  59. // eslint-disable-next-line prefer-template
  60. matcherHint('.not.toBePolygonGeometry', 'GeometryObject', '') +
  61. '\n\n' +
  62. `Expected input to not be a valid GeoJSON Polygon geometry.\n\n` +
  63. `Received: ${printReceived(geometryObject)}`
  64. /**
  65. * Combines a custom error message with built in Jest tools to provide a more descriptive error
  66. * meessage to the end user.
  67. *
  68. * @param {string} errorMessage Error message text to return to the user
  69. * @returns {string} Concatenated Jest test result string
  70. */
  71. function failMessage(errorMessage) {
  72. return (
  73. // eslint-disable-next-line prefer-template, no-unused-expressions
  74. matcherHint('.toBePolygonGeometry', 'GeometryObject', '') +
  75. '\n\n' +
  76. `${errorMessage}\n\n` +
  77. `Received: ${printReceived(geometryObject)}`
  78. )
  79. }
  80. try {
  81. polygonGeometry(geometryObject)
  82. } catch (err) {
  83. return { pass: false, message: () => failMessage(err.message) }
  84. }
  85. return { pass: true, message: () => passMessage }
  86. }
  87. exports.toBePolygonGeometry = toBePolygonGeometry