Add test for defaultPrevented

Signed-off-by: Johan Hammar <johan.hammar@gmail.com>
This commit is contained in:
Johan Hammar
2021-10-06 20:14:12 +02:00
parent ebb2886122
commit 3539f88c6d
@@ -16,12 +16,12 @@
import React from 'react';
import { renderInTestApp } from '@backstage/test-utils';
import { screen } from '@testing-library/react';
import { createEvent, fireEvent, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import HomeIcon from '@material-ui/icons/Home';
import CreateComponentIcon from '@material-ui/icons/AddCircleOutline';
import { Sidebar } from './Bar';
import { SidebarItem } from './Items';
import { SidebarItem, SidebarSearchField } from './Items';
import { renderHook } from '@testing-library/react-hooks';
import { hexToRgb, makeStyles } from '@material-ui/core';
@@ -36,6 +36,7 @@ async function renderSidebar() {
await renderInTestApp(
<Sidebar>
<SidebarSearchField onSearch={() => {}} to="/search" />
<SidebarItem text="Home" icon={HomeIcon} to="./" />
<SidebarItem
icon={CreateComponentIcon}
@@ -72,4 +73,14 @@ describe('Items', () => {
).toHaveStyle(`background-color: ${hexToRgb('2b2a2a')}`);
});
});
describe('SidebarSearchField', () => {
it('should be defaultPrevented when enter is pressed', async () => {
const searchEvent = createEvent.keyDown(
await screen.findByPlaceholderText('Search'),
{ key: 'Enter', code: 'Enter', charCode: 13 },
);
fireEvent(await screen.findByPlaceholderText('Search'), searchEvent);
expect(searchEvent.defaultPrevented).toBeTruthy();
});
});
});