-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(webhooks): add tests for webhooks and macros using them
- Loading branch information
1 parent
1ecf685
commit 2378672
Showing
3 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { parseResponse } from '../api' | ||
|
||
describe('parseResponse', () => { | ||
const responseMock = { | ||
headers: null, | ||
async json() { | ||
return { foo: 'bar' } | ||
}, | ||
async text() { | ||
return 'foo is bar' | ||
}, | ||
} as unknown as Response | ||
|
||
beforeAll(() => { | ||
// @ts-ignore | ||
responseMock.headers = new Map() | ||
}) | ||
|
||
test('reads Content-Type header to determine parse type', async () => { | ||
responseMock.headers.set('content-type', 'text/plain; charset=utf-8') | ||
const resultText = await parseResponse(responseMock) | ||
expect(resultText).toEqual(await responseMock.text()) | ||
|
||
responseMock.headers.set('content-type', 'application/json; charset=utf-8') | ||
const resultJSON = await parseResponse(responseMock) | ||
expect(resultJSON).toEqual(await responseMock.json()) | ||
}) | ||
|
||
test('defaults to text()', async () => { | ||
responseMock.headers.set( | ||
'content-type', | ||
'multipart/form-data; boundary=ExampleBoundaryString' | ||
) | ||
const resultText = await parseResponse(responseMock) | ||
expect(resultText).toEqual(await responseMock.text()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import type { ModelWithWorkflow } from '../../models/types' | ||
import { getAllWebhookConfigs, getWebhookConfig, invokeWebhook } from '../service' | ||
|
||
describe('Webhook Service', () => { | ||
const env = process.env | ||
|
||
beforeAll(() => { | ||
global.fetch = jest.fn(() => | ||
Promise.resolve({ | ||
headers: { | ||
get() { | ||
return 'text/plain; charset=utf-8' | ||
}, | ||
}, | ||
ok: true, | ||
json: () => Promise.resolve({ foo: 'bar' }), | ||
text: () => Promise.resolve('foo is bar'), | ||
}) | ||
) as jest.Mock | ||
}) | ||
|
||
beforeEach(() => { | ||
process.env = { ...env } | ||
}) | ||
|
||
afterEach(() => { | ||
process.env = { ...env } | ||
}) | ||
|
||
describe('getAllWebhookConfigs', () => { | ||
test('gets all webhook configs as ErrorData', () => { | ||
// NOTE: this line needs to be left as escaped JSON. | ||
// prettier-ignore | ||
process.env.UI_WEBHOOKS = | ||
"[{\"token\":\"an-example-token-for-endpoint-1\",\"URL\":\"http://example.com/1\"}, {\"token\":\"an-example-token-for-endpoint-2\",\"URL\":\"http://example.com/2\"}]" | ||
|
||
const [error, data] = getAllWebhookConfigs() | ||
|
||
expect(error).toBeNull() | ||
expect(data).toMatchInlineSnapshot(` | ||
Array [ | ||
Object { | ||
"URL": "http://example.com/1", | ||
"token": "an-example-token-for-endpoint-1", | ||
}, | ||
Object { | ||
"URL": "http://example.com/2", | ||
"token": "an-example-token-for-endpoint-2", | ||
}, | ||
] | ||
`) | ||
}) | ||
}) | ||
|
||
describe('getWebhookConfig', () => { | ||
test('gets webhook config, matching by URL', () => { | ||
// NOTE: this line needs to be left as escaped JSON. | ||
// prettier-ignore | ||
process.env.UI_WEBHOOKS = | ||
"[{\"token\":\"an-example-token-for-endpoint-1\",\"URL\":\"http://example.com/1\"}, {\"token\":\"an-example-token-for-endpoint-2\",\"URL\":\"http://example.com/2\"}]" | ||
|
||
const [error, data] = getWebhookConfig('http://example.com/2') | ||
|
||
expect(error).toBeNull() | ||
expect(data).toMatchInlineSnapshot(` | ||
Object { | ||
"URL": "http://example.com/2", | ||
"token": "an-example-token-for-endpoint-2", | ||
} | ||
`) | ||
}) | ||
}) | ||
|
||
describe('invokeWebhook', () => { | ||
test('invokes (fetches) a webhook URL', async () => { | ||
const fetchSpy = jest.spyOn(global, 'fetch') | ||
// NOTE: this line needs to be left as escaped JSON. | ||
// prettier-ignore | ||
process.env.UI_WEBHOOKS = | ||
"[{\"token\":\"an-example-token-for-endpoint-1\",\"URL\":\"http://example.com/1\"}, {\"token\":\"an-example-token-for-endpoint-2\",\"URL\":\"http://example.com/2\"}]" | ||
|
||
const [configError, webhook] = getWebhookConfig('http://example.com/2') | ||
const [error, result] = await invokeWebhook(webhook, { | ||
model: {} as ModelWithWorkflow, | ||
document: {}, | ||
state: 'Draft', | ||
}) | ||
|
||
expect(configError).toBeNull() | ||
expect(error).toBeNull() | ||
expect(result).toMatchInlineSnapshot(`"foo is bar"`) | ||
expect(fetchSpy).toHaveBeenCalledTimes(1) | ||
}) | ||
}) | ||
}) |