Add tests for TopographicMap
[aai/sparky-fe.git] / test / generic-components / map / TopographicMap.test.js
1 import TopographicMap from 'generic-components/map/TopographicMap'
2 import {PROJECTION_TYPES} from "generic-components/map/MapConstants.js"
3 import React from 'react';
4 import {shallow, mount} from 'enzyme'
5
6 describe('Utility methods tests', () => {
7     const sut = shallow(<TopographicMap/>).instance();
8
9     describe('areArraysEqual tests', () => {
10         it('Empty arrays should be equal', () => {
11              expect(sut.areArraysEqual([], [])).toBe(true);
12         });
13
14         it('Nested empty arrays should be equal', () => {
15             expect(sut.areArraysEqual([[]], [[]])).toBe(true);
16             expect(sut.areArraysEqual([[],[]], [[],[]])).toBe(true);
17         });
18
19         it('A nested empty array should not be equal to an empty array', () => {
20             expect(sut.areArraysEqual([[]], [])).toBe(false);
21         });
22
23         it('Should not coerce elements', () => {
24             expect(sut.areArraysEqual([undefined], [null])).toBe(false);
25             expect(sut.areArraysEqual([['x']],['x'])).toBe(true);
26             expect(sut.areArraysEqual([['xx']],['xx'])).toBe(false);
27             expect(sut.areArraysEqual([true], ["true"])).toBe(false);
28             expect(sut.areArraysEqual([1], ["1"])).toBe(false);
29         });
30     });
31
32     describe('extractNestedObjectInJson tests', () => {
33         it('Should return an empty object from an empty object', () => {
34             expect(sut.extractNestedObjectInJson({},['a'])).toBeUndefined();
35             expect(sut.extractNestedObjectInJson({},['a', 'b'])).toBeUndefined();
36         });
37
38         it('Should extract a nested element', () => {
39             const value = "Foo";
40             expect(sut.extractNestedObjectInJson({a:{b: value}},['a', 'b'])).toBe(value);
41         });
42     });
43 });
44
45 describe('TopographicMap integration tests', () => {
46     const svgTag = 'svg';
47     const circleTag = 'circle';
48
49     const worldBounds = [
50         {longtitude: 28.70, latitude: -127.50},
51         {longtitude: 48.85, latitude: -55.90}];
52
53     const outsideUSA = [{longitude: 19.145136, latitude: 51.919438}];
54     const insideUSA = [
55         {longitude:-122.4196396, latitude:37.7771187},
56         {longitude:-122.4196396, latitude:37.6771187},
57         {longitude:-122.4196396, latitude:37.5771187},
58         {longitude:-122.4196396, latitude:37.4771187}];
59
60     describe('AlbertsUSA projection tests', () => {
61         it('Points outside USA should not be supported', () => {
62             expect(() => mount(<TopographicMap pointArray={outsideUSA} />)).toThrow();
63             expect(() => mount(<TopographicMap pointArray={outsideUSA.concat(insideUSA)} />)).toThrow();
64             expect(() => mount(<TopographicMap pointArray={insideUSA.concat(outsideUSA)} />)).toThrow();
65         });
66
67         it('Should generate n points on a map', () => {
68             const sut = mount(<TopographicMap pointArray={insideUSA} />);
69             expect(sut.find(svgTag).find(circleTag).length).toBe(insideUSA.length);
70         });
71     });
72
73     describe('Equirectangular projection tests', () => {
74         function createEquirectangularProjection(points) {
75             return mount(<TopographicMap currentProjection={PROJECTION_TYPES.EQUIRECTANGULAR} pointArray={points} />);
76         }
77
78         it('Should display points', () => {
79             const sutInsideUSA = createEquirectangularProjection(insideUSA);
80             const sutOutsideUSA = createEquirectangularProjection(outsideUSA);
81             const sutMixed = createEquirectangularProjection(insideUSA.concat(outsideUSA));
82
83             expect(sutInsideUSA.find(svgTag).find(circleTag).length).toBe(insideUSA.length);
84             expect(sutOutsideUSA.find(svgTag).find(circleTag).length).toBe(outsideUSA.length);
85             expect(sutMixed.find(svgTag).find(circleTag).length).toBe(insideUSA.length + outsideUSA.length);
86         });
87     });
88 });