Skip to content

Commit

Permalink
WIP on an inexplicably not passing unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-Ag committed Jul 18, 2024
1 parent fc4f5ed commit 8f5f5ed
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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' })
})
})
})
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<AudienceCreationResponse>(`${this.baseUrl}${this.adAccountId}/customaudiences`, {
return await this.request<AudienceCreationResponse>(`${BASE_URL}${this.adAccountId}/customaudiences`, {
method: 'post',
json: {
name,
Expand All @@ -54,7 +53,7 @@ export default class FacebookClient {
): Promise<{ data?: GetSingleAudienceResponse; error?: FacebookResponseError }> => {
try {
const fields = '?fields=id,name'
const { data } = await this.request<GetSingleAudienceResponse>(`${this.baseUrl}${audienceId}${fields}`)
const { data } = await this.request<GetSingleAudienceResponse>(`${BASE_URL}${audienceId}${fields}`)
return { data, error: undefined }
} catch (error) {
return { data: undefined, error: error as FacebookResponseError }
Expand All @@ -63,7 +62,7 @@ export default class FacebookClient {

getAllAudiences = async (): Promise<{ choices: DynamicFieldItem[]; error: DynamicFieldError | undefined }> => {
const { data } = await this.request<GetAllAudienceResponse>(
`${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 }) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit 8f5f5ed

Please sign in to comment.