Skip to content

Commit

Permalink
test: add test for persons endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
joscha committed Jul 23, 2024
1 parent 7ee3be9 commit 9ab4d71
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/v1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Fields } from './fields.ts'
import { FieldValues } from './field_values.ts'
import { FieldValueChanges } from './field_value_changes.ts'
import { Organizations } from './organizations.ts'
import { Persons } from './persons.ts'
export type * as ListEntries from './list_entries.ts'
export type * as Lists from './lists.ts'
export type * as Fields from './fields.ts'
Expand Down Expand Up @@ -51,6 +52,7 @@ export class Affinity {
this.fieldValues = new FieldValues(this.axios)
this.fieldValueChanges = new FieldValueChanges(this.axios)
this.organizations = new Organizations(this.axios)
this.persons = new Persons(this.axios)
}

public readonly auth: Auth
Expand All @@ -66,4 +68,6 @@ export class Affinity {
public readonly fieldValueChanges: FieldValueChanges

public readonly organizations: Organizations

public readonly persons: Persons
}
43 changes: 43 additions & 0 deletions src/v1/tests/__snapshots__/persons_test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
export const snapshot = {};

snapshot[`persons > can search for persons 1`] = `
{
next_page_token: null,
persons: [
{
emails: [
"[email protected]",
],
first_name: "Joscha",
id: 198383654,
last_name: "Test",
primary_email: "[email protected]",
type: 0,
},
{
emails: [
"[email protected]",
],
first_name: "Someone's",
id: 191703462,
last_name: "and Else's appointments",
primary_email: "[email protected]",
type: 0,
},
{
emails: [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
],
first_name: "Joscha",
id: 54576635,
last_name: "Feth",
primary_email: "[email protected]",
type: 1,
},
],
}
`;
35 changes: 35 additions & 0 deletions src/v1/tests/fixtures/persons/search.raw.response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"next_page_token": null,
"persons": [
{
"emails": ["[email protected]"],
"first_name": "Joscha",
"id": 198383654,
"last_name": "Test",
"primary_email": "[email protected]",
"type": 0
},
{
"emails": ["[email protected]"],
"first_name": "Someone's",
"id": 191703462,
"last_name": "and Else's appointments",
"primary_email": "[email protected]",
"type": 0
},
{
"emails": [
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]",
"[email protected]"
],
"first_name": "Joscha",
"id": 54576635,
"last_name": "Feth",
"primary_email": "[email protected]",
"type": 1
}
]
}
156 changes: 156 additions & 0 deletions src/v1/tests/persons_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { assertSnapshot } from '@std/testing/snapshot'
import { afterEach, beforeEach, describe, it } from '@std/testing/bdd'

import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import { Affinity } from '../index.ts'
import { getRawFixture } from './get_raw_fixture.ts'
import { apiKey, isLiveRun } from './env.ts'
import { personFieldsUrl, personsUrl } from '../urls.ts'
import type { SearchPersonsRequest } from '../persons.ts'

describe('persons', () => {
let mock: MockAdapter
let affinity: Affinity

beforeEach(() => {
if (!isLiveRun()) {
mock = new MockAdapter(axios, { onNoMatch: 'throwException' })
}
affinity = new Affinity(apiKey() || 'api_key')
})
afterEach(() => {
mock?.reset()
})

// it('can get a specific person', async (t) => {
// const person_id = 64779194
// mock?.onGet(personsUrl(person_id)).reply(
// 200,
// await getRawFixture('persons/get.raw.response.json'),
// )
// const res = await affinity.persons.get({ person_id })
// await assertSnapshot(t, res)
// })

it('can search for persons', async (t) => {
const request = { term: 'joscha' }
mock?.onGet(personsUrl(), { params: request }).reply(
200,
await getRawFixture('persons/search.raw.response.json'),
)
const res = await affinity.persons.search(request)
await assertSnapshot(t, res)
})

// it('can search for persons with the appropriate dates', async (t) => {
// const myDate = new Date(1717428411010)
// const request: SearchPersonsRequest = {
// min_first_email_date: myDate,
// term: 'joscha',
// }
// mock?.onGet(personsUrl(), {
// params: {
// term: request.term,
// min_first_email_date: myDate.toISOString(),
// },
// }).reply(
// 200,
// await getRawFixture('persons/search.raw.response.json'),
// )
// const res = await affinity.persons.search(request)
// await assertSnapshot(t, res)
// })

// it('can create a new person', async (t) => {
// const data = {
// name: 'Acme Corporation',
// domain: 'acme.co',
// person_ids: [38706],
// }
// mock?.onPost(personsUrl()).reply(
// 201,
// await getRawFixture('persons/create.raw.response.json'),
// )
// const res = await affinity.persons.create(data)
// await assertSnapshot(t, res)
// })

// it('can update an person', async (t) => {
// const data = {
// person_id: 120611418,
// name: 'Acme Corp.',
// person_ids: [38706, 89734],
// }
// mock?.onPut(personsUrl(data.person_id)).reply(
// 200,
// await getRawFixture('persons/update.raw.response.json'),
// )
// const res = await affinity.persons.update(data)
// await assertSnapshot(t, res)
// })

// it('can delete an person', async (t) => {
// const person_id = 120611418
// mock?.onDelete(personsUrl(person_id)).reply(200, {
// success: true,
// })
// const res = await affinity.persons.delete({ person_id })
// await assertSnapshot(t, res)
// })

// it('can get global person fields', async (t) => {
// mock?.onGet(personFieldsUrl()).reply(
// 200,
// await getRawFixture('persons/get_fields.raw.response.json'),
// )
// const res = await affinity.persons.getFields()
// await assertSnapshot(t, res)
// })

// it('iterates over all persons', async (t) => {
// const params: SearchPersonsRequest = {
// term: 'fridel',
// page_size: 1,
// }

// {
// // set up pages sequentially, each referencing the one after
// const { default: pages } = await import(
// './fixtures/persons/paginated.iterator.combined.response.json',
// {
// with: {
// type: 'json',
// },
// }
// )

// pages.forEach((page, i) => {
// const { next_page_token: previous_page_token } = pages[i - 1] ||
// {}
// const data: SearchPersonsRequest = {
// ...params,
// }
// if (previous_page_token) {
// data.page_token = previous_page_token
// }
// // console.log('Setting up page', params, page.list_entries)
// mock?.onGet(personsUrl(), {
// params: data,
// }).reply(
// 200,
// page,
// )
// })
// }

// let page = 0
// for await (
// const entries of affinity.persons.searchIterator(params)
// ) {
// await assertSnapshot(t, entries, {
// name: `page ${++page} of persons`,
// })
// }
// })
})

0 comments on commit 9ab4d71

Please sign in to comment.