From dc7ecb6dfe101a1a726b9b0bafb3b77a0752d795 Mon Sep 17 00:00:00 2001 From: Stephen Hulme Date: Fri, 10 Nov 2023 13:06:42 +0000 Subject: [PATCH] tests: add mock_api tests --- app/javascript/test_support/mock_api.spec.js | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 app/javascript/test_support/mock_api.spec.js diff --git a/app/javascript/test_support/mock_api.spec.js b/app/javascript/test_support/mock_api.spec.js new file mode 100644 index 0000000000..2a1a91011e --- /dev/null +++ b/app/javascript/test_support/mock_api.spec.js @@ -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) + }) +})