Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jayan-blutui committed Nov 14, 2024
1 parent 99790f5 commit 551615c
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 4 deletions.
72 changes: 72 additions & 0 deletions src/resources/agency/cassettes/cassettes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,76 @@ describe('Cassette', () => {
})
})
})

describe('create', () => {
it('can create a new cassette', async () => {
fetchOnce(cassetteFixture)
const cassette = await blutui.agency('foo').cassettes.create({
handle: 'default',
name: 'Default',
project: 'project-id',
})

expect(fetchURL()).toBe(`${blutui.baseURL}/v1/agencies/foo/cassettes`)
expect(cassette).toMatchObject({
object: 'cassette',
name: 'Default',
})
})
})

describe('update', () => {
it('can update an existing cassette', async () => {
fetchOnce(cassetteFixture)
const cassette = await blutui
.agency('foo')
.cassettes.update(cassetteFixture.id, {
name: 'Default',
})

expect(fetchURL()).toBe(
`${blutui.baseURL}/v1/agencies/foo/cassettes/${cassetteFixture.id}`
)
expect(cassette).toMatchObject({
object: 'cassette',
name: 'Default',
})
})
})

describe('remove', () => {
it('can remove a cassette', async () => {
fetchOnce({ id: cassetteFixture.id, object: 'cassette', deleted: true })
const cassette = await blutui
.agency('foo')
.cassettes.remove(cassetteFixture.id)

expect(fetchURL()).toBe(
`${blutui.baseURL}/v1/agencies/foo/cassettes/${cassetteFixture.id}`
)
expect(cassette).toMatchObject({
object: 'cassette',
deleted: true,
})
})
})

describe('duplicate', () => {
it('can duplicate an existing cassette', async () => {
fetchOnce(cassetteFixture)
const cassette = await blutui
.agency('foo')
.cassettes.duplicate(cassetteFixture.id, {
name: 'Default',
handle: 'default',
})

expect(fetchURL()).toBe(
`${blutui.baseURL}/v1/agencies/foo/cassettes/${cassetteFixture.id}/duplicate`
)
expect(cassette).toMatchObject({
object: 'cassette',
})
})
})
})
78 changes: 74 additions & 4 deletions src/resources/agency/cassettes/cassettes.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,86 @@
import { deserializeCassette } from './serializers'
import {
deserializeCassette,
serializeCreateCassetteOptions,
serializeDuplicateCassetteOptions,
serializeUpdateCassetteOptions,
} from './serializers'

import type { Agency } from '@/agency'
import type { CassetteResponse, Cassette } from './interfaces'
import type {
CassetteResponse,
Cassette,
CreateCassetteOptions,
SerializedCreateCassetteOptions,
UpdateCassetteOptions,
SerializedUpdateCassetteOptions,
DuplicateCassetteOptions,
SerializedDuplicateCassetteOptions,
} from './interfaces'
import type { DeletedResponse, Expandable } from '@/types'

export class Cassettes {
constructor(private readonly agency: Agency) {}

/**
* Retrieve a Cassette by ID.
*/
async get(id: string): Promise<Cassette> {
const { data } = await this.agency.get<CassetteResponse>(`cassettes/${id}`)
async get(id: string, options?: Expandable<'project'>): Promise<Cassette> {
const { data } = await this.agency.get<CassetteResponse>(
`cassettes/${id}`,
{
query: options,
}
)

return deserializeCassette(data)
}

/**
* Create a new Cassette for a project your agency..
*/
async create(payload: CreateCassetteOptions): Promise<Cassette> {
const { data } = await this.agency.post<
CassetteResponse,
SerializedCreateCassetteOptions
>('cassettes', serializeCreateCassetteOptions(payload))

return deserializeCassette(data)
}

/**
* Update a Cassette by ID.
*/
async update(id: string, payload: UpdateCassetteOptions): Promise<Cassette> {
const { data } = await this.agency.patch<
CassetteResponse,
SerializedUpdateCassetteOptions
>(`cassettes/${id}`, serializeUpdateCassetteOptions(payload))

return deserializeCassette(data)
}

/**
* Remove a Cassette from a project in your agency.
*/
async remove(id: string): Promise<DeletedResponse> {
const { data } = await this.agency.delete<DeletedResponse>(
`cassettes/${id}`
)

return data
}

/**
* Duplicate a Cassette for a project your agency.
*/
async duplicate(
id: string,
payload: DuplicateCassetteOptions
): Promise<Cassette> {
const { data } = await this.agency.post<
CassetteResponse,
SerializedDuplicateCassetteOptions
>(`cassettes/${id}/duplicate`, serializeDuplicateCassetteOptions(payload))

return deserializeCassette(data)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export interface CreateCassetteOptions {
name: string
project: string
handle: string
}

export interface SerializedCreateCassetteOptions {
name: string
project: string
handle: string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface DuplicateCassetteOptions {
handle: string
name: string
}

export interface SerializedDuplicateCassetteOptions {
handle: string
name: string
}
3 changes: 3 additions & 0 deletions src/resources/agency/cassettes/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './cassette.interface'
export * from './create-cassette-options.interface'
export * from './duplicate-cassette-options.interface'
export * from './update-cassette-options.interface'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface UpdateCassetteOptions {
name: string
}

export interface SerializedUpdateCassetteOptions {
name: string
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type {
CreateCassetteOptions,
SerializedCreateCassetteOptions,
} from '../interfaces'

export const serializeCreateCassetteOptions = (
options: CreateCassetteOptions
): SerializedCreateCassetteOptions => ({
handle: options.handle,
name: options.name,
project: options.project,
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type {
DuplicateCassetteOptions,
SerializedDuplicateCassetteOptions,
} from '../interfaces'

export const serializeDuplicateCassetteOptions = (
options: DuplicateCassetteOptions
): SerializedDuplicateCassetteOptions => ({
handle: options.handle,
name: options.name,
})
3 changes: 3 additions & 0 deletions src/resources/agency/cassettes/serializers/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export * from './cassette.serializer'
export * from './create-cassette-options.serializer'
export * from './duplicate-cassette-options.serializer'
export * from './update-cassette-options.serializer'
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type {
SerializedUpdateCassetteOptions,
UpdateCassetteOptions,
} from '../interfaces'

export const serializeUpdateCassetteOptions = (
options: UpdateCassetteOptions
): SerializedUpdateCassetteOptions => ({
name: options.name,
})

0 comments on commit 551615c

Please sign in to comment.