docbrown/app/javascript/shared/components/__tests__/UsernameInput.test.js
Joshua Wehner 6fe9d7d7a6
Allow org admins to add members as co-authors (#20008)
* Basics might be working?

* Stop propagating button clicks in autocomplete pills

* Better blank slate

* Better location to stop propagation

* Remove author_id from org co-authors

* Move UserStore and try testing it

* Remove extraneous comments

* OH... that's what that does!

* Very basic testing

* Re-organize javascripts

* Rename & re-org for testing

* Cleanup

* More tests

* Remove unnecessary nesting

* Coninuing to try to bump coverage

* Include /packs/ in code coverage metric

* Try tweaking jest coverage more?

We probably can't collect coverage from all of packs/* (because coverage is too low) but maybe we can try to opt-in for newer areas as we go?

* Relocate JS tests, for build & coverage

* User ID exception on search, not fetch

* Remove commented-out console.log

---------

Co-authored-by: Mac Siri <krairit.siri@gmail.com>
2023-09-15 08:12:41 -04:00

49 lines
1.4 KiB
JavaScript

import { h } from 'preact';
import { render } from '@testing-library/preact';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import { UsernameInput } from '../UsernameInput';
function fakeUsers() {
return [
{ name: 'Alice', username: 'alice', id: 1 },
{ name: 'Bob', username: 'bob', id: 2 },
{ name: 'Charlie', username: 'charlie', id: 3 },
{ name: 'Almost Alice', username: 'almostalice', id: 4 },
];
}
describe('<UsernameInput />', () => {
it('renders the default component', () => {
const { container } = render(
<UsernameInput
labelText="Example label"
fetchSuggestions={() => {}}
handleSelectionsChanged={() => {}}
/>,
);
expect(container.innerHTML).toMatchSnapshot();
});
it('calls handleSelectionsChanged with user IDs', async () => {
const fakeHandler = jest.fn();
const { getByLabelText, queryByRole } = render(
<UsernameInput
labelText="Enter username"
fetchSuggestions={fakeUsers}
handleSelectionsChanged={fakeHandler}
/>,
);
const input = getByLabelText('Enter username');
input.focus();
await userEvent.type(input, 'Bob,');
await userEvent.type(input, 'Charlie,');
expect(queryByRole('button', { name: 'Edit example' })).toBeNull();
expect(fakeHandler).toHaveBeenCalledWith('2, 3');
});
});