Skip to content

Commit

Permalink
tests: add mock_api tests
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenHulme committed Nov 10, 2023
1 parent eae71ff commit dc7ecb6
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions app/javascript/test_support/mock_api.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import mockApi from './mock_api'

describe('mockApi', () => {
let api = mockApi()

const barcode = 'DN12345'
const mockTube = { type: 'tube', id: 123 }
api.mockGet(`labware/${barcode}`, {}, { data: mockTube })
api.mockGet('labware', { filter: { barcode: barcode } }, { data: [mockTube] })
api.mockFail(
'server-errors/500',
{},
{
errors: [
{
detail: 'A server error occurred',
code: 500,
status: 500,
},
],
}
)

it('should make a get request and return a single tube', async () => {
let response = await api.devour.one('labware', barcode).get()

expect(response.data).toEqual(mockTube)
expect(response.errors).toEqual(undefined)
expect(response.meta).toEqual(undefined)
expect(response.links).toEqual(undefined)
})

it('should make a findall request and return an array of tubes', async () => {
let response = await api.devour.findAll('labware', {
filter: { barcode: barcode },
})

expect(response.data).toEqual([mockTube])
expect(response.errors).toEqual(undefined)
expect(response.meta).toEqual(undefined)
expect(response.links).toEqual(undefined)
})

it('should make a failed request and return an error', async () => {
let response = await api.devour
.one('server-errors', '500')
.get()
.catch((err) => {
return err
})

expect(response.data).toEqual(undefined)
expect(response.errors).toEqual([{ code: 500, detail: 'A server error occurred', status: 500 }])
expect(response.meta).toEqual(undefined)
expect(response.links).toEqual(undefined)
})
})

0 comments on commit dc7ecb6

Please sign in to comment.