Skip to content

Commit

Permalink
test: massively reduce LoC and complexity by simplifying approach to …
Browse files Browse the repository at this point in the history
…project/unproject in test files (#356)
  • Loading branch information
JamesLMilner authored Oct 21, 2024
1 parent b99297c commit b40f970
Show file tree
Hide file tree
Showing 49 changed files with 1,301 additions and 4,826 deletions.
10 changes: 5 additions & 5 deletions src/adapters/arcgis-maps-sdk.adapter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { TerraDrawArcGISMapsSDKAdapter } from "./arcgis-maps-sdk.adapter";
import MapView from "@arcgis/core/views/MapView.js";
import { getMockPointerEvent } from "../test/mock-pointer-event";
import { MockPointerEvent } from "../test/mock-pointer-event";
import Point from "@arcgis/core/geometry/Point";
import { TerraDrawAdapterStyling } from "../common";
import Color from "@arcgis/core/Color";
import MapViewScreenPoint = __esri.MapViewScreenPoint;
import { createMockCallbacks } from "../test/mock-callbacks";
import { MockCallbacks } from "../test/mock-callbacks";

jest.mock("@arcgis/core/views/MapView", () => jest.fn());
jest.mock("@arcgis/core/geometry/Point");
Expand Down Expand Up @@ -89,7 +89,7 @@ describe("TerraDrawArcGISMapsSDKAdapter", () => {

expect(adapter).toBeDefined();

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

expect(mockMapView.on).toHaveBeenCalledTimes(2);
expect(mockMapView.on).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -118,7 +118,7 @@ describe("TerraDrawArcGISMapsSDKAdapter", () => {

expect(adapter).toBeDefined();

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());
adapter.unregister();
expect(removeAll).toHaveBeenCalledTimes(1);
expect(remove).toHaveBeenCalledTimes(2);
Expand All @@ -142,7 +142,7 @@ describe("TerraDrawArcGISMapsSDKAdapter", () => {
longitude: -0.1275,
})) as unknown as (point: MapViewScreenPoint | MouseEvent) => Point;

const result = adapter.getLngLatFromEvent(getMockPointerEvent());
const result = adapter.getLngLatFromEvent(MockPointerEvent());
expect(result).toEqual({ lat: 51.507222, lng: -0.1275 });
});
});
Expand Down
110 changes: 47 additions & 63 deletions src/adapters/google-maps.adapter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
*/
import { TerraDrawAdapterStyling } from "../common";
import { GeoJSONStoreFeatures } from "../store/store";
import { createMockCallbacks } from "../test/mock-callbacks";
import { MockCallbacks } from "../test/mock-callbacks";
import {
createMockLineString,
createMockPoint,
createMockPolygonSquare,
MockLineString,
MockPoint,
MockPolygonSquare,
} from "../test/mock-features";
import { getMockPointerEvent } from "../test/mock-pointer-event";
import { MockPointerEvent } from "../test/mock-pointer-event";
import { TerraDrawGoogleMapsAdapter } from "./google-maps.adapter";

const createMockGoogleMap = (overrides?: Partial<google.maps.Map>) => {
Expand Down Expand Up @@ -109,7 +109,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {
minPixelDragDistanceDrawing: 8,
coordinatePrecision: 9,
});
}).toThrowError();
}).toThrow();
});
});

Expand Down Expand Up @@ -138,7 +138,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mockMap,
});

const callbackMock = createMockCallbacks();
const callbackMock = MockCallbacks();
adapter.register(callbackMock);

expect(div.addEventListener).toHaveBeenCalledTimes(6);
Expand Down Expand Up @@ -209,7 +209,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mockMap,
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

expect(removeListenerMock).not.toHaveBeenCalled();

Expand Down Expand Up @@ -237,9 +237,9 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mapMock,
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

expect(adapter.getLngLatFromEvent(getMockPointerEvent())).toBeNull();
expect(adapter.getLngLatFromEvent(MockPointerEvent())).toBeNull();
expect(mapMock.getBounds).toHaveBeenCalled();
});

Expand Down Expand Up @@ -278,9 +278,9 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mapMock,
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

expect(adapter.getLngLatFromEvent(getMockPointerEvent())).toBeNull();
expect(adapter.getLngLatFromEvent(MockPointerEvent())).toBeNull();
expect(getProjectionMock).toHaveBeenCalled();
});

Expand Down Expand Up @@ -327,9 +327,9 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mapMock,
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

const lngLatFromEvent = adapter.getLngLatFromEvent(getMockPointerEvent());
const lngLatFromEvent = adapter.getLngLatFromEvent(MockPointerEvent());
expect(lngLatFromEvent?.lng).toEqual(testLng);
expect(lngLatFromEvent?.lat).toEqual(testLat);
});
Expand All @@ -354,7 +354,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {

expect(() => {
adapter.project(-1, -2);
}).toThrowError("cannot get overlay");
}).toThrow("cannot get overlay");

expect(mapMock.getBounds).not.toHaveBeenCalled();
});
Expand All @@ -375,11 +375,11 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mapMock,
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

expect(() => {
adapter.project(-1, -2);
}).toThrowError("cannot get bounds");
}).toThrow("cannot get bounds");

expect(mapMock.getBounds).toHaveBeenCalled();
});
Expand All @@ -402,11 +402,11 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mapMock,
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

expect(() => {
adapter.project(testLng, testLat);
}).toThrowError();
}).toThrow();

expect(getProjectionMock).toHaveBeenCalledTimes(1);
});
Expand All @@ -432,11 +432,11 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mapMock,
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

expect(() => {
adapter.project(testLng, testLat);
}).toThrowError();
}).toThrow();

expect(getProjectionMock).toHaveBeenCalledTimes(1);
expect(fromLatLngToContainerPixelMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -465,7 +465,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: mapMock,
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

const projected = adapter.project(testLng, testLat);

Expand Down Expand Up @@ -493,7 +493,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {

expect(() => {
adapter.unproject(50, 80);
}).toThrowError();
}).toThrow();

expect(getProjectionMock).not.toHaveBeenCalled();
});
Expand All @@ -512,11 +512,11 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: createMockGoogleMap(),
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

expect(() => {
adapter.unproject(-1, -2);
}).toThrowError();
}).toThrow();

expect(getProjectionMock).toHaveBeenCalled();
});
Expand All @@ -541,7 +541,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {
map: createMockGoogleMap(),
});

adapter.register(createMockCallbacks());
adapter.register(MockCallbacks());

const unprojected = adapter.unproject(50, 80);
expect(getProjectionMock).toHaveBeenCalledTimes(1);
Expand Down Expand Up @@ -660,7 +660,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {

describe("render", () => {
it("rejects updates to invalid features", () => {
const p1 = createMockPoint("point-1") as GeoJSONStoreFeatures;
const p1 = MockPoint("point-1") as GeoJSONStoreFeatures;
const mockMap = createMockGoogleMap({
data: {
addListener: () => {},
Expand Down Expand Up @@ -690,7 +690,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {
mockStyleDraw,
);

const p2 = createMockPoint("point-2") as GeoJSONStoreFeatures;
const p2 = MockPoint("point-2") as GeoJSONStoreFeatures;
p2.id = undefined;

expect(() => {
Expand All @@ -703,11 +703,11 @@ describe("TerraDrawGoogleMapsAdapter", () => {
},
mockStyleDraw,
);
}).toThrowError();
}).toThrow();
});

it("rejects updates to unrecognized/out-of-sync features", () => {
const p1 = createMockPoint("point-1") as GeoJSONStoreFeatures;
const p1 = MockPoint("point-1") as GeoJSONStoreFeatures;
Object.assign(p1, { forEachProperty: () => {} });
Object.assign(p1, { setProperty: () => {} });
Object.assign(p1, { setGeometry: jest.fn() });
Expand Down Expand Up @@ -748,7 +748,7 @@ describe("TerraDrawGoogleMapsAdapter", () => {
);

// Not present in map copy of state, will cause to throw
const p2 = createMockPoint("point-2") as GeoJSONStoreFeatures;
const p2 = MockPoint("point-2") as GeoJSONStoreFeatures;

expect(() => {
adapter.render(
Expand All @@ -760,21 +760,21 @@ describe("TerraDrawGoogleMapsAdapter", () => {
},
mockStyleDraw,
);
}).toThrowError();
}).toThrow();

expect(getFeatureByIdMock).toHaveBeenCalledTimes(2);
});

describe("Point", () => {
it("adds and deletes features", () => {
const p1 = createMockPoint("point-1") as GeoJSONStoreFeatures;
const p2 = createMockPoint("point-2", 2, 4) as GeoJSONStoreFeatures;
const p1 = MockPoint("point-1") as GeoJSONStoreFeatures;
const p2 = MockPoint("point-2", 2, 4) as GeoJSONStoreFeatures;

verifyAddingAndDeletingFeature(p1, p2);
});

it("applies styles to added feature", () => {
const feature = createMockPoint("point-1") as GeoJSONStoreFeatures;
const feature = MockPoint("point-1") as GeoJSONStoreFeatures;
Object.assign(feature, {
getGeometry: jest.fn(() => ({
getType: () => "Point",
Expand Down Expand Up @@ -802,32 +802,26 @@ describe("TerraDrawGoogleMapsAdapter", () => {
});

it("adds features on successive renders", () => {
const p1 = createMockPoint("point-1") as GeoJSONStoreFeatures;
const p1 = MockPoint("point-1") as GeoJSONStoreFeatures;
verifyAddingFeatureSecondRender(p1);
});

it("updates features on successive renders", () => {
const p1 = createMockPoint("point-1") as GeoJSONStoreFeatures;
const p1 = MockPoint("point-1") as GeoJSONStoreFeatures;
verifyUpdatesFeature(p1);
});
});

describe("LineString", () => {
it("adds and deletes features", () => {
const p1 = createMockLineString(
"line-string-1",
) as GeoJSONStoreFeatures;
const p2 = createMockLineString(
"line-string-2",
) as GeoJSONStoreFeatures;
const p1 = MockLineString("line-string-1") as GeoJSONStoreFeatures;
const p2 = MockLineString("line-string-2") as GeoJSONStoreFeatures;

verifyAddingAndDeletingFeature(p1, p2);
});

it("applies styles to added feature", () => {
const feature = createMockLineString(
"line-string-1",
) as GeoJSONStoreFeatures;
const feature = MockLineString("line-string-1") as GeoJSONStoreFeatures;
Object.assign(feature, {
getGeometry: jest.fn(() => ({
getType: () => "LineString",
Expand All @@ -851,36 +845,26 @@ describe("TerraDrawGoogleMapsAdapter", () => {
});

it("adds features on successive renders", () => {
const p1 = createMockLineString(
"line-string-1",
) as GeoJSONStoreFeatures;
const p1 = MockLineString("line-string-1") as GeoJSONStoreFeatures;
verifyAddingFeatureSecondRender(p1);
});

it("updates features on successive renders", () => {
const p1 = createMockLineString(
"line-string-1",
) as GeoJSONStoreFeatures;
const p1 = MockLineString("line-string-1") as GeoJSONStoreFeatures;
verifyUpdatesFeature(p1);
});
});

describe("Polygon", () => {
it("adds and deletes features", () => {
const sq1 = createMockPolygonSquare("square-1") as GeoJSONStoreFeatures;
const sq2 = createMockPolygonSquare(
"square-2",
2,
4,
) as GeoJSONStoreFeatures;
const sq1 = MockPolygonSquare("square-1") as GeoJSONStoreFeatures;
const sq2 = MockPolygonSquare("square-2", 2, 4) as GeoJSONStoreFeatures;

verifyAddingAndDeletingFeature(sq1, sq2);
});

it("applies styles to added feature", () => {
const feature = createMockPolygonSquare(
"square-1",
) as GeoJSONStoreFeatures;
const feature = MockPolygonSquare("square-1") as GeoJSONStoreFeatures;
Object.assign(feature, {
getGeometry: jest.fn(() => ({
getType: () => "Polygon",
Expand All @@ -906,12 +890,12 @@ describe("TerraDrawGoogleMapsAdapter", () => {
});

it("adds features on successive renders", () => {
const sq1 = createMockPolygonSquare("square-1") as GeoJSONStoreFeatures;
const sq1 = MockPolygonSquare("square-1") as GeoJSONStoreFeatures;
verifyAddingFeatureSecondRender(sq1);
});

it("updates features on successive renders", () => {
const sq1 = createMockPolygonSquare("square-1") as GeoJSONStoreFeatures;
const sq1 = MockPolygonSquare("square-1") as GeoJSONStoreFeatures;
verifyUpdatesFeature(sq1);
});
});
Expand Down
Loading

0 comments on commit b40f970

Please sign in to comment.