|
| 1 | +import { renderHook } from '@testing-library/react-hooks'; |
| 2 | +import * as Storage from 'aws-amplify/storage'; |
| 3 | + |
| 4 | +import useGetUrl, { UseGetUrlInput } from '../useGetUrl'; |
| 5 | + |
| 6 | +const getUrlSpy = jest.spyOn(Storage, 'getUrl'); |
| 7 | + |
| 8 | +const url = new URL('https://amplify.s3.amazonaws.com/path/to/the/file.jpg'); |
| 9 | + |
| 10 | +const onError = jest.fn(); |
| 11 | + |
| 12 | +const KEY_INPUT: UseGetUrlInput = { |
| 13 | + key: 'file.jpg', |
| 14 | + options: { accessLevel: 'guest' }, |
| 15 | + onError, |
| 16 | +}; |
| 17 | + |
| 18 | +const PATH_INPUT: UseGetUrlInput = { |
| 19 | + path: 'guest/file.jpg', |
| 20 | + onError, |
| 21 | +}; |
| 22 | + |
| 23 | +describe('useGetUrl', () => { |
| 24 | + beforeEach(() => { |
| 25 | + getUrlSpy.mockClear(); |
| 26 | + onError.mockClear(); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('with key params', () => { |
| 30 | + it('should return a Storage URL', async () => { |
| 31 | + getUrlSpy.mockResolvedValue({ url, expiresAt: new Date() }); |
| 32 | + |
| 33 | + const { result, waitForNextUpdate } = renderHook(() => |
| 34 | + useGetUrl(KEY_INPUT) |
| 35 | + ); |
| 36 | + |
| 37 | + expect(getUrlSpy).toHaveBeenCalledWith({ |
| 38 | + key: KEY_INPUT.key, |
| 39 | + options: KEY_INPUT.options, |
| 40 | + }); |
| 41 | + expect(result.current.isLoading).toBe(true); |
| 42 | + expect(result.current.url).toBe(undefined); |
| 43 | + |
| 44 | + // Next update will happen when getUrl resolves |
| 45 | + await waitForNextUpdate(); |
| 46 | + |
| 47 | + expect(getUrlSpy).toHaveBeenCalledTimes(1); |
| 48 | + expect(result.current.isLoading).toBe(false); |
| 49 | + expect(result.current.url).toBe(url); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should invoke onStorageGetError when getUrl fails', async () => { |
| 53 | + const customError = new Error('Something went wrong'); |
| 54 | + getUrlSpy.mockRejectedValue(customError); |
| 55 | + |
| 56 | + const { result, waitForNextUpdate } = renderHook(() => |
| 57 | + useGetUrl(KEY_INPUT) |
| 58 | + ); |
| 59 | + |
| 60 | + expect(getUrlSpy).toHaveBeenCalledWith({ |
| 61 | + key: KEY_INPUT.key, |
| 62 | + options: KEY_INPUT.options, |
| 63 | + }); |
| 64 | + |
| 65 | + // Next update will happen when getUrl resolves |
| 66 | + await waitForNextUpdate(); |
| 67 | + |
| 68 | + expect(result.current.isLoading).toBe(false); |
| 69 | + expect(result.current.url).toBe(undefined); |
| 70 | + expect(onError).toHaveBeenCalledTimes(1); |
| 71 | + expect(onError).toHaveBeenCalledWith(customError); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('with path params', () => { |
| 76 | + it('should return a Storage URL', async () => { |
| 77 | + getUrlSpy.mockResolvedValue({ url, expiresAt: new Date() }); |
| 78 | + |
| 79 | + const { result, waitForNextUpdate } = renderHook(() => |
| 80 | + useGetUrl(PATH_INPUT) |
| 81 | + ); |
| 82 | + |
| 83 | + expect(getUrlSpy).toHaveBeenCalledWith({ path: PATH_INPUT.path }); |
| 84 | + expect(result.current.isLoading).toBe(true); |
| 85 | + expect(result.current.url).toBe(undefined); |
| 86 | + |
| 87 | + // Next update will happen when getUrl resolves |
| 88 | + await waitForNextUpdate(); |
| 89 | + |
| 90 | + expect(getUrlSpy).toHaveBeenCalledTimes(1); |
| 91 | + expect(result.current.isLoading).toBe(false); |
| 92 | + expect(result.current.url).toBe(url); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should invoke onGetUrlError when getUrl fails', async () => { |
| 96 | + const customError = new Error('Something went wrong'); |
| 97 | + getUrlSpy.mockRejectedValue(customError); |
| 98 | + |
| 99 | + const { result, waitForNextUpdate } = renderHook(() => |
| 100 | + useGetUrl(PATH_INPUT) |
| 101 | + ); |
| 102 | + |
| 103 | + expect(getUrlSpy).toHaveBeenCalledWith({ path: PATH_INPUT.path }); |
| 104 | + |
| 105 | + // Next update will happen when getUrl resolves |
| 106 | + await waitForNextUpdate(); |
| 107 | + |
| 108 | + expect(result.current.isLoading).toBe(false); |
| 109 | + expect(result.current.url).toBe(undefined); |
| 110 | + expect(onError).toHaveBeenCalledTimes(1); |
| 111 | + expect(onError).toHaveBeenCalledWith(customError); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it('ignores the first response if rerun a second time before the first call resolves in the happy path', async () => { |
| 116 | + const secondUrl = new URL( |
| 117 | + 'https://amplify.s3.amazonaws.com/path/to/the/second-file.jpg' |
| 118 | + ); |
| 119 | + |
| 120 | + getUrlSpy |
| 121 | + .mockResolvedValueOnce({ url, expiresAt: new Date() }) |
| 122 | + .mockResolvedValueOnce({ url: secondUrl, expiresAt: new Date() }); |
| 123 | + |
| 124 | + const { result, waitForNextUpdate, rerender } = renderHook( |
| 125 | + (input: UseGetUrlInput = PATH_INPUT) => useGetUrl(input) |
| 126 | + ); |
| 127 | + |
| 128 | + expect(getUrlSpy).toHaveBeenCalledWith({ path: PATH_INPUT.path }); |
| 129 | + expect(result.current.isLoading).toBe(true); |
| 130 | + expect(result.current.url).toBe(undefined); |
| 131 | + |
| 132 | + rerender({ ...PATH_INPUT, path: 'guest/second-file.jpg' }); |
| 133 | + expect(result.current.isLoading).toBe(true); |
| 134 | + expect(result.current.url).toBe(undefined); |
| 135 | + |
| 136 | + // Next update will happen when getUrl resolves |
| 137 | + await waitForNextUpdate(); |
| 138 | + |
| 139 | + expect(getUrlSpy).toHaveBeenCalledWith({ |
| 140 | + path: 'guest/second-file.jpg', |
| 141 | + }); |
| 142 | + |
| 143 | + expect(getUrlSpy).toHaveBeenCalledTimes(2); |
| 144 | + expect(result.current.isLoading).toBe(false); |
| 145 | + expect(result.current.url).toBe(secondUrl); |
| 146 | + }); |
| 147 | + |
| 148 | + it('ignores the first response if rerun a second time before the first call resolves in the unhappy path', async () => { |
| 149 | + const firstError = new Error('Something went wrong'); |
| 150 | + const secondError = new Error('Something went wrong again'); |
| 151 | + |
| 152 | + getUrlSpy |
| 153 | + .mockRejectedValueOnce(firstError) |
| 154 | + .mockRejectedValueOnce(secondError); |
| 155 | + |
| 156 | + const { result, waitForNextUpdate, rerender } = renderHook( |
| 157 | + (input: UseGetUrlInput = PATH_INPUT) => useGetUrl(input) |
| 158 | + ); |
| 159 | + |
| 160 | + expect(result.current.isLoading).toBe(true); |
| 161 | + expect(result.current.url).toBe(undefined); |
| 162 | + expect(onError).toHaveBeenCalledTimes(0); |
| 163 | + |
| 164 | + rerender({ ...PATH_INPUT, path: 'guest/second-file.jpg' }); |
| 165 | + |
| 166 | + expect(result.current.isLoading).toBe(true); |
| 167 | + expect(result.current.url).toBe(undefined); |
| 168 | + expect(onError).toHaveBeenCalledTimes(0); |
| 169 | + |
| 170 | + await waitForNextUpdate(); |
| 171 | + |
| 172 | + expect(result.current.isLoading).toBe(false); |
| 173 | + expect(getUrlSpy).toHaveBeenCalledTimes(2); |
| 174 | + expect(result.current.url).toBe(undefined); |
| 175 | + expect(onError).toHaveBeenCalledTimes(1); |
| 176 | + expect(onError).toHaveBeenCalledWith(secondError); |
| 177 | + }); |
| 178 | + |
| 179 | + it('does not call `onError` if it is not a function', async () => { |
| 180 | + const customError = new Error('Something went wrong'); |
| 181 | + |
| 182 | + getUrlSpy.mockRejectedValueOnce(customError); |
| 183 | + |
| 184 | + const input = { ...PATH_INPUT, onError: null }; |
| 185 | + |
| 186 | + const { result, waitForNextUpdate } = renderHook(() => |
| 187 | + // @ts-expect-error test against invalid input |
| 188 | + useGetUrl(input) |
| 189 | + ); |
| 190 | + |
| 191 | + expect(result.current.isLoading).toBe(true); |
| 192 | + expect(getUrlSpy).toHaveBeenCalledWith({ path: PATH_INPUT.path }); |
| 193 | + expect(result.current.url).toBe(undefined); |
| 194 | + |
| 195 | + await waitForNextUpdate(); |
| 196 | + |
| 197 | + expect(result.current.isLoading).toBe(false); |
| 198 | + expect(result.current.url).toBe(undefined); |
| 199 | + expect(onError).toHaveBeenCalledTimes(0); |
| 200 | + }); |
| 201 | +}); |
0 commit comments