Skip to content

Commit

Permalink
Migrate unit tests to Jest
Browse files Browse the repository at this point in the history
  • Loading branch information
simon04 committed Nov 22, 2020
1 parent 0b871ec commit 5b64c34
Show file tree
Hide file tree
Showing 17 changed files with 5,914 additions and 2,882 deletions.
3 changes: 3 additions & 0 deletions jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"preset": "ts-jest"
}
8,379 changes: 5,648 additions & 2,731 deletions package-lock.json

Large diffs are not rendered by default.

13 changes: 3 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"build:demo-rollup": "cd demo-rollup && npm install && npm run build",
"build:demo-webpack": "cd demo-rollup && npm install && npm run build",
"changelog": "conventional-changelog --infile CHANGELOG.md --same-file --output-unreleased",
"test": "npm run test:karma -- --single-run && npm run lint",
"test:karma": "karma start spec/karma.conf.js",
"test": "jest",
"lint": "npm run lint:js && npm run lint:style",
"lint:js": "eslint .",
"lint:style": "prettier --check $(npm run ls-files)",
Expand Down Expand Up @@ -70,18 +69,12 @@
"cpr": "^3.0.1",
"eslint": "^6.8.0",
"eslint-plugin-prettier": "^3.1.2",
"karma": "^4.4.1",
"karma-expect": "^1.1.3",
"karma-mocha": "^1.3.0",
"karma-phantomjs-launcher": "^1.0.4",
"karma-sinon": "^1.0.5",
"jest": "^26.6.3",
"leaflet": "^1.6.0",
"mocha": "^6.2.2",
"phantomjs-prebuilt": "^2.1.16",
"prettier": "^1.19.1",
"rollup": "^2.6.1",
"rollup-plugin-terser": "^5.3.0",
"sinon": "^7.5.0",
"ts-jest": "^26.4.4",
"tslib": "^1.11.1",
"typescript": "^3.8.3"
},
Expand Down
7 changes: 0 additions & 7 deletions spec/.eslintrc.yaml

This file was deleted.

104 changes: 104 additions & 0 deletions spec/__snapshots__/nominatim.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`L.Control.Geocoder.Nominatim geocodes Innsbruck 1`] = `
Array [
Array [
Array [
Object {
"bbox": Object {
"_northEast": Object {
"lat": 47.2808566,
"lng": 11.418183,
},
"_southWest": Object {
"lat": 47.2583715,
"lng": 11.3811871,
},
},
"center": Object {
"lat": 47.26951525,
"lng": 11.3971372042211,
},
"html": "<span class=\\"\\"> Innsbruck </span><br/><span class=\\"leaflet-control-geocoder-address-context\\">Tyrol Austria</span>",
"icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png",
"name": "Innsbruck, Tyrol, Austria",
"properties": Object {
"address": Object {
"city": "Innsbruck",
"city_district": "Innsbruck",
"country": "Austria",
"country_code": "at",
"county": "Innsbruck",
"state": "Tyrol",
},
"boundingbox": Array [
47.2583715,
47.2808566,
11.3811871,
11.418183,
],
"class": "boundary",
"display_name": "Innsbruck, Tyrol, Austria",
"icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_boundary_administrative.p.20.png",
"importance": 0.763909048330467,
"lat": "47.26951525",
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"lon": "11.3971372042211",
"osm_id": 8182617,
"osm_type": "relation",
"place_id": 199282228,
"type": "administrative",
},
},
],
],
]
`;

exports[`L.Control.Geocoder.Nominatim reverse geocodes 47.3/11.3 1`] = `
Array [
Array [
Array [
Object {
"bounds": Object {
"_northEast": Object {
"lat": 47.2065094,
"lng": 11.3836945900354,
},
"_southWest": Object {
"lat": 47.2065094,
"lng": 11.3836945900354,
},
},
"center": Object {
"lat": 47.2065094,
"lng": 11.3836945900354,
},
"html": "<span class=\\"\\">Tyrol Austria</span>",
"name": "Innsbruck-Land, Tyrol, Austria",
"properties": Object {
"address": Object {
"country": "Austria",
"country_code": "at",
"county": "Innsbruck-Land",
"state": "Tyrol",
},
"boundingbox": Array [
"46.9624854",
"47.4499229",
"10.9896868",
"11.7051742",
],
"display_name": "Innsbruck-Land, Tyrol, Austria",
"lat": "47.2065094",
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"lon": "11.3836945900354",
"osm_id": 78251,
"osm_type": "relation",
"place_id": 197718025,
},
},
],
],
]
`;
46 changes: 46 additions & 0 deletions spec/__snapshots__/pelias.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`L.Control.Geocoder.Openrouteservice geocodes Innsbruck 1`] = `
Array [
Array [
Array [
Object {
"bbox": Object {
"_northEast": Object {
"lat": 47.29398,
"lng": 11.452584553,
},
"_southWest": Object {
"lat": 47.2470573997,
"lng": 11.3218091258,
},
},
"center": Object {
"lat": 47.272308,
"lng": 11.407851,
},
"name": "Innsbruck, Austria",
"properties": Object {
"accuracy": "centroid",
"confidence": 1,
"continent": "Europe",
"country": "Austria",
"country_a": "AUT",
"county": "Innsbruck",
"county_a": "IN",
"id": "101748061",
"label": "Innsbruck, Austria",
"layer": "locality",
"localadmin": "Innsbruck",
"locality": "Innsbruck",
"match_type": "exact",
"name": "Innsbruck",
"region": "Tirol",
"region_a": "TR",
"source_id": "101748061",
},
},
],
],
]
`;
6 changes: 0 additions & 6 deletions spec/control.spec.js

This file was deleted.

14 changes: 0 additions & 14 deletions spec/karma.conf.js

This file was deleted.

42 changes: 20 additions & 22 deletions spec/latlng.spec.js → spec/latlng.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
describe('L.Control.Geocoder.LatLng', function() {
import * as L from 'leaflet';
import { LatLng } from '../src/geocoders/latlng';

describe('LatLng', function() {
// test cases from https://github.com/openstreetmap/openstreetmap-website/blob/master/test/controllers/geocoder_controller_test.rb
var expected;
let expected;
beforeEach(function() {
expected = L.latLng(50.06773, 14.37742);
});
Expand All @@ -13,24 +16,20 @@ describe('L.Control.Geocoder.LatLng', function() {
});

it('does not geocode no-lat-lng', function() {
var geocoder = new L.Control.Geocoder.LatLng();
var callback = sinon.fake();
const geocoder = new LatLng();
const callback = jest.fn();
geocoder.geocode('no-lat-lng', callback);
expect(callback.calledOnce).to.not.be.ok();
expect(callback).toHaveBeenCalledTimes(0);
});

it('passes unsupported queries to the next geocoder', function() {
var next = {
geocode: function(_query, cb) {
cb('XXX');
}
const next = {
geocode: (_query, cb) => cb('XXX')
};
var geocoder = new L.Control.Geocoder.LatLng({ next: next });
var callback = sinon.fake();
const geocoder = new LatLng({ next: next });
const callback = jest.fn();
geocoder.geocode('no-lat-lng', callback);
expect(callback.calledOnce).to.be.ok();
expect(callback.lastArg).to.be.ok();
expect(callback.lastArg).to.eql('XXX');
expect(callback).toHaveBeenCalledWith('XXX');
});

it('geocodes lat/lon pairs using N/E with degrees', function() {
Expand Down Expand Up @@ -139,14 +138,13 @@ describe('L.Control.Geocoder.LatLng', function() {
});

function geocode(query) {
var geocoder = new L.Control.Geocoder.LatLng();
var callback = sinon.fake();
const geocoder = new LatLng();
const callback = jest.fn();
geocoder.geocode(query, callback);
expect(callback.calledOnce).to.be.ok();
expect(callback.lastArg).to.be.ok();
expect(callback.lastArg).to.have.length(1);
expect(callback.lastArg[0].name).to.eql(query);
expect(callback.lastArg[0].center.lat).to.be.within(expected.lat - 1e-6, expected.lat + 1e-6);
expect(callback.lastArg[0].center.lng).to.be.within(expected.lng - 1e-6, expected.lng + 1e-6);
expect(callback).toBeCalledTimes(1);
const feature = callback.mock.calls[0][0][0];
expect(feature.name).toBe(query);
expect(feature.center.lat).toBeCloseTo(expected.lat);
expect(feature.center.lng).toBeCloseTo(expected.lng);
}
});
20 changes: 20 additions & 0 deletions spec/mockXMLHttpRequest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function mockXMLHttpRequest<T>(response: T): XMLHttpRequest {
const xhrMock: Partial<XMLHttpRequest> = {
open: jest.fn(),
send: jest.fn(),
setRequestHeader: jest.fn(),
readyState: 4,
status: 200,
response
};
jest.spyOn(window, 'XMLHttpRequest').mockImplementation(() => xhrMock as XMLHttpRequest);
return xhrMock as XMLHttpRequest;
}

export function testXMLHttpRequest<T>(url: string, response: T, trigger: () => void) {
const xhrMock = mockXMLHttpRequest(response);
trigger();
expect(xhrMock.open).toBeCalledWith('GET', url, true);
expect(xhrMock.setRequestHeader).toBeCalledWith('Accept', 'application/json');
(xhrMock.onreadystatechange as EventListener)(new Event(''));
}
Loading

0 comments on commit 5b64c34

Please sign in to comment.