Skip to content

Commit

Permalink
Merge pull request #25 from snohio/lastlink/tests
Browse files Browse the repository at this point in the history
mock geo api and fix jest deprecations
  • Loading branch information
lastlink authored Mar 25, 2024
2 parents 08177c0 + 6127e87 commit bb32713
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 20 deletions.
11 changes: 5 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module.exports = {
globals: {
"ts-jest": {
tsConfig: "tsconfig.json"
}
},
moduleFileExtensions: [
"ts",
"js"
],
transform: {
"^.+\\.(ts|tsx)$": "ts-jest"
"^.+\\.(ts|tsx)$": ['ts-jest', {
babel: true,
tsconfig: 'tsconfig.json',
}]
},
testMatch: [
"**/tests/**/*.spec.(ts|js)"
],
preset: "ts-jest",
// turn off console.log
silent: true,
runner: "jest-serial-runner",
Expand Down
52 changes: 52 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"css": "postcss src/static/css/tailwind.css --o _site/static/css/style.css --watch",
"build": "cross-env NODE_ENV=production eleventy && cross-env NODE_ENV=production postcss src/static/css/tailwind.css --o _site/static/css/style.css",
"browsersync": "browser-sync start --server \"_site\" --files \"_site\" --port 8080 --no-notify --no-open",
"test": "npm run build-interfaces && jest -c ./jest.config.js --forceExit --verbose -i --no-cache",
"test:coverage": "npm run build-interfaces && jest --forceExit --coverage --verbose",
"test": "npm run build-interfaces && jest -c ./jest.config.js --detectOpenHandles --forceExit --verbose -i --no-cache",
"test:coverage": "npm run build-interfaces && jest --detectOpenHandles --forceExit --coverage --verbose",
"test:watch": "npm run build-interfaces && jest --watchAll",
"build-interfaces": "npx ts-interface-builder src/**/*interface.ts tests/**/*interface.ts"
},
Expand All @@ -17,6 +17,7 @@
"@11ty/eleventy-plugin-webc": "^0.11.2",
"@tailwindcss/typography": "^0.5.1",
"@types/jest": "^29.5.12",
"@types/node-fetch": "^2.6.11",
"alpinejs": "^3.12.0",
"autoprefixer": "^10.4.2",
"browser-sync": "^2.27.7",
Expand Down
2 changes: 1 addition & 1 deletion src/js/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function geoEncode(addressString) {
return feature.properties.rank.confidence >= 0.9
})
.filter((feature) => {
return feature.properties.postcode === result.query.parsed.postcode;
return feature.properties.postcode.toLowerCase() === result.query.parsed.postcode;
})

switch (matches.length) {
Expand Down
11 changes: 0 additions & 11 deletions tests/js/filters.spec.js

This file was deleted.

99 changes: 99 additions & 0 deletions tests/js/filters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@

jest.mock('node-fetch');
import fetch, {Response} from 'node-fetch';
import { GeocodeResponse } from '../types/Geoapify-interface';
import { Mock } from 'jest-mock';

const filters = require('../../src/js/filters');

describe('filters', function() {
it('no address return default', async () => {
const r = await filters.geoApify('');
expect(r).toEqual("[0,0]")
});

it('test mock address', async () => {
const mockResponse:GeocodeResponse = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"datasource": {
"sourcename": "openstreetmap",
"attribution": "© OpenStreetMap contributors",
"license": "Open Database License",
"url": "https://www.openstreetmap.org/copyright"
},
"country": "United Kingdom",
"country_code": "gb",
"state": "England",
"county": "Greater London",
"city": "London",
"postcode": "W1H 1LJ",
"suburb": "Marylebone",
"street": "Upper Montagu Street",
"housenumber": "38",
"lon": -0.16030636023550826,
"lat": 51.52016005,
"state_code": "ENG",
"result_type": "building",
"formatted": "38 Upper Montagu Street, London, W1H 1LJ, United Kingdom",
"address_line1": "38 Upper Montagu Street",
"address_line2": "London, W1H 1LJ, United Kingdom",
"category": "building.residential",
"timezone": {
"name": "Europe/London",
"offset_STD": "+00:00",
"offset_STD_seconds": 0,
"offset_DST": "+01:00",
"offset_DST_seconds": 3600,
"abbreviation_STD": "GMT",
"abbreviation_DST": "BST"
},
"plus_code": "9C3XGRCQ+3V",
"plus_code_short": "GRCQ+3V London, Greater London, United Kingdom",
"rank": {
"importance": 0.41000999999999993,
"popularity": 8.988490181891963,
"confidence": 0.95,
"confidence_city_level": 1,
"confidence_street_level": 1,
"match_type": "full_match"
},
"place_id": "51dcb14637eb84c4bf59c6b7c19a94c24940f00102f901370cef1100000000c00203"
},
"geometry": {
"type": "Point",
"coordinates": [
-0.16030636023550826,
51.52016005
]
},
"bbox": [
-0.160394,
51.5201061,
-0.1602251,
51.5202273
]
}
],
"query": {
"text": "38 Upper Montagu Street, Westminster W1H 1LJ, United Kingdom",
"parsed": {
"housenumber": "38",
"street": "upper montagu street",
"postcode": "w1h 1lj",
"district": "westminster",
"country": "united kingdom",
"expected_type": "building"
}
}
};
(fetch as unknown as Mock).mockReturnValue(Promise.resolve({ json: () => Promise.resolve(mockResponse),
}));
const r = await filters.geoApify('123 Main Street');
await jest.runAllTimersAsync();
expect(r).toEqual('{"lat":51.52016005,"lon":-0.16030636023550826}')
});
})
84 changes: 84 additions & 0 deletions tests/types/Geoapify-interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
export interface GeocodeResponse {
type: string
features: Feature[]
query: Query
}

export interface Feature {
type: string
properties: Properties
geometry: Geometry
bbox: number[]
}

export interface Properties {
datasource: Datasource
country: string
country_code: string
state: string
county: string
city: string
postcode: string
suburb: string
street: string
housenumber: string
lon: number
lat: number
state_code: string
result_type: string
formatted: string
address_line1: string
address_line2: string
category: string
timezone: Timezone
plus_code: string
plus_code_short: string
rank: Rank
place_id: string
}

export interface Datasource {
sourcename: string
attribution: string
license: string
url: string
}

export interface Timezone {
name: string
offset_STD: string
offset_STD_seconds: number
offset_DST: string
offset_DST_seconds: number
abbreviation_STD: string
abbreviation_DST: string
}

export interface Rank {
importance: number
popularity: number
confidence: number
confidence_city_level: number
confidence_street_level: number
match_type: string
}

export interface Geometry {
type: string
coordinates: number[]
}

export interface Query {
text: string
parsed: Parsed
}

export interface Parsed {
housenumber: string
street: string
postcode: string
district: string
country: string
expected_type: string
}

0 comments on commit bb32713

Please sign in to comment.