forked from blakeblackshear/frigate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53288d3
commit 6d133ef
Showing
5 changed files
with
136 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const baseUrl = 'http:https://base-url.local:5000'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { h } from 'preact'; | ||
import { ApiProvider, useFetch, useApiHost } from '..'; | ||
import { render, screen } from '@testing-library/preact'; | ||
|
||
jest.mock('../baseUrl'); | ||
|
||
describe('useApiHost', () => { | ||
test('is set from the baseUrl', async () => { | ||
function Test() { | ||
const apiHost = useApiHost(); | ||
return <div>{apiHost}</div>; | ||
} | ||
render( | ||
<ApiProvider> | ||
<Test /> | ||
</ApiProvider> | ||
); | ||
expect(screen.queryByText('http:https://base-url.local:5000')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe('useFetch', () => { | ||
function Test() { | ||
const { data, status } = useFetch('/api/tacos'); | ||
return ( | ||
<div> | ||
<span>{data ? data.returnData : ''}</span> | ||
<span>{status}</span> | ||
</div> | ||
); | ||
} | ||
test('loads data', async () => { | ||
const fetchSpy = jest.spyOn(window, 'fetch').mockImplementation( | ||
(url) => | ||
new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve({ ok: true, json: () => Promise.resolve({ returnData: 'yep' }) }); | ||
}, 1); | ||
}) | ||
); | ||
|
||
render( | ||
<ApiProvider> | ||
<Test /> | ||
</ApiProvider> | ||
); | ||
|
||
expect(screen.queryByText('loading')).toBeInTheDocument(); | ||
expect(screen.queryByText('yep')).not.toBeInTheDocument(); | ||
|
||
jest.runAllTimers(); | ||
await screen.findByText('loaded'); | ||
expect(fetchSpy).toHaveBeenCalledWith('http:https://base-url.local:5000/api/tacos'); | ||
|
||
expect(screen.queryByText('loaded')).toBeInTheDocument(); | ||
expect(screen.queryByText('yep')).toBeInTheDocument(); | ||
}); | ||
|
||
test('sets error if response is not okay', async () => { | ||
jest.spyOn(window, 'fetch').mockImplementation( | ||
(url) => | ||
new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve({ ok: false }); | ||
}, 1); | ||
}) | ||
); | ||
|
||
render( | ||
<ApiProvider> | ||
<Test /> | ||
</ApiProvider> | ||
); | ||
|
||
expect(screen.queryByText('loading')).toBeInTheDocument(); | ||
jest.runAllTimers(); | ||
await screen.findByText('error'); | ||
}); | ||
|
||
test('does not re-fetch if the query has already been made', async () => { | ||
const fetchSpy = jest.spyOn(window, 'fetch').mockImplementation( | ||
(url) => | ||
new Promise((resolve) => { | ||
setTimeout(() => { | ||
resolve({ ok: true, json: () => Promise.resolve({ returnData: 'yep' }) }); | ||
}, 1); | ||
}) | ||
); | ||
|
||
const { rerender } = render( | ||
<ApiProvider> | ||
<Test key={0} /> | ||
</ApiProvider> | ||
); | ||
|
||
expect(screen.queryByText('loading')).toBeInTheDocument(); | ||
expect(screen.queryByText('yep')).not.toBeInTheDocument(); | ||
|
||
jest.runAllTimers(); | ||
await screen.findByText('loaded'); | ||
expect(fetchSpy).toHaveBeenCalledWith('http:https://base-url.local:5000/api/tacos'); | ||
|
||
rerender( | ||
<ApiProvider> | ||
<Test key={1} /> | ||
</ApiProvider> | ||
); | ||
|
||
expect(screen.queryByText('loaded')).toBeInTheDocument(); | ||
expect(screen.queryByText('yep')).toBeInTheDocument(); | ||
|
||
jest.runAllTimers(); | ||
|
||
expect(fetchSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const baseUrl = import.meta.env.SNOWPACK_PUBLIC_API_HOST || window.baseUrl || ''; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters