Skip to content

Commit

Permalink
feat: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Brugger committed Sep 1, 2023
1 parent b334efe commit 8a0ff7a
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"devDependencies": {
"@changesets/cli": "^2.26.1",
"@flatfile/listener": "^0.3.15",
"@flatfile/utils-testing": "^0.0.1",
"@flatfile/utils-testing": "^0.0.2",
"@parcel/packager-ts": "^2.9.1",
"@parcel/transformer-typescript-types": "^2.9.1",
"@types/jest": "^29.5.0",
Expand Down
2 changes: 1 addition & 1 deletion plugins/webhook-egress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"build": "parcel build",
"dev": "parcel watch",
"check": "tsc ./**/*.ts --noEmit --esModuleInterop",
"test": "jest --passWithNoTests"
"test": "jest ./**/*.spec.ts --config=../../jest.config.js --runInBand"
},
"keywords": [],
"author": "Flatfile, Inc.",
Expand Down
98 changes: 98 additions & 0 deletions plugins/webhook-egress/src/webhook.egress.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import api from '@flatfile/api'
import {
createRecords,
deleteSpace,
setupListener,
setupSimpleWorkbook,
setupSpace,
} from '@flatfile/utils-testing'
import { webhookEgress } from './webhook.egress'
import { FlatfileEvent } from '@flatfile/listener'

jest.setTimeout(10_000)

describe('webhookEgress() e2e', () => {
const listener = setupListener()

let spaceId
let workbookId
let sheetId

beforeAll(async () => {
const space = await setupSpace()
spaceId = space.id
const workbook = await setupSimpleWorkbook(space.id, [
'name',
'email',
'notes',
])
workbookId = workbook.id
sheetId = workbook.sheets[0].id
await createRecords(sheetId, [
{
name: 'John Doe',
email: '[email protected]',
notes: 'foobar',
},
{
name: 'Jane Doe',
email: '[email protected]',
notes: 'foobar',
},
])
})

afterAll(async () => {
await deleteSpace(spaceId)
})

it('returns successful outcome message', async () => {
const operationSuccess = 'egressTestSuccess'
const webhookUrl =
'https://webhook.site/4eb39ab6-87d7-4087-8a49-08dcb547252b'
listener.use(webhookEgress(operationSuccess, webhookUrl))

const { data: successfulJob } = await api.jobs.create({
type: 'workbook',
operation: operationSuccess,
source: workbookId,
})
const successfulJobId = successfulJob.id
await api.jobs.execute(successfulJobId)

await listener.waitFor('job:ready', 1, `workbook:${operationSuccess}`)

const response = await api.jobs.get(successfulJobId)
expect(response.data.outcome.message).toEqual(
`Data was successfully submitted to the provided webhook. Go check it out at ${webhookUrl}.`
)
})

it('returns failure outcome message', async () => {
const logErrorSpy = jest.spyOn(global.console, 'error')

const operationFailure = 'egressTestFailure'
const invalidWebhookUrl = 'https://webhook.site'
listener.use(webhookEgress(operationFailure, invalidWebhookUrl))

const { data: failedJob } = await api.jobs.create({
type: 'workbook',
operation: operationFailure,
source: workbookId,
})
const failedJobId = failedJob.id
await api.jobs.execute(failedJobId)

await listener.waitFor('job:ready', 1, `workbook:${operationFailure}`)

expect(logErrorSpy).toHaveBeenCalledWith(
'[@flatfile/plugin-webhook-egress]:[FATAL] {}'
)

const response = await api.jobs.get(failedJobId)
expect(response.data.outcome).toEqual({
message:
"This job failed probably because it couldn't find the webhook URL.",
})
})
})
7 changes: 5 additions & 2 deletions plugins/webhook-egress/src/webhook.egress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ export function webhookEgress(job: string, webhookUrl?: string) {
}
} else {
logError(
'@flatfile/webhook-egress',
'@flatfile/plugin-webhook-egress',
`Failed to submit data to ${webhookReceiver}. Status: ${response.status} ${response.statusText}`
)
}
} catch (error) {
logError('@flatfile/webhook-egress', JSON.stringify(error, null, 2))
logError(
'@flatfile/plugin-webhook-egress',
JSON.stringify(error, null, 2)
)

return {
outcome: {
Expand Down
1 change: 1 addition & 0 deletions utils/testing/src/test.listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export class TestListener extends FlatfileListener {
*
* @param event The event to wait for
* @param count The count of the event
* @param job The job to wait for
* @returns A promise that resolves when the count of the event has been reached
*/
waitFor(event: string, count: number = 1, job?: string): Promise<number> {
Expand Down

0 comments on commit 8a0ff7a

Please sign in to comment.