docbrown/app/javascript/__tests__/convertCoauthorIdsToUsernameInputs.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

162 lines
4.9 KiB
JavaScript

import fetch from 'jest-fetch-mock';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';
import { convertCoauthorIdsToUsernameInputs } from '../packs/dashboards/convertCoauthorIdsToUsernameInputs';
global.fetch = fetch;
function fakeFetchResponseJSON() {
return JSON.stringify([
{ name: 'Alice', username: 'alice', id: 1 },
{ name: 'Bob', username: 'bob', id: 2 },
{ name: 'Charlie', username: 'charlie', id: 3 },
]);
}
describe('convertCoauthorIdsToUsernameInputs', () => {
beforeEach(() => {
global.Honeybadger = { notify: jest.fn() };
fetch.resetMocks();
fetch.mockResponse(fakeFetchResponseJSON());
});
describe('when there is no pre-existing id value', () => {
beforeEach(() => {
window.document.body.innerHTML = `
<form method="post">
<div class="crayons-field mb-4">
<label for="user_id">Author</label>
<select class="crayons-select" name="article[user_id]" id="article_user_id">
<option selected="selected" value="1">
Alice
</option>
<option value="2">
Bob
</option>
<option value="3">
Charlie
</option>
</select>
</div>
<div class="crayons-field mb-4">
<label for="co_author_ids">Co-authors</label>
<div class="crayons-field">
<input id="article_ID_co_author_ids_list"
class="article_org_co_author_ids_list"
name="article[co_author_ids_list]"
data-fetch-users="/org7053/members.json"
type="text"
value="" >
</div>
</div>
<button type="submit" class="crayons-btn">Save</button>
</form>
`;
});
it('calls fetch, with exception for author ID', async () => {
await convertCoauthorIdsToUsernameInputs();
expect(fetch).toHaveBeenCalledWith('/org7053/members.json');
});
it('makes the co-author field hidden', async () => {
await convertCoauthorIdsToUsernameInputs();
const co_author_field = document.getElementById(
'article_ID_co_author_ids_list',
);
expect(co_author_field.type).toBe('hidden');
});
it('renders matching snapshot', async () => {
await convertCoauthorIdsToUsernameInputs();
expect(document.forms[0].innerHTML).toMatchSnapshot();
});
it('works as expected', async () => {
await convertCoauthorIdsToUsernameInputs();
const input = document.querySelector(
"input[placeholder='Add up to 4...']",
);
input.focus();
await userEvent.type(input, 'Bob,');
const hiddenField = document.querySelector(
"input[name='article[co_author_ids_list]']",
);
expect(hiddenField.value).toBe('2');
});
});
describe('when there *is* a pre-existing id value', () => {
beforeEach(() => {
window.document.body.innerHTML = `
<form method="post">
<div class="crayons-field mb-4">
<label for="user_id">Author</label>
<select class="crayons-select" name="article[user_id]" id="article_user_id">
<option selected="selected" value="1">
Alice
</option>
<option value="2">
Bob
</option>
<option value="3">
Charlie
</option>
</select>
</div>
<div class="crayons-field mb-4">
<label for="co_author_ids">Co-authors</label>
<div class="crayons-field">
<input id="article_ID_co_author_ids_list"
class="article_org_co_author_ids_list"
name="article[co_author_ids_list]"
data-fetch-users="/org7053/members.json"
type="text"
value="3" >
</div>
</div>
<button type="submit" class="crayons-btn">Save</button>
</form>
`;
});
it('renders matching snapshot', async () => {
await convertCoauthorIdsToUsernameInputs();
expect(document.forms[0].innerHTML).toMatchSnapshot();
});
it('works as expected', async () => {
await convertCoauthorIdsToUsernameInputs();
const input = document.querySelector(
"input[placeholder='Add another...']",
);
input.focus();
await userEvent.type(input, 'Bob,');
const hiddenField = document.querySelector(
"input[name='article[co_author_ids_list]']",
);
expect(hiddenField.value).toBe('3, 2');
});
it('can remove previously selected', async () => {
await convertCoauthorIdsToUsernameInputs();
const deselect = document.querySelector(
'.c-autocomplete--multi__selected',
);
await userEvent.click(deselect);
const hiddenField = document.querySelector(
"input[name='article[co_author_ids_list]']",
);
expect(hiddenField.value).toBe('');
});
});
});