From 8a0ff7aa169c672c10cb7c4652404fc007343027 Mon Sep 17 00:00:00 2001 From: Carl Brugger Date: Fri, 1 Sep 2023 13:15:43 -0500 Subject: [PATCH] feat: tests --- package-lock.json | 2 +- package.json | 2 +- plugins/webhook-egress/package.json | 2 +- .../webhook-egress/src/webhook.egress.spec.ts | 98 +++++++++++++++++++ plugins/webhook-egress/src/webhook.egress.ts | 7 +- utils/testing/src/test.listener.ts | 1 + 6 files changed, 107 insertions(+), 5 deletions(-) create mode 100644 plugins/webhook-egress/src/webhook.egress.spec.ts diff --git a/package-lock.json b/package-lock.json index cd833a73c..1352ffb71 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,7 +16,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", diff --git a/package.json b/package.json index 15f65f784..89f60aacd 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/plugins/webhook-egress/package.json b/plugins/webhook-egress/package.json index f8e55d11f..a7ecb355d 100644 --- a/plugins/webhook-egress/package.json +++ b/plugins/webhook-egress/package.json @@ -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.", diff --git a/plugins/webhook-egress/src/webhook.egress.spec.ts b/plugins/webhook-egress/src/webhook.egress.spec.ts new file mode 100644 index 000000000..99f732a71 --- /dev/null +++ b/plugins/webhook-egress/src/webhook.egress.spec.ts @@ -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: 'john@doe.com', + notes: 'foobar', + }, + { + name: 'Jane Doe', + email: 'jane@doe.com', + 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.", + }) + }) +}) diff --git a/plugins/webhook-egress/src/webhook.egress.ts b/plugins/webhook-egress/src/webhook.egress.ts index 6561c1a4b..b8024f851 100644 --- a/plugins/webhook-egress/src/webhook.egress.ts +++ b/plugins/webhook-egress/src/webhook.egress.ts @@ -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: { diff --git a/utils/testing/src/test.listener.ts b/utils/testing/src/test.listener.ts index fbe023c71..cf3e554b9 100644 --- a/utils/testing/src/test.listener.ts +++ b/utils/testing/src/test.listener.ts @@ -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 {