Skip to content

Commit

Permalink
Add proper testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Twixes committed Mar 26, 2021
1 parent 84d0f82 commit 6127057
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 43 deletions.
Binary file added GeoLite2-City-Test.mmdb
Binary file not shown.
73 changes: 31 additions & 42 deletions index.test.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,35 @@
import { Plugin, PluginMeta } from '@posthog/plugin-scaffold'
// @ts-ignore
import { createPageview, clone } from '@posthog/plugin-scaffold/test/utils'
import { createPageview, resetMeta } from '@posthog/plugin-scaffold/test/utils'
import { join } from 'path'
import { Reader } from '@maxmind/geoip2-node'
import * as index from '.'

// import { processEvent } from '..'
const { processEvent } = index as Required<Plugin>

// test('setupPlugin and processEvent with no DB', async () => {
// const pageviewEvent = createPageview()
// const processedPageviewEvent = await processEvent(clone(pageviewEvent))
// expect(processedPageviewEvent).toEqual(pageviewEvent)
// })
async function resetMetaWithMmdb(file = 'GeoLite2-City-Test.mmdb'): Promise<PluginMeta> {
const mmdb = await Reader.open(join(__dirname, file))
return resetMeta({
geoip: {
locate: (ipAddress: string) => mmdb.city(ipAddress),
},
}) as PluginMeta
}

// test('GeoLite2-City', async () => {
// const event = await processEvent({ ...createPageview(), ip: '89.160.20.129' })
// expect(event!.properties).toEqual(
// expect.objectContaining({
// $city_name: 'Linköping',
// $country_name: 'Sweden',
// $country_code: 'SE',
// $continent_name: 'Europe',
// $continent_code: 'EU',
// $latitude: 58.4167,
// $longitude: 15.6167,
// $time_zone: 'Europe/Stockholm',
// $subdivision_1_code: 'E',
// $subdivision_1_name: 'Östergötland County',
// })
// )
// })

// test('GeoIP2-City', async () => {
// const event = await processEvent({ ...createPageview(), ip: '89.160.20.129' })
// expect(event!.properties).toEqual(
// expect.objectContaining({
// $city_name: 'Linköping',
// $country_name: 'Sweden',
// $country_code: 'SE',
// $continent_name: 'Europe',
// $continent_code: 'EU',
// $latitude: 58.4167,
// $longitude: 15.6167,
// $time_zone: 'Europe/Stockholm',
// $subdivision_1_code: 'E',
// $subdivision_1_name: 'Östergötland County',
// })
// )
// })
test('GeoLite2-City', async () => {
const event = await processEvent({ ...createPageview(), ip: '89.160.20.129' }, await resetMetaWithMmdb())
expect(event!.properties).toEqual(
expect.objectContaining({
$city_name: 'Linköping',
$country_name: 'Sweden',
$country_code: 'SE',
$continent_name: 'Europe',
$continent_code: 'EU',
$latitude: 58.4167,
$longitude: 15.6167,
$time_zone: 'Europe/Stockholm',
$subdivision_1_code: 'E',
$subdivision_1_name: 'Östergötland County',
})
)
})
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"homepage": "https://github.com/PostHog/posthog-plugin-geoip#readme",
"author": "PostHog <[email protected]>",
"scripts": {
"test": "jest src",
"test": "jest .",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"format": "prettier --write .",
Expand All @@ -27,6 +27,7 @@
},
"dependencies": {},
"devDependencies": {
"@maxmind/geoip2-node": "^2.3.1",
"@posthog/plugin-scaffold": "0.4.2",
"@types/jest": "^26.0.19",
"@typescript-eslint/eslint-plugin": "^4.12.0",
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noUnusedParameters": true,
"esModuleInterop": true,
"noEmit": true,
"resolveJsonModule": true
},
Expand Down

0 comments on commit 6127057

Please sign in to comment.