|
| 1 | +import fetch from 'jest-fetch-mock'; |
| 2 | +import '@testing-library/jest-dom'; |
| 3 | +import userEvent from '@testing-library/user-event'; |
| 4 | + |
| 5 | +import { convertCoauthorIdsToUsernameInputs } from '../packs/dashboards/convertCoauthorIdsToUsernameInputs'; |
| 6 | + |
| 7 | +global.fetch = fetch; |
| 8 | + |
| 9 | +function fakeFetchResponseJSON() { |
| 10 | + return JSON.stringify([ |
| 11 | + { name: 'Alice', username: 'alice', id: 1 }, |
| 12 | + { name: 'Bob', username: 'bob', id: 2 }, |
| 13 | + { name: 'Charlie', username: 'charlie', id: 3 }, |
| 14 | + ]); |
| 15 | +} |
| 16 | + |
| 17 | +describe('convertCoauthorIdsToUsernameInputs', () => { |
| 18 | + beforeEach(() => { |
| 19 | + global.Honeybadger = { notify: jest.fn() }; |
| 20 | + |
| 21 | + fetch.resetMocks(); |
| 22 | + fetch.mockResponse(fakeFetchResponseJSON()); |
| 23 | + }); |
| 24 | + |
| 25 | + describe('when there is no pre-existing id value', () => { |
| 26 | + beforeEach(() => { |
| 27 | + window.document.body.innerHTML = ` |
| 28 | + <form method="post"> |
| 29 | + <div class="crayons-field mb-4"> |
| 30 | + <label for="user_id">Author</label> |
| 31 | + <select class="crayons-select" name="article[user_id]" id="article_user_id"> |
| 32 | + <option selected="selected" value="1"> |
| 33 | + Alice |
| 34 | + </option> |
| 35 | + <option value="2"> |
| 36 | + Bob |
| 37 | + </option> |
| 38 | + <option value="3"> |
| 39 | + Charlie |
| 40 | + </option> |
| 41 | + </select> |
| 42 | + </div> |
| 43 | +
|
| 44 | + <div class="crayons-field mb-4"> |
| 45 | + <label for="co_author_ids">Co-authors</label> |
| 46 | + <div class="crayons-field"> |
| 47 | + <input id="article_ID_co_author_ids_list" |
| 48 | + class="article_org_co_author_ids_list" |
| 49 | + name="article[co_author_ids_list]" |
| 50 | + data-fetch-users="/org7053/members.json" |
| 51 | + type="text" |
| 52 | + value="" > |
| 53 | + </div> |
| 54 | + </div> |
| 55 | +
|
| 56 | + <button type="submit" class="crayons-btn">Save</button> |
| 57 | + </form> |
| 58 | + `; |
| 59 | + }); |
| 60 | + |
| 61 | + it('calls fetch, with exception for author ID', async () => { |
| 62 | + await convertCoauthorIdsToUsernameInputs(); |
| 63 | + expect(fetch).toHaveBeenCalledWith('/org7053/members.json'); |
| 64 | + }); |
| 65 | + |
| 66 | + it('makes the co-author field hidden', async () => { |
| 67 | + await convertCoauthorIdsToUsernameInputs(); |
| 68 | + const co_author_field = document.getElementById( |
| 69 | + 'article_ID_co_author_ids_list', |
| 70 | + ); |
| 71 | + expect(co_author_field.type).toBe('hidden'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('renders matching snapshot', async () => { |
| 75 | + await convertCoauthorIdsToUsernameInputs(); |
| 76 | + expect(document.forms[0].innerHTML).toMatchSnapshot(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('works as expected', async () => { |
| 80 | + await convertCoauthorIdsToUsernameInputs(); |
| 81 | + const input = document.querySelector( |
| 82 | + "input[placeholder='Add up to 4...']", |
| 83 | + ); |
| 84 | + input.focus(); |
| 85 | + await userEvent.type(input, 'Bob,'); |
| 86 | + |
| 87 | + const hiddenField = document.querySelector( |
| 88 | + "input[name='article[co_author_ids_list]']", |
| 89 | + ); |
| 90 | + expect(hiddenField.value).toBe('2'); |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('when there *is* a pre-existing id value', () => { |
| 95 | + beforeEach(() => { |
| 96 | + window.document.body.innerHTML = ` |
| 97 | + <form method="post"> |
| 98 | + <div class="crayons-field mb-4"> |
| 99 | + <label for="user_id">Author</label> |
| 100 | + <select class="crayons-select" name="article[user_id]" id="article_user_id"> |
| 101 | + <option selected="selected" value="1"> |
| 102 | + Alice |
| 103 | + </option> |
| 104 | + <option value="2"> |
| 105 | + Bob |
| 106 | + </option> |
| 107 | + <option value="3"> |
| 108 | + Charlie |
| 109 | + </option> |
| 110 | + </select> |
| 111 | + </div> |
| 112 | +
|
| 113 | + <div class="crayons-field mb-4"> |
| 114 | + <label for="co_author_ids">Co-authors</label> |
| 115 | + <div class="crayons-field"> |
| 116 | + <input id="article_ID_co_author_ids_list" |
| 117 | + class="article_org_co_author_ids_list" |
| 118 | + name="article[co_author_ids_list]" |
| 119 | + data-fetch-users="/org7053/members.json" |
| 120 | + type="text" |
| 121 | + value="3" > |
| 122 | + </div> |
| 123 | + </div> |
| 124 | +
|
| 125 | + <button type="submit" class="crayons-btn">Save</button> |
| 126 | + </form> |
| 127 | + `; |
| 128 | + }); |
| 129 | + |
| 130 | + it('renders matching snapshot', async () => { |
| 131 | + await convertCoauthorIdsToUsernameInputs(); |
| 132 | + expect(document.forms[0].innerHTML).toMatchSnapshot(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('works as expected', async () => { |
| 136 | + await convertCoauthorIdsToUsernameInputs(); |
| 137 | + const input = document.querySelector( |
| 138 | + "input[placeholder='Add another...']", |
| 139 | + ); |
| 140 | + input.focus(); |
| 141 | + await userEvent.type(input, 'Bob,'); |
| 142 | + |
| 143 | + const hiddenField = document.querySelector( |
| 144 | + "input[name='article[co_author_ids_list]']", |
| 145 | + ); |
| 146 | + expect(hiddenField.value).toBe('3, 2'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('can remove previously selected', async () => { |
| 150 | + await convertCoauthorIdsToUsernameInputs(); |
| 151 | + const deselect = document.querySelector( |
| 152 | + '.c-autocomplete--multi__selected', |
| 153 | + ); |
| 154 | + await userEvent.click(deselect); |
| 155 | + |
| 156 | + const hiddenField = document.querySelector( |
| 157 | + "input[name='article[co_author_ids_list]']", |
| 158 | + ); |
| 159 | + expect(hiddenField.value).toBe(''); |
| 160 | + }); |
| 161 | + }); |
| 162 | +}); |
0 commit comments