prometheus/web/ui/react-app/src/hooks/useLocalStorage.test.tsx
Dustin Hooten ca60bf298c React UI: Implement /targets page (#6276)
* Add LastScrapeDuration to targets endpoint

Signed-off-by: Dustin Hooten <dhooten@splunk.com>

* Add Scrape job name to targets endpoint

Signed-off-by: Dustin Hooten <dhooten@splunk.com>

* Implement the /targets page in react

Signed-off-by: Dustin Hooten <dhooten@splunk.com>

* Add state query param to targets endpoint

Signed-off-by: Dustin Hooten <dhooten@splunk.com>

* Use state filter in api call

Signed-off-by: Dustin Hooten <dhooten@splunk.com>

* api feedback

Signed-off-by: Dustin Hooten <dhooten@splunk.com>

* pr feedback frontend

Signed-off-by: Dustin Hooten <dhooten@splunk.com>

* Implement and use localstorage hook

Signed-off-by: Dustin Hooten <dhooten@splunk.com>

* PR feedback

Signed-off-by: Dustin Hooten <dhooten@splunk.com>
2019-11-11 22:42:24 +01:00

28 lines
1.1 KiB
TypeScript

import { useLocalStorage } from './useLocalStorage';
import { renderHook, act } from '@testing-library/react-hooks';
describe('useLocalStorage', () => {
it('returns the initialState', () => {
const initialState = { a: 1, b: 2 };
const { result } = renderHook(() => useLocalStorage('mystorage', initialState));
expect(result.current[0]).toEqual(initialState);
});
it('stores the initialState as serialized json in localstorage', () => {
const key = 'mystorage';
const initialState = { a: 1, b: 2 };
renderHook(() => useLocalStorage(key, initialState));
expect(localStorage.getItem(key)).toEqual(JSON.stringify(initialState));
});
it('returns a setValue function that can reset local storage', () => {
const key = 'mystorage';
const initialState = { a: 1, b: 2 };
const { result } = renderHook(() => useLocalStorage(key, initialState));
const newValue = { a: 2, b: 5 };
act(() => {
result.current[1](newValue);
});
expect(result.current[0]).toEqual(newValue);
expect(localStorage.getItem(key)).toEqual(JSON.stringify(newValue));
});
});