Skip to content

Commit

Permalink
fix spec coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWoodward authored and RoyEJohnson committed Feb 13, 2024
1 parent 9b13088 commit 70ae22b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
37 changes: 31 additions & 6 deletions src/app/content/components/TableOfContents/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React from 'react';
import { unmountComponentAtNode } from 'react-dom';
import { act as reactDomAct } from 'react-dom/test-utils';
import renderer from 'react-test-renderer';
import ConnectedTableOfContents, { TableOfContents } from '.';
import createTestStore from '../../../../test/createTestStore';
import { book as archiveBook, page, shortPage } from '../../../../test/mocks/archiveLoader';
import { mockCmsBook } from '../../../../test/mocks/osWebLoader';
import { renderToDom } from '../../../../test/reactutils';
import TestContainer from '../../../../test/TestContainer';
import * as reactUtils from '../../../reactUtils';
import { AppState, Store } from '../../../types';
import { assertWindow } from '../../../utils';
import * as actions from '../../actions';
import { initialState } from '../../reducer';
import { formatBookData } from '../../utils';
import * as domUtils from '../../utils/domUtils';
import * as reactUtils from '../../../reactUtils';

const book = formatBookData(archiveBook, mockCmsBook);

Expand Down Expand Up @@ -62,7 +63,6 @@ describe('TableOfContents', () => {
expect(scrollSidebarSectionIntoView).toHaveBeenCalledTimes(2);
});

jest.useFakeTimers();
it('opens and closes', () => {
jest.spyOn(reactUtils, 'useMatchMobileQuery')
.mockReturnValue(true);
Expand All @@ -77,18 +77,43 @@ describe('TableOfContents', () => {
renderer.act(() => {
store.dispatch(actions.closeToc());
});

expect(component.root.findByType(TableOfContents).props.isOpen).toBe(false);

renderer.act(() => {
store.dispatch(actions.openToc());
});
expect(component.root.findByType(TableOfContents).props.isOpen).toBe(true);
});

it('focuses when opening', () => {
jest.spyOn(reactUtils, 'useMatchMobileMediumQuery')
.mockReturnValue(true);

const {root} = renderToDom(<TestContainer store={store}>
<ConnectedTableOfContents />
</TestContainer>);
const sb = root.querySelector('[data-testid="toc"]');
const sb = root.querySelector('[data-testid="toc"]')!;
const firstTocItem = sb.querySelector('ol > li a, old > li summary') as HTMLElement;
const focusSpy = jest.spyOn(firstTocItem as any, 'focus');

renderer.act(() => {
reactDomAct(() => {
store.dispatch(actions.closeToc());
});
reactDomAct(() => {
sb?.dispatchEvent(new Event('transitionend'));
});

expect(focusSpy).not.toHaveBeenCalled();

reactDomAct(() => {
store.dispatch(actions.openToc());
});
reactDomAct(() => {
sb?.dispatchEvent(new Event('transitionend'));
});
expect(component.root.findByType(TableOfContents).props.isOpen).toBe(true);

expect(focusSpy).toHaveBeenCalled();
});

it('resets toc on navigate', () => {
Expand All @@ -113,7 +138,7 @@ describe('TableOfContents', () => {

const event = document.createEvent('UIEvents');
event.initEvent('scroll', true, false);
renderer.act(() => {
reactDomAct(() => {
assertWindow().dispatchEvent(event);
});

Expand Down
4 changes: 0 additions & 4 deletions src/app/content/components/TableOfContents/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ function TabTrapper({
return null;
}



// tslint:disable-next-line:variable-name
const SidebarBody = React.forwardRef<
HTMLElement,
Expand All @@ -79,8 +77,6 @@ const SidebarBody = React.forwardRef<
};

if (props.isTocOpen) {
// This is primarily for code coverage
transitionListener();
el?.addEventListener('transitionend', transitionListener);
}

Expand Down

0 comments on commit 70ae22b

Please sign in to comment.