Skip to content

Commit

Permalink
Create exports.spec.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
p-zielinski committed Oct 10, 2023
1 parent b32f196 commit de440f3
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions packages/sdk/test/exports.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { voucherifyClient as client } from './client'
import { isoRegex } from './utils/isoRegex'
import { ExportResourceResponse } from '@voucherify/sdk'

describe('Exports API', () => {
it('Should create export, with no parameters', async () => {
const result = await client.distributions.exports.create({ exported_object: 'voucher' })
expect(result).toMatchObject({
id: expect.stringMatching(/^exp-*/),
object: 'export',
created_at: expect.stringMatching(isoRegex),
status: 'SCHEDULED',
channel: 'API',
exported_object: 'voucher',
parameters: {},
result: null,
user_id: null,
})
})

it('Should create export, with some parameters', async () => {
const request = {
exported_object: 'voucher' as const,
parameters: {
fields: ['id', 'code', 'voucher_type', 'value', 'discount_type'],
filters: { code: { conditions: { $is_unknown: true } } },
},
}
const result = await client.distributions.exports.create(request)
//we must narrow down result type, otherwise we will have hard time with 'parameters' type
expect(result.exported_object).toEqual(request.exported_object)
if (result.exported_object !== request.exported_object) {
return
}
//we need to use endpoint list, since only this endpoint has most complicated definition
const list = await client.distributions.exports.list()
const justExported = list.exports.find(_export => _export.id === result.id)
if (!justExported) {
return
}
//we must find out if 'fields' and 'filters' are defined, to figure out its value
expect(justExported.parameters.fields).toBeDefined()
if (!justExported.parameters.fields) {
return
}
expect(justExported.parameters.filters).toBeDefined()
if (!justExported.parameters.filters) {
return
}
expect(justExported as ExportResourceResponse).toMatchObject({
id: expect.stringMatching(/^exp-*/),
object: 'export',
created_at: expect.stringMatching(isoRegex),
status: expect.stringMatching(/.*/),
channel: 'API',
exported_object: request.exported_object,
parameters: request.parameters,
result: null,
user_id: null,
})
})
})

0 comments on commit de440f3

Please sign in to comment.