diff --git a/jest.config.js b/jest.config.js index 4ae93fc..c7bc1bf 100644 --- a/jest.config.js +++ b/jest.config.js @@ -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", diff --git a/package-lock.json b/package-lock.json index d245a0b..0811a77 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,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", @@ -1740,6 +1741,16 @@ "integrity": "sha512-EhcH/wvidPy1WeML3TtYFGR83UzjxeWRen9V402T8aUGYsCHOmfoisV3ZSg03gAFIbLq8TnWOJ0f4cALtnSEUg==", "dev": true }, + "node_modules/@types/node-fetch": { + "version": "2.6.11", + "resolved": "https://registry.npmjs.org/@types/node-fetch/-/node-fetch-2.6.11.tgz", + "integrity": "sha512-24xFj9R5+rfQJLRyM56qh+wnVSYhyXC2tkoBndtY0U+vubqNsYXGjufB2nn8Q6gt0LrARwL6UBtMCSVCwl4B1g==", + "dev": true, + "dependencies": { + "@types/node": "*", + "form-data": "^4.0.0" + } + }, "node_modules/@types/stack-utils": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/@types/stack-utils/-/stack-utils-2.0.3.tgz", @@ -2049,6 +2060,12 @@ "retry": "0.12.0" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true + }, "node_modules/autoprefixer": { "version": "10.4.14", "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.14.tgz", @@ -2874,6 +2891,18 @@ "integrity": "sha512-jeC1axXpnb0/2nn/Y1LPuLdgXBLH7aDcHu4KEKfqw3CUhX7ZpfBSlPKyqXE6btIgEzfWtrX3/tyBCaCvXvMkOw==", "dev": true }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/commander": { "version": "2.20.3", "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", @@ -3395,6 +3424,15 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true, + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/depd": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/depd/-/depd-2.0.0.tgz", @@ -4310,6 +4348,20 @@ "url": "https://github.com/sponsors/isaacs" } }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dev": true, + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/formats": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/formats/-/formats-1.0.0.tgz", diff --git a/package.json b/package.json index 46e72fe..fd64a49 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -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", diff --git a/src/js/filters.js b/src/js/filters.js index 61a4117..898fd4e 100644 --- a/src/js/filters.js +++ b/src/js/filters.js @@ -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) { diff --git a/tests/js/filters.spec.js b/tests/js/filters.spec.js deleted file mode 100644 index 1277e52..0000000 --- a/tests/js/filters.spec.js +++ /dev/null @@ -1,11 +0,0 @@ - - -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]") - }); -}) \ No newline at end of file diff --git a/tests/js/filters.spec.ts b/tests/js/filters.spec.ts new file mode 100644 index 0000000..e9a4e0b --- /dev/null +++ b/tests/js/filters.spec.ts @@ -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}') + }); +}) \ No newline at end of file diff --git a/tests/types/Geoapify-interface.ts b/tests/types/Geoapify-interface.ts new file mode 100644 index 0000000..dcdd588 --- /dev/null +++ b/tests/types/Geoapify-interface.ts @@ -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 + } + \ No newline at end of file