Skip to content

Commit

Permalink
add tests for useApiCall
Browse files Browse the repository at this point in the history
  • Loading branch information
koji committed May 15, 2024
1 parent 529d4bd commit b2a4100
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
3 changes: 2 additions & 1 deletion opentrons-ai-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"node": ">=18.19.0"
},
"devDependencies": {
"@types/styled-components": "^5.1.26"
"@types/styled-components": "^5.1.26",
"axios-mock-adapter": "1.22.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, it, expect } from 'vitest'
import { act, renderHook } from '@testing-library/react'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import { useApiCall } from '../useApiCall'

const mock = new MockAdapter(axios)

describe('useApiCall', () => {
it('should post data successfully', async () => {
const mockData = { message: 'Hello, World!' }
mock.onPost('/test').reply(200, mockData)

const { result } = renderHook(() => useApiCall())

await act(async () => {
await result.current.fetchData({
url: '/test',
method: 'POST',
data: mockData,
})
})

expect(result.current.isLoading).toBe(false)
expect(result.current.data).toEqual(mockData)
expect(result.current.error).toBe(null)
})

it('should handle post error', async () => {
mock.onPost('/test').networkError()

const { result } = renderHook(() => useApiCall())

await act(async () => {
await result.current.fetchData({ url: '/test', method: 'POST', data: {} })
})

expect(result.current.isLoading).toBe(false)
expect(result.current.data).toBe(null)
expect(result.current.error).toBe('Network Error')
})
})
10 changes: 9 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5741,6 +5741,14 @@ aws4@^1.8.0:
resolved "https://registry.yarnpkg.com/aws4/-/aws4-1.12.0.tgz#ce1c9d143389679e253b314241ea9aa5cec980d3"
integrity sha512-NmWvPnx0F1SfrQbYwOi7OeaNGokp9XhzNioJ/CSBs8Qa4vxug81mhJEAVZwxXuBmYB5KDRfMq/F3RR0BIU7sWg==

[email protected]:
version "1.22.0"
resolved "https://registry.yarnpkg.com/axios-mock-adapter/-/axios-mock-adapter-1.22.0.tgz#0f3e6be0fc9b55baab06f2d49c0b71157e7c053d"
integrity sha512-dmI0KbkyAhntUR05YY96qg2H6gg0XMl2+qTW0xmYg6Up+BFBAJYRLROMXRdDEL06/Wqwa0TJThAYvFtSFdRCZw==
dependencies:
fast-deep-equal "^3.1.3"
is-buffer "^2.0.5"

axios@^0.21.1:
version "0.21.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
Expand Down Expand Up @@ -12224,7 +12232,7 @@ is-buffer@^1.1.5:
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.6.tgz#efaa2ea9daa0d7ab2ea13a97b2b8ad51fefbe8be"
integrity sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==

is-buffer@^2.0.0:
is-buffer@^2.0.0, is-buffer@^2.0.5:
version "2.0.5"
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-2.0.5.tgz#ebc252e400d22ff8d77fa09888821a24a658c191"
integrity sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ==
Expand Down

0 comments on commit b2a4100

Please sign in to comment.