From 8f5f5ed48a05cd2b24d121925fc81bb35033b1b3 Mon Sep 17 00:00:00 2001 From: Nick Aguilar Date: Thu, 18 Jul 2024 15:04:14 -0700 Subject: [PATCH] WIP on an inexplicably not passing unit test --- .../__tests__/fb-operations.test.ts | 32 +++++++++++++++++++ .../fbca-operations.ts | 11 +++---- .../sync/__tests__/index.test.ts | 2 +- 3 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 packages/destination-actions/src/destinations/facebook-custom-audiences/__tests__/fb-operations.test.ts diff --git a/packages/destination-actions/src/destinations/facebook-custom-audiences/__tests__/fb-operations.test.ts b/packages/destination-actions/src/destinations/facebook-custom-audiences/__tests__/fb-operations.test.ts new file mode 100644 index 0000000000..bb1d185d22 --- /dev/null +++ b/packages/destination-actions/src/destinations/facebook-custom-audiences/__tests__/fb-operations.test.ts @@ -0,0 +1,32 @@ +import createRequestClient from '../../../../../core/src/request-client' +import FacebookClient, { BASE_URL } from '../fbca-operations' +import { Settings } from '../generated-types' +import nock from 'nock' + +const requestClient = createRequestClient() +const settings: Settings = { + adAccountId: 'act_123456' +} + +describe('Facebook Custom Audiences', () => { + describe('retlOnMappingSave hook', () => { + const facebookClient = new FacebookClient(requestClient, settings.adAccountId) + const hookInputs = { + audienceName: 'test-audience' + } + + it('should create a custom audience in facebook', async () => { + nock(`${BASE_URL}`) + .post(`/${settings.adAccountId}/customaudiences`, { + name: hookInputs.audienceName, + subtype: 'CUSTOM', + customer_file_source: 'BOTH_USER_AND_PARTNER_PROVIDED' + }) + .reply(201, { id: '123' }) + + const { data } = await facebookClient.createAudience(hookInputs.audienceName) + + expect(data).toEqual({ id: '123' }) + }) + }) +}) diff --git a/packages/destination-actions/src/destinations/facebook-custom-audiences/fbca-operations.ts b/packages/destination-actions/src/destinations/facebook-custom-audiences/fbca-operations.ts index 5bea0775fe..bb12a0bcbc 100644 --- a/packages/destination-actions/src/destinations/facebook-custom-audiences/fbca-operations.ts +++ b/packages/destination-actions/src/destinations/facebook-custom-audiences/fbca-operations.ts @@ -1,10 +1,11 @@ import { DynamicFieldItem, DynamicFieldError, RequestClient } from '@segment/actions-core' const FACEBOOK_API_VERSION = 'v20.0' +// exported for unit testing +export const BASE_URL = `https://graph.facebook.com/${FACEBOOK_API_VERSION}/` interface AudienceCreationResponse { id: string - message: string } interface GetAllAudienceResponse { @@ -29,17 +30,15 @@ interface FacebookResponseError { export default class FacebookClient { request: RequestClient - baseUrl: string adAccountId: string constructor(request: RequestClient, adAccountId: string) { this.request = request - this.baseUrl = `https://graph.facebook.com/${FACEBOOK_API_VERSION}/` this.adAccountId = this.formatAdAccount(adAccountId) } createAudience = async (name: string) => { - return this.request(`${this.baseUrl}${this.adAccountId}/customaudiences`, { + return await this.request(`${BASE_URL}${this.adAccountId}/customaudiences`, { method: 'post', json: { name, @@ -54,7 +53,7 @@ export default class FacebookClient { ): Promise<{ data?: GetSingleAudienceResponse; error?: FacebookResponseError }> => { try { const fields = '?fields=id,name' - const { data } = await this.request(`${this.baseUrl}${audienceId}${fields}`) + const { data } = await this.request(`${BASE_URL}${audienceId}${fields}`) return { data, error: undefined } } catch (error) { return { data: undefined, error: error as FacebookResponseError } @@ -63,7 +62,7 @@ export default class FacebookClient { getAllAudiences = async (): Promise<{ choices: DynamicFieldItem[]; error: DynamicFieldError | undefined }> => { const { data } = await this.request( - `${this.baseUrl}${this.adAccountId}/customaudiences?fields=id,name&limit=200` + `${BASE_URL}${this.adAccountId}/customaudiences?fields=id,name&limit=200` ) const choices = data.data.map(({ id, name }) => ({ diff --git a/packages/destination-actions/src/destinations/facebook-custom-audiences/sync/__tests__/index.test.ts b/packages/destination-actions/src/destinations/facebook-custom-audiences/sync/__tests__/index.test.ts index 6b14a45f80..78792197e0 100644 --- a/packages/destination-actions/src/destinations/facebook-custom-audiences/sync/__tests__/index.test.ts +++ b/packages/destination-actions/src/destinations/facebook-custom-audiences/sync/__tests__/index.test.ts @@ -3,7 +3,7 @@ import Destination from '../../index' const testDestination = createTestIntegration(Destination) -describe('FacebookCustomAudiences.add', () => { +describe('FacebookCustomAudiences.sync', () => { it('is magic', () => { expect(testDestination).toBe(testDestination) })