test(useSearchModal): Add new test case for location change

Signed-off-by: Renan Mendes Carvalho <aitherios@gmail.com>
This commit is contained in:
Renan Mendes Carvalho
2023-03-10 16:23:54 +01:00
committed by Fredrik Adelöw
parent 3b9e96c536
commit 430b9f2890
2 changed files with 21 additions and 9 deletions
@@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { act, renderHook } from '@testing-library/react-hooks';
import { useSearchModal } from './useSearchModal';
import { BrowserRouter } from 'react-router-dom';
import { BrowserRouter, Router } from 'react-router-dom';
import { createMemoryHistory } from 'history';
describe('useSearchModal', () => {
it.each([
@@ -79,4 +80,21 @@ describe('useSearchModal', () => {
hidden: true,
});
});
it('should hide when location changes', () => {
const history = createMemoryHistory({ initialEntries: ['/'] });
const rendered = renderHook(() => useSearchModal(true), {
wrapper: ({ children }) => (
<Router location={history.location} navigator={history}>
{children}
</Router>
),
});
expect(rendered.result.current.state.hidden).toBe(false);
act(() => history.push('/new/path'));
rendered.rerender();
expect(rendered.result.current.state.hidden).toBe(true);
});
});
@@ -14,13 +14,7 @@
* limitations under the License.
*/
import React, {
ReactNode,
useCallback,
useContext,
useState,
useEffect,
} from 'react';
import React, { ReactNode, useCallback, useContext, useState } from 'react';
import { useLocation } from 'react-router-dom';
import {
createVersionedContext,