0
0
mirror of https://github.com/PostHog/posthog.git synced 2024-11-22 08:40:03 +01:00
posthog/jest.setup.fetch-mock.js

28 lines
1.2 KiB
JavaScript
Raw Normal View History

const { readFileSync } = require('fs')
const { DateTime } = require('luxon')
const { join } = require('path')
2020-11-30 16:57:02 +01:00
jest.mock('node-fetch', () => {
const responsesToUrls = {
'https://google.com/results.json?query=fetched': { count: 2, query: 'bla', results: [true, true] },
'https://mmdb.posthog.net/': readFileSync(join(__dirname, 'tests', 'assets', 'GeoLite2-City-Test.mmdb.br')),
}
const headersToUrls = {
'https://mmdb.posthog.net/': new Map([
['content-type', 'vnd.maxmind.maxmind-db'],
['content-disposition', `attachment; filename="GeoLite2-City-${DateTime.local().toISODate()}.mmdb"`],
]),
2020-11-30 16:57:02 +01:00
}
return jest.fn(
(url) =>
new Promise((resolve) =>
resolve({
buffer: () => new Promise((resolve) => resolve(responsesToUrls[url]) || Buffer.from('fetchmock')),
json: () => new Promise((resolve) => resolve(responsesToUrls[url]) || { fetch: 'mock' }),
text: () => new Promise((resolve) => resolve(JSON.stringify(responsesToUrls[url])) || 'fetchmock'),
headers: headersToUrls[url],
2020-11-30 16:57:02 +01:00
})
)
)
})