@geospatial-sdk/core 0.0.5-alpha.1 → 0.0.5-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/dist/fixtures/geojson.fixtures.d.ts +4 -0
  2. package/dist/fixtures/geojson.fixtures.d.ts.map +1 -0
  3. package/dist/fixtures/geojson.fixtures.js +55121 -0
  4. package/dist/fixtures/map-context.fixtures.d.ts +16 -0
  5. package/dist/fixtures/map-context.fixtures.d.ts.map +1 -0
  6. package/dist/fixtures/map-context.fixtures.js +73 -0
  7. package/dist/lib/index.d.ts +3 -0
  8. package/dist/lib/index.d.ts.map +1 -0
  9. package/dist/lib/index.js +3 -0
  10. package/dist/lib/model/index.d.ts +3 -0
  11. package/dist/lib/model/index.d.ts.map +1 -0
  12. package/dist/lib/model/index.js +2 -0
  13. package/dist/lib/model/map-context-diff.d.ts +35 -0
  14. package/dist/lib/model/map-context-diff.d.ts.map +1 -0
  15. package/dist/lib/model/map-context-diff.js +1 -0
  16. package/dist/lib/model/map-context.d.ts +78 -0
  17. package/dist/lib/model/map-context.d.ts.map +1 -0
  18. package/dist/lib/model/map-context.js +1 -0
  19. package/dist/lib/utils/freeze.d.ts +2 -0
  20. package/dist/lib/utils/freeze.d.ts.map +1 -0
  21. package/dist/lib/utils/freeze.js +15 -0
  22. package/dist/lib/utils/freeze.test.d.ts +2 -0
  23. package/dist/lib/utils/freeze.test.d.ts.map +1 -0
  24. package/dist/lib/utils/freeze.test.js +51 -0
  25. package/dist/lib/utils/index.d.ts +5 -0
  26. package/dist/lib/utils/index.d.ts.map +1 -0
  27. package/dist/lib/utils/index.js +4 -0
  28. package/dist/lib/utils/map-context-diff.d.ts +22 -0
  29. package/dist/lib/utils/map-context-diff.d.ts.map +1 -0
  30. package/dist/lib/utils/map-context-diff.js +79 -0
  31. package/dist/lib/utils/map-context-diff.test.d.ts +2 -0
  32. package/dist/lib/utils/map-context-diff.test.d.ts.map +1 -0
  33. package/dist/lib/utils/map-context-diff.test.js +217 -0
  34. package/dist/lib/utils/map-context.d.ts +6 -0
  35. package/dist/lib/utils/map-context.d.ts.map +1 -0
  36. package/dist/lib/utils/map-context.js +36 -0
  37. package/dist/lib/utils/map-context.test.d.ts +2 -0
  38. package/dist/lib/utils/map-context.test.d.ts.map +1 -0
  39. package/dist/lib/utils/map-context.test.js +131 -0
  40. package/dist/lib/utils/url.d.ts +7 -0
  41. package/dist/lib/utils/url.d.ts.map +1 -0
  42. package/dist/lib/utils/url.js +17 -0
  43. package/dist/lib/utils/url.test.d.ts +2 -0
  44. package/dist/lib/utils/url.test.d.ts.map +1 -0
  45. package/dist/lib/utils/url.test.js +8 -0
  46. package/dist/utils/index.d.ts +1 -0
  47. package/dist/utils/index.d.ts.map +1 -1
  48. package/dist/utils/index.js +1 -0
  49. package/dist/utils/map-context-diff.d.ts +1 -4
  50. package/dist/utils/map-context-diff.d.ts.map +1 -1
  51. package/dist/utils/map-context-diff.js +1 -33
  52. package/dist/utils/map-context-diff.test.js +1 -120
  53. package/dist/utils/map-context.d.ts +6 -0
  54. package/dist/utils/map-context.d.ts.map +1 -0
  55. package/dist/utils/map-context.js +36 -0
  56. package/dist/utils/map-context.test.d.ts +2 -0
  57. package/dist/utils/map-context.test.d.ts.map +1 -0
  58. package/dist/utils/map-context.test.js +150 -0
  59. package/lib/model/map-context-diff.ts +4 -4
  60. package/lib/utils/index.ts +1 -0
  61. package/lib/utils/map-context-diff.test.ts +8 -310
  62. package/lib/utils/map-context-diff.ts +1 -43
  63. package/lib/utils/map-context.test.ts +294 -0
  64. package/lib/utils/map-context.ts +51 -0
  65. package/package.json +2 -2
@@ -1,223 +1,15 @@
1
1
  import { MapContext, MapContextDiff, MapContextLayer } from "../model";
2
- import { deepFreeze } from "./freeze";
2
+ import { computeMapContextDiff } from "./map-context-diff";
3
3
  import {
4
- computeMapContextDiff,
5
- getLayerHash,
6
- isLayerSame,
7
- isLayerSameAndUnchanged,
8
- } from "./map-context-diff";
9
-
10
- const SAMPLE_CONTEXT: MapContext = deepFreeze({
11
- view: {
12
- center: [10, 20],
13
- zoom: 3,
14
- extent: [40, 50, 60, 70],
15
- },
16
- layers: [],
17
- });
18
-
19
- const SAMPLE_LAYER1: MapContextLayer = deepFreeze({
20
- type: "wms",
21
- url: "http://abc.org/wms",
22
- name: "myLayer",
23
- extras: { myField: "abc" },
24
- });
25
- const SAMPLE_LAYER2: MapContextLayer = deepFreeze({
26
- type: "xyz",
27
- url: "http://abc.org/tiles",
28
- extras: { myField2: "123" },
29
- });
30
- const SAMPLE_LAYER3: MapContextLayer = deepFreeze({
31
- type: "geojson",
32
- data: '{ "type": "Feature", "properties": {}}',
33
- extras: { myField3: "000" },
34
- });
35
- const SAMPLE_LAYER4: MapContextLayer = deepFreeze({
36
- type: "wfs",
37
- url: "http://abc.org/wfs",
38
- featureType: "myFeatureType",
39
- extras: { myField4: "aaa" },
40
- });
41
- const SAMPLE_LAYER5: MapContextLayer = deepFreeze({
42
- type: "xyz",
43
- url: "http://my.tiles/server",
44
- });
4
+ SAMPLE_CONTEXT,
5
+ SAMPLE_LAYER1,
6
+ SAMPLE_LAYER2,
7
+ SAMPLE_LAYER3,
8
+ SAMPLE_LAYER4,
9
+ SAMPLE_LAYER5,
10
+ } from "../../fixtures/map-context.fixtures";
45
11
 
46
12
  describe("Context diff utils", () => {
47
- describe("isLayerSame", () => {
48
- describe("layers with id", () => {
49
- it("compares non-strictly by id", () => {
50
- expect(
51
- isLayerSame(
52
- { ...SAMPLE_LAYER1, id: "a" },
53
- { ...SAMPLE_LAYER1, id: "b" },
54
- ),
55
- ).toBe(false);
56
- expect(
57
- isLayerSame(
58
- { ...SAMPLE_LAYER1, id: "ab" },
59
- { ...SAMPLE_LAYER2, id: "ab" },
60
- ),
61
- ).toBe(true);
62
- expect(
63
- isLayerSame(
64
- { ...SAMPLE_LAYER1, id: 1 },
65
- { ...SAMPLE_LAYER2, id: "01" },
66
- ),
67
- ).toBe(true);
68
- expect(
69
- isLayerSame({ ...SAMPLE_LAYER1, id: 1 }, { ...SAMPLE_LAYER2, id: 1 }),
70
- ).toBe(true);
71
- expect(
72
- isLayerSame({ ...SAMPLE_LAYER1, id: 1 }, { ...SAMPLE_LAYER1, id: 2 }),
73
- ).toBe(false);
74
- });
75
- });
76
- describe("layers without id", () => {
77
- it("compares by properties", () => {
78
- expect(isLayerSame(SAMPLE_LAYER1, SAMPLE_LAYER1)).toBe(true);
79
- expect(
80
- isLayerSame(SAMPLE_LAYER1, { ...SAMPLE_LAYER1, name: "abc" }),
81
- ).toBe(false);
82
- expect(
83
- isLayerSame(
84
- {
85
- ...SAMPLE_LAYER1,
86
- url: "http://abc.org",
87
- name: SAMPLE_LAYER1.name,
88
- },
89
- { ...SAMPLE_LAYER1, url: "http://abc.org" },
90
- ),
91
- ).toBe(true);
92
- expect(isLayerSame(SAMPLE_LAYER1, SAMPLE_LAYER2)).toBe(false);
93
- });
94
- it("ignores extras prop", () => {
95
- expect(
96
- isLayerSame(SAMPLE_LAYER1, {
97
- ...SAMPLE_LAYER1,
98
- extras: { otherProp: "abc" },
99
- }),
100
- ).toBe(true);
101
- expect(
102
- isLayerSame(SAMPLE_LAYER1, {
103
- ...SAMPLE_LAYER1,
104
- extras: undefined,
105
- }),
106
- ).toBe(true);
107
- const { extras, ...layer } = SAMPLE_LAYER1;
108
- expect(isLayerSame(SAMPLE_LAYER1, layer)).toBe(true);
109
- });
110
- });
111
- describe("layers with and without id", () => {
112
- it("compares by properties", () => {
113
- expect(
114
- isLayerSame(SAMPLE_LAYER1, { ...SAMPLE_LAYER1, id: "123" }),
115
- ).toBe(false);
116
- expect(
117
- isLayerSame(
118
- { ...SAMPLE_LAYER1, id: "123" },
119
- { id: "123", ...SAMPLE_LAYER1, name: SAMPLE_LAYER1.name },
120
- ),
121
- ).toBe(true);
122
- });
123
- });
124
- });
125
-
126
- describe("isLayerSameAndUnchanged", () => {
127
- describe("layers with id", () => {
128
- it("compares non-strictly by id and version field", () => {
129
- expect(
130
- isLayerSameAndUnchanged(
131
- { ...SAMPLE_LAYER1, id: "a" },
132
- { ...SAMPLE_LAYER1, id: "b" },
133
- ),
134
- ).toBe(false);
135
- expect(
136
- isLayerSameAndUnchanged(
137
- { ...SAMPLE_LAYER1, id: "a" },
138
- { ...SAMPLE_LAYER2, id: "a" },
139
- ),
140
- ).toBe(true);
141
- expect(
142
- isLayerSameAndUnchanged(
143
- { ...SAMPLE_LAYER1, id: "ab", version: 2 },
144
- { ...SAMPLE_LAYER2, id: "ab", version: 2 },
145
- ),
146
- ).toBe(true);
147
- expect(
148
- isLayerSameAndUnchanged(
149
- { ...SAMPLE_LAYER1, id: "ab", version: 2 },
150
- { ...SAMPLE_LAYER1, id: "ab", version: 1 },
151
- ),
152
- ).toBe(false);
153
- expect(
154
- isLayerSameAndUnchanged(
155
- { ...SAMPLE_LAYER1, id: 1 },
156
- { ...SAMPLE_LAYER1, id: "01", version: 3 },
157
- ),
158
- ).toBe(false);
159
- });
160
- });
161
- describe("layers without id", () => {
162
- it("compares by properties, including extras", () => {
163
- expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, SAMPLE_LAYER1)).toBe(
164
- true,
165
- );
166
- expect(
167
- isLayerSameAndUnchanged(SAMPLE_LAYER1, {
168
- ...SAMPLE_LAYER1,
169
- name: "abc",
170
- }),
171
- ).toBe(false);
172
- expect(
173
- isLayerSameAndUnchanged(
174
- {
175
- ...SAMPLE_LAYER1,
176
- url: "http://abc.org",
177
- name: SAMPLE_LAYER1.name,
178
- },
179
- { ...SAMPLE_LAYER1, url: "http://abc.org" },
180
- ),
181
- ).toBe(true);
182
- expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, SAMPLE_LAYER2)).toBe(
183
- false,
184
- );
185
- });
186
- it("takes into account extras prop", () => {
187
- expect(
188
- isLayerSameAndUnchanged(SAMPLE_LAYER1, {
189
- ...SAMPLE_LAYER1,
190
- extras: { otherProp: "abc" },
191
- }),
192
- ).toBe(false);
193
- expect(
194
- isLayerSameAndUnchanged(SAMPLE_LAYER1, {
195
- ...SAMPLE_LAYER1,
196
- extras: undefined,
197
- }),
198
- ).toBe(false);
199
- const { extras, ...layer } = SAMPLE_LAYER1;
200
- expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, layer)).toBe(false);
201
- });
202
- });
203
- describe("layers with and without id", () => {
204
- it("compares by properties", () => {
205
- expect(
206
- isLayerSameAndUnchanged(SAMPLE_LAYER1, {
207
- ...SAMPLE_LAYER1,
208
- id: "123",
209
- }),
210
- ).toBe(false);
211
- expect(
212
- isLayerSameAndUnchanged(
213
- { ...SAMPLE_LAYER1, id: "123" },
214
- { id: "123", ...SAMPLE_LAYER1, name: SAMPLE_LAYER1.name },
215
- ),
216
- ).toBe(true);
217
- });
218
- });
219
- });
220
-
221
13
  describe("computeMapContextDiff", () => {
222
14
  let contextOld: MapContext;
223
15
  let contextNew: MapContext;
@@ -480,98 +272,4 @@ describe("Context diff utils", () => {
480
272
  });
481
273
  });
482
274
  });
483
-
484
- describe("getLayerHash", () => {
485
- it("works with serializable entities", () => {
486
- expect(
487
- getLayerHash({
488
- ...SAMPLE_LAYER1,
489
- extras: {
490
- array: [11, 22, null, undefined],
491
- object: {},
492
- undef: undefined,
493
- },
494
- }),
495
- ).toBeTypeOf("string");
496
- });
497
- it("ignores extra property by default", () => {
498
- expect(
499
- getLayerHash({
500
- ...SAMPLE_LAYER1,
501
- extras: {
502
- array: [11, 22, null, undefined],
503
- object: {},
504
- },
505
- }),
506
- ).toEqual(
507
- getLayerHash({
508
- ...SAMPLE_LAYER1,
509
- extras: { hello: "world" },
510
- }),
511
- );
512
- });
513
- it("works with non-serializable entities (but they are ignored)", () => {
514
- const hash = getLayerHash(
515
- {
516
- ...SAMPLE_LAYER1,
517
- extras: {
518
- func: () => 123,
519
- canvas: document.createElement("canvas"),
520
- },
521
- },
522
- true,
523
- );
524
- expect(hash).toBeTypeOf("string");
525
- expect(hash).toEqual(
526
- getLayerHash(
527
- {
528
- ...SAMPLE_LAYER1,
529
- extras: {
530
- func: () => 456,
531
- canvas: document.createElement("div"),
532
- },
533
- },
534
- true,
535
- ),
536
- );
537
- expect(hash).not.toEqual(
538
- getLayerHash(
539
- {
540
- ...SAMPLE_LAYER1,
541
- extras: {},
542
- },
543
- true,
544
- ),
545
- );
546
- });
547
- it("does not take into account properties order", () => {
548
- expect(
549
- getLayerHash(
550
- {
551
- type: "wms",
552
- url: "http://abc.org/wms",
553
- name: "myLayer",
554
- extras: {
555
- array: [1, 2, 3],
556
- object: {},
557
- },
558
- },
559
- true,
560
- ),
561
- ).toEqual(
562
- getLayerHash(
563
- {
564
- extras: {
565
- array: [1, 2, 3],
566
- object: {},
567
- },
568
- url: "http://abc.org/wms",
569
- type: "wms",
570
- name: "myLayer",
571
- },
572
- true,
573
- ),
574
- );
575
- });
576
- });
577
275
  });
@@ -6,49 +6,7 @@ import {
6
6
  MapContextLayerReordered,
7
7
  MapContextView,
8
8
  } from "../model";
9
-
10
- export function getLayerHash(
11
- layer: MapContextLayer,
12
- includeExtras = false,
13
- ): string {
14
- function getHash(input: unknown): string {
15
- if (input instanceof Object) {
16
- const obj: Record<string, string> = {};
17
- const keys = Object.keys(input).sort();
18
- for (const key of keys) {
19
- if (!includeExtras && key === "extras") continue;
20
- obj[key] = getHash(input[key as keyof typeof input]);
21
- }
22
- const hash = JSON.stringify(obj)
23
- .split("")
24
- .reduce((prev, curr) => (prev << 5) - prev + curr.charCodeAt(0), 0);
25
- return (hash >>> 0).toString();
26
- } else {
27
- return JSON.stringify(input);
28
- }
29
- }
30
- return getHash(layer);
31
- }
32
-
33
- export function isLayerSame(
34
- layerA: MapContextLayer,
35
- layerB: MapContextLayer,
36
- ): boolean {
37
- if ("id" in layerA && "id" in layerB) {
38
- return layerA.id == layerB.id;
39
- }
40
- return getLayerHash(layerA) === getLayerHash(layerB);
41
- }
42
-
43
- export function isLayerSameAndUnchanged(
44
- layerA: MapContextLayer,
45
- layerB: MapContextLayer,
46
- ): boolean {
47
- if ("id" in layerA && "id" in layerB) {
48
- return layerA.id == layerB.id && layerA.version == layerB.version;
49
- }
50
- return getLayerHash(layerA, true) === getLayerHash(layerB, true);
51
- }
9
+ import { isLayerSame, isLayerSameAndUnchanged } from "./map-context";
52
10
 
53
11
  /**
54
12
  * The following logic is produced by identifying layers in both context
@@ -0,0 +1,294 @@
1
+ import { describe } from "vitest";
2
+ import {
3
+ getLayerHash,
4
+ getLayerPosition,
5
+ isLayerSame,
6
+ isLayerSameAndUnchanged,
7
+ } from "./map-context";
8
+ import { MapContext, MapContextLayer } from "../model";
9
+ import {
10
+ SAMPLE_CONTEXT,
11
+ SAMPLE_LAYER1,
12
+ SAMPLE_LAYER2,
13
+ } from "../../fixtures/map-context.fixtures";
14
+
15
+ describe("Map context utils", () => {
16
+ describe("isLayerSame", () => {
17
+ describe("layers with id", () => {
18
+ it("compares non-strictly by id", () => {
19
+ expect(
20
+ isLayerSame(
21
+ { ...SAMPLE_LAYER1, id: "a" },
22
+ { ...SAMPLE_LAYER1, id: "b" },
23
+ ),
24
+ ).toBe(false);
25
+ expect(
26
+ isLayerSame(
27
+ { ...SAMPLE_LAYER1, id: "ab" },
28
+ { ...SAMPLE_LAYER2, id: "ab" },
29
+ ),
30
+ ).toBe(true);
31
+ expect(
32
+ isLayerSame(
33
+ { ...SAMPLE_LAYER1, id: 1 },
34
+ { ...SAMPLE_LAYER2, id: "01" },
35
+ ),
36
+ ).toBe(true);
37
+ expect(
38
+ isLayerSame({ ...SAMPLE_LAYER1, id: 1 }, { ...SAMPLE_LAYER2, id: 1 }),
39
+ ).toBe(true);
40
+ expect(
41
+ isLayerSame({ ...SAMPLE_LAYER1, id: 1 }, { ...SAMPLE_LAYER1, id: 2 }),
42
+ ).toBe(false);
43
+ });
44
+ });
45
+ describe("layers without id", () => {
46
+ it("compares by properties", () => {
47
+ expect(isLayerSame(SAMPLE_LAYER1, SAMPLE_LAYER1)).toBe(true);
48
+ expect(
49
+ isLayerSame(SAMPLE_LAYER1, { ...SAMPLE_LAYER1, name: "abc" }),
50
+ ).toBe(false);
51
+ expect(
52
+ isLayerSame(
53
+ {
54
+ ...SAMPLE_LAYER1,
55
+ url: "http://abc.org",
56
+ name: SAMPLE_LAYER1.name,
57
+ },
58
+ { ...SAMPLE_LAYER1, url: "http://abc.org" },
59
+ ),
60
+ ).toBe(true);
61
+ expect(isLayerSame(SAMPLE_LAYER1, SAMPLE_LAYER2)).toBe(false);
62
+ });
63
+ it("ignores extras prop", () => {
64
+ expect(
65
+ isLayerSame(SAMPLE_LAYER1, {
66
+ ...SAMPLE_LAYER1,
67
+ extras: { otherProp: "abc" },
68
+ }),
69
+ ).toBe(true);
70
+ expect(
71
+ isLayerSame(SAMPLE_LAYER1, {
72
+ ...SAMPLE_LAYER1,
73
+ extras: undefined,
74
+ }),
75
+ ).toBe(true);
76
+ const { extras, ...layer } = SAMPLE_LAYER1;
77
+ expect(isLayerSame(SAMPLE_LAYER1, layer)).toBe(true);
78
+ });
79
+ });
80
+ describe("layers with and without id", () => {
81
+ it("compares by properties", () => {
82
+ expect(
83
+ isLayerSame(SAMPLE_LAYER1, { ...SAMPLE_LAYER1, id: "123" }),
84
+ ).toBe(false);
85
+ expect(
86
+ isLayerSame(
87
+ { ...SAMPLE_LAYER1, id: "123" },
88
+ { id: "123", ...SAMPLE_LAYER1, name: SAMPLE_LAYER1.name },
89
+ ),
90
+ ).toBe(true);
91
+ });
92
+ });
93
+ });
94
+
95
+ describe("isLayerSameAndUnchanged", () => {
96
+ describe("layers with id", () => {
97
+ it("compares non-strictly by id and version field", () => {
98
+ expect(
99
+ isLayerSameAndUnchanged(
100
+ { ...SAMPLE_LAYER1, id: "a" },
101
+ { ...SAMPLE_LAYER1, id: "b" },
102
+ ),
103
+ ).toBe(false);
104
+ expect(
105
+ isLayerSameAndUnchanged(
106
+ { ...SAMPLE_LAYER1, id: "a" },
107
+ { ...SAMPLE_LAYER2, id: "a" },
108
+ ),
109
+ ).toBe(true);
110
+ expect(
111
+ isLayerSameAndUnchanged(
112
+ { ...SAMPLE_LAYER1, id: "ab", version: 2 },
113
+ { ...SAMPLE_LAYER2, id: "ab", version: 2 },
114
+ ),
115
+ ).toBe(true);
116
+ expect(
117
+ isLayerSameAndUnchanged(
118
+ { ...SAMPLE_LAYER1, id: "ab", version: 2 },
119
+ { ...SAMPLE_LAYER1, id: "ab", version: 1 },
120
+ ),
121
+ ).toBe(false);
122
+ expect(
123
+ isLayerSameAndUnchanged(
124
+ { ...SAMPLE_LAYER1, id: 1 },
125
+ { ...SAMPLE_LAYER1, id: "01", version: 3 },
126
+ ),
127
+ ).toBe(false);
128
+ });
129
+ });
130
+ describe("layers without id", () => {
131
+ it("compares by properties, including extras", () => {
132
+ expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, SAMPLE_LAYER1)).toBe(
133
+ true,
134
+ );
135
+ expect(
136
+ isLayerSameAndUnchanged(SAMPLE_LAYER1, {
137
+ ...SAMPLE_LAYER1,
138
+ name: "abc",
139
+ }),
140
+ ).toBe(false);
141
+ expect(
142
+ isLayerSameAndUnchanged(
143
+ {
144
+ ...SAMPLE_LAYER1,
145
+ url: "http://abc.org",
146
+ name: SAMPLE_LAYER1.name,
147
+ },
148
+ { ...SAMPLE_LAYER1, url: "http://abc.org" },
149
+ ),
150
+ ).toBe(true);
151
+ expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, SAMPLE_LAYER2)).toBe(
152
+ false,
153
+ );
154
+ });
155
+ it("takes into account extras prop", () => {
156
+ expect(
157
+ isLayerSameAndUnchanged(SAMPLE_LAYER1, {
158
+ ...SAMPLE_LAYER1,
159
+ extras: { otherProp: "abc" },
160
+ }),
161
+ ).toBe(false);
162
+ expect(
163
+ isLayerSameAndUnchanged(SAMPLE_LAYER1, {
164
+ ...SAMPLE_LAYER1,
165
+ extras: undefined,
166
+ }),
167
+ ).toBe(false);
168
+ const { extras, ...layer } = SAMPLE_LAYER1;
169
+ expect(isLayerSameAndUnchanged(SAMPLE_LAYER1, layer)).toBe(false);
170
+ });
171
+ });
172
+ describe("layers with and without id", () => {
173
+ it("compares by properties", () => {
174
+ expect(
175
+ isLayerSameAndUnchanged(SAMPLE_LAYER1, {
176
+ ...SAMPLE_LAYER1,
177
+ id: "123",
178
+ }),
179
+ ).toBe(false);
180
+ expect(
181
+ isLayerSameAndUnchanged(
182
+ { ...SAMPLE_LAYER1, id: "123" },
183
+ { id: "123", ...SAMPLE_LAYER1, name: SAMPLE_LAYER1.name },
184
+ ),
185
+ ).toBe(true);
186
+ });
187
+ });
188
+ });
189
+
190
+ describe("getLayerHash", () => {
191
+ it("works with serializable entities", () => {
192
+ expect(
193
+ getLayerHash({
194
+ ...SAMPLE_LAYER1,
195
+ extras: {
196
+ array: [11, 22, null, undefined],
197
+ object: {},
198
+ undef: undefined,
199
+ },
200
+ }),
201
+ ).toBeTypeOf("string");
202
+ });
203
+ it("ignores extra property by default", () => {
204
+ expect(
205
+ getLayerHash({
206
+ ...SAMPLE_LAYER1,
207
+ extras: {
208
+ array: [11, 22, null, undefined],
209
+ object: {},
210
+ },
211
+ }),
212
+ ).toEqual(
213
+ getLayerHash({
214
+ ...SAMPLE_LAYER1,
215
+ extras: { hello: "world" },
216
+ }),
217
+ );
218
+ });
219
+ it("works with non-serializable entities (but they are ignored)", () => {
220
+ const hash = getLayerHash(
221
+ {
222
+ ...SAMPLE_LAYER1,
223
+ extras: {
224
+ func: () => 123,
225
+ canvas: document.createElement("canvas"),
226
+ },
227
+ },
228
+ true,
229
+ );
230
+ expect(hash).toBeTypeOf("string");
231
+ expect(hash).toEqual(
232
+ getLayerHash(
233
+ {
234
+ ...SAMPLE_LAYER1,
235
+ extras: {
236
+ func: () => 456,
237
+ canvas: document.createElement("div"),
238
+ },
239
+ },
240
+ true,
241
+ ),
242
+ );
243
+ expect(hash).not.toEqual(
244
+ getLayerHash(
245
+ {
246
+ ...SAMPLE_LAYER1,
247
+ extras: {},
248
+ },
249
+ true,
250
+ ),
251
+ );
252
+ });
253
+ it("does not take into account properties order", () => {
254
+ expect(
255
+ getLayerHash(
256
+ {
257
+ type: "wms",
258
+ url: "http://abc.org/wms",
259
+ name: "myLayer",
260
+ extras: {
261
+ array: [1, 2, 3],
262
+ object: {},
263
+ },
264
+ },
265
+ true,
266
+ ),
267
+ ).toEqual(
268
+ getLayerHash(
269
+ {
270
+ extras: {
271
+ array: [1, 2, 3],
272
+ object: {},
273
+ },
274
+ url: "http://abc.org/wms",
275
+ type: "wms",
276
+ name: "myLayer",
277
+ },
278
+ true,
279
+ ),
280
+ );
281
+ });
282
+ });
283
+
284
+ describe("getLayerPosition", () => {
285
+ it("returns the index of the layer in the context", () => {
286
+ const context: MapContext = {
287
+ ...SAMPLE_CONTEXT,
288
+ layers: [SAMPLE_LAYER1, SAMPLE_LAYER2],
289
+ };
290
+ expect(getLayerPosition(context, SAMPLE_LAYER1)).toBe(0);
291
+ expect(getLayerPosition(context, SAMPLE_LAYER2)).toBe(1);
292
+ });
293
+ });
294
+ });