-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
707: Added unit tests for ContextMenu Component and fixed code to get…
… them to pass
- Loading branch information
Showing
9 changed files
with
928 additions
and
805 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
lib/platform-bible-react/src/components/contextMenu.component.closed.render.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { PlatformMenus } from 'platform-bible-utils'; | ||
import { render, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import ContextMenu from './context-menu.component'; | ||
import NonValidatingDocumentCombiner from '../test-utils/non-validating-document-combiner'; | ||
import * as jsonMenu from './sample.composed.full.menu.json'; | ||
|
||
describe('ContextMenu', () => { | ||
const topMenuCombiner = new NonValidatingDocumentCombiner(jsonMenu, { | ||
copyDocuments: false, | ||
ignoreDuplicateProperties: true, | ||
}); | ||
|
||
// Assert the type that schema validation should have already sorted out | ||
// eslint-disable-next-line no-type-assertion/no-type-assertion | ||
const menuData = topMenuCombiner.output as PlatformMenus; | ||
render(<ContextMenu | ||
menuDefinition={menuData.defaultWebViewContextMenu} | ||
commandHandler={() => {}} > | ||
click me | ||
</ContextMenu>); | ||
|
||
it('renders no menu items before context menu is displayed', () => { | ||
console.log(screen.debug()); | ||
expect(screen.queryAllByRole('menuitem').length).toBe(0); | ||
}); | ||
}); |
79 changes: 79 additions & 0 deletions
79
lib/platform-bible-react/src/components/contextMenu.component.open.render.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { PlatformMenus } from 'platform-bible-utils'; | ||
import { fireEvent, render, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import { PropsWithChildren } from 'react'; | ||
import ContextMenu from './context-menu.component'; | ||
import NonValidatingDocumentCombiner from '../test-utils/non-validating-document-combiner'; | ||
import * as jsonMenu from './sample.composed.full.menu.json'; | ||
|
||
jest.mock('@mui/material', () => { | ||
const mui = jest.requireActual('@mui/material'); // Import the actual MUI components | ||
|
||
return { | ||
...mui, // Spread the actual MUI exports | ||
|
||
// Mocked components | ||
MenuItem: ({ | ||
divider, | ||
className, | ||
children, | ||
}: { | ||
divider: boolean | undefined; | ||
className: string | undefined; | ||
} & PropsWithChildren) => { | ||
const dividerStyle = divider ? ' hasDivider' : ''; | ||
return ( | ||
<li className={`${className || ''}${dividerStyle}`} role="menuitem"> | ||
{children} | ||
</li> | ||
); | ||
}, | ||
}; | ||
}); | ||
|
||
describe('ContextMenu renders', () => { | ||
const topMenuCombiner = new NonValidatingDocumentCombiner(jsonMenu, { | ||
copyDocuments: false, | ||
ignoreDuplicateProperties: true, | ||
}); | ||
|
||
// Assert the type that schema validation should have already sorted out | ||
// eslint-disable-next-line no-type-assertion/no-type-assertion | ||
const menuData = topMenuCombiner.output as PlatformMenus; | ||
render(<ContextMenu | ||
menuDefinition={menuData.defaultWebViewContextMenu} | ||
commandHandler={() => {}} > | ||
click me | ||
</ContextMenu>); | ||
|
||
const contextMenuTarget = screen.getByText('click me'); | ||
fireEvent.contextMenu(contextMenuTarget); | ||
|
||
console.log(screen.debug()); | ||
|
||
const allMenuItems = screen.queryAllByRole('menuitem'); | ||
|
||
it('the correct total number of items', () => { | ||
expect(allMenuItems.length).toBe(2); | ||
}); | ||
|
||
it('the correct total number of groups with dividers', () => { | ||
let cItemsWithDividers = 0; | ||
allMenuItems.forEach((m) => { | ||
if (m.outerHTML?.match('hasDivider')) cItemsWithDividers += 1; | ||
}); | ||
|
||
// In the test data, there are two groups in the context menu, so there should be a divider | ||
// between them. | ||
expect(cItemsWithDividers).toBe(1); | ||
}); | ||
|
||
it('the last group in a column without a final separator', () => { | ||
const htmlForWordListItem = allMenuItems | ||
.map((i) => i.outerHTML) | ||
.find((html) => html && /%wordList%/.test(html)); | ||
|
||
expect(htmlForWordListItem).toBeDefined(); | ||
expect(htmlForWordListItem).not.toMatch('hasDivider'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters