diff --git a/packages/vkui/src/hooks/useAdaptivityHasHover.test.tsx b/packages/vkui/src/hooks/useAdaptivityHasHover.test.tsx new file mode 100644 index 0000000000..1e5e14c379 --- /dev/null +++ b/packages/vkui/src/hooks/useAdaptivityHasHover.test.tsx @@ -0,0 +1,29 @@ +import React from 'react'; +import { renderHook } from '@testing-library/react-hooks'; +import { AdaptivityProvider } from '../components/AdaptivityProvider/AdaptivityProvider'; +import { useAdaptivityHasHover } from './useAdaptivityHasHover'; + +describe(useAdaptivityHasHover, () => { + it('returns on client', () => { + const { result } = renderHook(useAdaptivityHasHover, {}); + expect(result.current).toEqual(true); + }); + + it('context hasHover={true}', () => { + const { result } = renderHook(useAdaptivityHasHover, { + wrapper: ({ children }) => ( + {children} + ), + }); + expect(result.current).toEqual(true); + }); + + it('context hasHover={false}', () => { + const { result } = renderHook(useAdaptivityHasHover, { + wrapper: ({ children }) => ( + {children} + ), + }); + expect(result.current).toEqual(false); + }); +}); diff --git a/packages/vkui/src/hooks/useAdaptivityHasHover.ts b/packages/vkui/src/hooks/useAdaptivityHasHover.ts index fbeedc6d3e..38247c3821 100644 --- a/packages/vkui/src/hooks/useAdaptivityHasHover.ts +++ b/packages/vkui/src/hooks/useAdaptivityHasHover.ts @@ -15,9 +15,11 @@ export function useAdaptivityHasHover(deferDetect = true): undefined | boolean { const { hasHover: hasHoverContext } = React.useContext(AdaptivityContext); const hasHover = hasHoverContext === undefined ? hasHoverLib : hasHoverContext; - const isClient = useIsClient(!deferDetect); - if (!isClient) { - return undefined; + const needTwoPassRendering = deferDetect || hasHoverContext === undefined; + + const isClient = useIsClient(!needTwoPassRendering); + if (!isClient || hasHoverContext !== undefined) { + return hasHoverContext; } return hasHover; diff --git a/packages/vkui/src/hooks/useAdaptivityHasPointer.ts b/packages/vkui/src/hooks/useAdaptivityHasPointer.ts index b3782d3884..1f394f02cd 100644 --- a/packages/vkui/src/hooks/useAdaptivityHasPointer.ts +++ b/packages/vkui/src/hooks/useAdaptivityHasPointer.ts @@ -14,9 +14,9 @@ export function useAdaptivityHasPointer(deferDetect?: false): boolean; export function useAdaptivityHasPointer(deferDetect = true): undefined | boolean { const { hasPointer: hasPointerContext } = React.useContext(AdaptivityContext); - const needTwoPassRendering = !deferDetect || hasPointerContext !== undefined; + const needTwoPassRendering = deferDetect || hasPointerContext === undefined; - const isClient = useIsClient(needTwoPassRendering); + const isClient = useIsClient(!needTwoPassRendering); if (!isClient || hasPointerContext !== undefined) { return hasPointerContext; }