Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions apps/web/src/modules/analyze/hooks/useTopicNavbarScroll.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { act, renderHook } from '@testing-library/react';
import useTopicNavbarScroll from './useTopicNavbarScroll';

jest.mock('next-i18next', () => ({
useTranslation: () => ({ t: (key: string) => `t:${key}` }),
}));

describe('useTopicNavbarScroll', () => {
it('resolves the overview title through i18n, not a hardcoded English string', () => {
jest.useFakeTimers();
const card = document.createElement('div');
card.id = 'topic_overview';
card.className = 'base-card';
card.getBoundingClientRect = () =>
({ top: 0, bottom: 100 }) as DOMRect;
document.body.appendChild(card);
Object.defineProperty(document.documentElement, 'scrollTop', {
value: 200,
configurable: true,
writable: true,
});

const { result } = renderHook(() => useTopicNavbarScroll());
act(() => {
document.dispatchEvent(new Event('scroll'));
jest.advanceTimersByTime(600);
});

expect(result.current.topicTitle).toBe('t:analyze:overview');
expect(result.current.topicTitle).not.toBe('Overview');
document.body.removeChild(card);
});
});
4 changes: 3 additions & 1 deletion apps/web/src/modules/analyze/hooks/useTopicNavbarScroll.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useMemo, useState } from 'react';
import { useTranslation } from 'next-i18next';
import { throttle } from 'lodash';

const isCardInView = (rect: DOMRect) => {
Expand Down Expand Up @@ -47,6 +48,7 @@ function lookupParentSiblingH2(element: HTMLElement): HTMLElement | null {

const useTopicNavbarScroll = () => {
const [inViewCardId, setInViewCardId] = useState('');
const { t } = useTranslation();

useEffect(() => {
const scrollEventListener = throttle(() => {
Expand Down Expand Up @@ -79,7 +81,7 @@ const useTopicNavbarScroll = () => {
}

if (inViewCardId === 'topic_overview') {
return { topicTitle: 'Overview', subTitle: '' };
return { topicTitle: t('analyze:overview'), subTitle: '' };
}

let topicTitle = '';
Expand Down