server/spec/fixtures/location.js

  1. /**
  2. * Test utilities to generate location-related data.
  3. *
  4. * @module server/spec/fixtures/location
  5. */
  6. const chance = require('chance').Chance();
  7. const _ = require('lodash');
  8. const { unique: uniqueGenerator } = require('test-value-generator');
  9. const Location = require('../../models/location');
  10. const { createRecord } = require('../utils');
  11. const geoJsonFixtures = require('./geojson');
  12. /**
  13. * Generates a random location record and saves it to the database.
  14. *
  15. * All of the generated location's properties are assigned random values unless
  16. * changed with the `data` argument.
  17. *
  18. * const locationFixtures = require('../spec/fixtures/location');
  19. *
  20. * const location = await locationFixtures.location({
  21. * name: 'Custom name'
  22. * });
  23. *
  24. * console.log(location.get('name')); // "Custom name"
  25. * console.log(location.get('short_name')); // "Lorem ipsum"
  26. *
  27. * @function
  28. * @param {object} [data={}] - Custom location data.
  29. * @param {object} [data.bbox] - A bounding box within which the generated location should be
  30. * @param {number[]} data.bbox.southWest - A longitude/latitude pair indicating the south-west corner of the bounding box
  31. * @param {number[]} data.bbox.northEast - A longitude/latitude pair indicating the north-east corner of the bounding box
  32. * @param {string} [data.name]
  33. * @param {string} [data.shortName] - Set to `null` to create a location without a short name.
  34. * @param {string} [data.description]
  35. * @param {string} [data.phone]
  36. * @param {string} [data.photoUrl]
  37. * @param {string} [data.siteUrl]
  38. * @param {object} [data.geometry]
  39. * @param {object} [data.properties={}]
  40. * @param {object} [data.address]
  41. * @param {string} [data.address.street]
  42. * @param {string} [data.address.number] - Set to `null` to create an address without a number
  43. * @param {string} [data.address.zipCode]
  44. * @param {string} [data.address.city]
  45. * @param {string} [data.address.state]
  46. * @returns {Promise<Location>} A promise that will be resolved with the saved location.
  47. */
  48. exports.location = function(data = {}) {
  49. return createRecord(Location, {
  50. name: data.name || exports.name(),
  51. short_name: _.has(data, 'shortName') ? data.shortName : exports.shortName(),
  52. description: data.description || chance.paragraph(),
  53. phone: data.phone || chance.phone(),
  54. photo_url: data.phoneUrl || chance.url({ domain: 'example.com', extensions: [ 'jpg' ] }),
  55. site_url: data.siteUrl || chance.url({ domain: 'example.com' }),
  56. geometry: data.geometry || geoJsonFixtures.point(_.pick(data, 'bbox')),
  57. properties: data.properties || {},
  58. address_street: _.get(data, 'address.street', chance.street()),
  59. address_number: _.get(data, 'address.number', chance.integer({ min: 1, max: 100 }).toString()),
  60. address_zip_code: _.get(data, 'address.zipCode', chance.zip()),
  61. address_city: _.get(data, 'address.city', chance.city()),
  62. address_state: _.get(data, 'address.state', chance.state()),
  63. created_at: data.createdAt,
  64. updated_at: data.updatedAt
  65. });
  66. };
  67. /**
  68. * Generates a unique random location name.
  69. *
  70. * const locationFixtures = require('../spec/fixtures/location');
  71. *
  72. * locationFixtures.name(); // "Lorem ipsum aute ad dolor exercitation labore amet"
  73. *
  74. * @function
  75. * @returns {string} A name for a location.
  76. */
  77. exports.name = uniqueGenerator(function() {
  78. return chance.sentence();
  79. });
  80. /**
  81. * Generates a unique random location short name.
  82. *
  83. * const locationFixtures = require('../spec/fixtures/location');
  84. *
  85. * locationFixtures.shortName(); // "Lorem"
  86. *
  87. * @function
  88. * @returns {string} A short name for a location.
  89. */
  90. exports.shortName = uniqueGenerator(function() {
  91. const word = chance.word();
  92. return word[0].toUpperCase() + word.slice(1);
  93. })