geojson.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { dataFrameToJSON, FieldType } from '@grafana/data';
  2. import { frameFromGeoJSON } from './geojson';
  3. describe('Read GeoJSON', () => {
  4. it('supports simple read', () => {
  5. const frame = frameFromGeoJSON({
  6. type: 'FeatureCollection',
  7. features: [
  8. {
  9. id: 'A',
  10. type: 'Feature',
  11. geometry: {
  12. type: 'Point',
  13. coordinates: [0, 0],
  14. },
  15. properties: {
  16. hello: 'A',
  17. },
  18. },
  19. {
  20. type: 'Feature',
  21. geometry: {
  22. type: 'Point',
  23. coordinates: [1, 1],
  24. },
  25. properties: {
  26. number: 1.2,
  27. hello: 'B',
  28. mixed: 'first',
  29. },
  30. },
  31. {
  32. type: 'Feature',
  33. geometry: {
  34. type: 'Point',
  35. coordinates: [2, 2],
  36. },
  37. properties: {
  38. number: 2.3,
  39. mixed: 2,
  40. },
  41. },
  42. ],
  43. });
  44. const msg = dataFrameToJSON(frame);
  45. expect(msg.schema).toMatchInlineSnapshot(`
  46. Object {
  47. "fields": Array [
  48. Object {
  49. "config": Object {},
  50. "name": "id",
  51. "type": "string",
  52. },
  53. Object {
  54. "config": Object {},
  55. "name": "geo",
  56. "type": "geo",
  57. },
  58. Object {
  59. "config": Object {},
  60. "name": "hello",
  61. "type": "string",
  62. },
  63. Object {
  64. "config": Object {},
  65. "name": "number",
  66. "type": "number",
  67. },
  68. Object {
  69. "config": Object {},
  70. "name": "mixed",
  71. "type": "string",
  72. },
  73. ],
  74. "meta": undefined,
  75. "name": undefined,
  76. "refId": undefined,
  77. }
  78. `);
  79. expect(
  80. frame.fields.reduce((acc, v, idx, arr) => {
  81. if (v.type !== FieldType.geo) {
  82. acc[v.name] = v.values.toArray();
  83. }
  84. return acc;
  85. }, {} as any)
  86. ).toMatchInlineSnapshot(`
  87. Object {
  88. "hello": Array [
  89. "A",
  90. "B",
  91. null,
  92. ],
  93. "id": Array [
  94. "A",
  95. null,
  96. null,
  97. ],
  98. "mixed": Array [
  99. null,
  100. "first",
  101. "2",
  102. ],
  103. "number": Array [
  104. null,
  105. 1.2,
  106. 2.3,
  107. ],
  108. }
  109. `);
  110. });
  111. });