add-email-input.test.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. import { fireEvent, render, screen, waitFor } from '@testing-library/react'
  2. import { expect } from 'chai'
  3. import sinon from 'sinon'
  4. import fetchMock from 'fetch-mock'
  5. import Input, {
  6. clearDomainCache,
  7. } from '../../../../../../frontend/js/features/settings/components/emails/add-email/input'
  8. import domainBlocklist from '../../../../../../frontend/js/features/settings/domain-blocklist'
  9. const testInstitutionData = [
  10. { university: { id: 124 }, hostname: 'domain.edu' },
  11. ]
  12. describe('<AddEmailInput/>', function () {
  13. const defaultProps = {
  14. onChange: (value: string) => {},
  15. handleAddNewEmail: () => {},
  16. }
  17. beforeEach(function () {
  18. clearDomainCache()
  19. fetchMock.reset()
  20. })
  21. describe('on initial render', function () {
  22. it('should render an input with a placeholder', function () {
  23. render(<Input {...defaultProps} />)
  24. screen.getByPlaceholderText('e.g. johndoe@mit.edu')
  25. })
  26. it('should not dispatch any `change` event', function () {
  27. const onChangeStub = sinon.stub()
  28. render(<Input {...defaultProps} onChange={onChangeStub} />)
  29. expect(onChangeStub.called).to.equal(false)
  30. })
  31. })
  32. describe('when typing text that does not contain any potential domain match', function () {
  33. let onChangeStub: sinon.SinonStub
  34. let handleAddNewEmailStub: sinon.SinonStub
  35. beforeEach(function () {
  36. fetchMock.get('express:/institutions/domains', 200)
  37. onChangeStub = sinon.stub()
  38. handleAddNewEmailStub = sinon.stub()
  39. render(
  40. <Input
  41. {...defaultProps}
  42. onChange={onChangeStub}
  43. handleAddNewEmail={handleAddNewEmailStub}
  44. />
  45. )
  46. fireEvent.change(screen.getByTestId('affiliations-email'), {
  47. target: { value: 'user' },
  48. })
  49. })
  50. it('should render the text being typed', function () {
  51. const input = screen.getByTestId('affiliations-email') as HTMLInputElement
  52. expect(input.value).to.equal('user')
  53. })
  54. it('should dispatch a `change` event on every stroke', function () {
  55. expect(onChangeStub.calledWith('user')).to.equal(true)
  56. fireEvent.change(screen.getByTestId('affiliations-email'), {
  57. target: { value: 's' },
  58. })
  59. expect(onChangeStub.calledWith('s')).to.equal(true)
  60. })
  61. it('should not make any request for institution domains', function () {
  62. expect(fetchMock.called()).to.be.false
  63. })
  64. it('should submit on Enter if email looks valid', async function () {
  65. fireEvent.change(screen.getByTestId('affiliations-email'), {
  66. target: { value: 'user@domain.com' },
  67. })
  68. fireEvent.keyDown(screen.getByTestId('affiliations-email'), {
  69. key: 'Enter',
  70. })
  71. expect(handleAddNewEmailStub.calledWith()).to.equal(true)
  72. })
  73. it('should not submit on Enter if email does not look valid', async function () {
  74. fireEvent.change(screen.getByTestId('affiliations-email'), {
  75. target: { value: 'user@' },
  76. })
  77. fireEvent.keyDown(screen.getByTestId('affiliations-email'), {
  78. key: 'Enter',
  79. })
  80. expect(handleAddNewEmailStub.calledWith()).to.equal(false)
  81. })
  82. })
  83. describe('when typing text that contains a potential domain match', function () {
  84. let onChangeStub: sinon.SinonStub
  85. beforeEach(function () {
  86. onChangeStub = sinon.stub()
  87. render(<Input {...defaultProps} onChange={onChangeStub} />)
  88. })
  89. describe('when there are no matches', function () {
  90. beforeEach(function () {
  91. fetchMock.get('express:/institutions/domains', 200)
  92. fireEvent.change(screen.getByTestId('affiliations-email'), {
  93. target: { value: 'user@d' },
  94. })
  95. })
  96. it('should render the text being typed', function () {
  97. const input = screen.getByTestId(
  98. 'affiliations-email'
  99. ) as HTMLInputElement
  100. expect(input.value).to.equal('user@d')
  101. })
  102. })
  103. describe('when there is a domain match', function () {
  104. beforeEach(function () {
  105. fetchMock.get('express:/institutions/domains', testInstitutionData)
  106. fireEvent.change(screen.getByTestId('affiliations-email'), {
  107. target: { value: 'user@d' },
  108. })
  109. })
  110. it('should render the text being typed along with the suggestion', async function () {
  111. const input = screen.getByTestId(
  112. 'affiliations-email'
  113. ) as HTMLInputElement
  114. expect(input.value).to.equal('user@d')
  115. await waitFor(() => {
  116. const shadowInput = screen.getByTestId(
  117. 'affiliations-email-shadow'
  118. ) as HTMLInputElement
  119. expect(shadowInput.value).to.equal('user@domain.edu')
  120. })
  121. })
  122. it('should dispatch a `change` event with the typed text', function () {
  123. expect(onChangeStub.calledWith('user@d')).to.equal(true)
  124. })
  125. it('should dispatch a `change` event with institution data when the typed email contains the institution domain', async function () {
  126. fireEvent.change(screen.getByTestId('affiliations-email'), {
  127. target: { value: 'user@domain.edu' },
  128. })
  129. await fetchMock.flush(true)
  130. expect(
  131. onChangeStub.calledWith(
  132. 'user@domain.edu',
  133. sinon.match(testInstitutionData[0])
  134. )
  135. ).to.equal(true)
  136. })
  137. it('should clear the suggestion when the potential domain match is completely deleted', async function () {
  138. await waitFor(() => {
  139. const shadowInput = screen.getByTestId(
  140. 'affiliations-email-shadow'
  141. ) as HTMLInputElement
  142. expect(shadowInput.value).to.equal('user@domain.edu')
  143. })
  144. fireEvent.change(screen.getByTestId('affiliations-email'), {
  145. target: { value: '' },
  146. })
  147. expect(screen.queryByText('user@domain.edu')).to.be.null
  148. })
  149. describe('when there is a suggestion and "Tab" key is pressed', function () {
  150. beforeEach(async function () {
  151. // wait until autocompletion available
  152. await waitFor(() => {
  153. const shadowInput = screen.getByTestId(
  154. 'affiliations-email-shadow'
  155. ) as HTMLInputElement
  156. expect(shadowInput.value).to.equal('user@domain.edu')
  157. })
  158. fireEvent.keyDown(screen.getByTestId('affiliations-email'), {
  159. key: 'Tab',
  160. })
  161. })
  162. it('it should autocomplete the input', async function () {
  163. const input = screen.getByTestId(
  164. 'affiliations-email'
  165. ) as HTMLInputElement
  166. expect(input.value).to.equal('user@domain.edu')
  167. })
  168. it('should dispatch a `change` event with the domain matched', async function () {
  169. expect(
  170. onChangeStub.calledWith(
  171. 'user@domain.edu',
  172. sinon.match(testInstitutionData[0])
  173. )
  174. ).to.equal(true)
  175. })
  176. })
  177. describe('when there is a suggestion and "Enter" key is pressed', function () {
  178. beforeEach(async function () {
  179. // wait until autocompletion available
  180. await waitFor(() => {
  181. const shadowInput = screen.getByTestId(
  182. 'affiliations-email-shadow'
  183. ) as HTMLInputElement
  184. expect(shadowInput.value).to.equal('user@domain.edu')
  185. })
  186. fireEvent.keyDown(screen.getByTestId('affiliations-email'), {
  187. key: 'Enter',
  188. })
  189. })
  190. it('it should autocomplete the input', async function () {
  191. const input = screen.getByTestId(
  192. 'affiliations-email'
  193. ) as HTMLInputElement
  194. expect(input.value).to.equal('user@domain.edu')
  195. })
  196. it('should dispatch a `change` event with the domain matched', async function () {
  197. expect(
  198. onChangeStub.calledWith(
  199. 'user@domain.edu',
  200. sinon.match(testInstitutionData[0])
  201. )
  202. ).to.equal(true)
  203. })
  204. })
  205. it('should cache the result and skip subsequent requests', async function () {
  206. fetchMock.reset()
  207. // clear input
  208. fireEvent.change(screen.getByTestId('affiliations-email'), {
  209. target: { value: '' },
  210. })
  211. // type a hint to trigger the domain search
  212. fireEvent.change(screen.getByTestId('affiliations-email'), {
  213. target: { value: 'user@d' },
  214. })
  215. expect(fetchMock.called()).to.be.false
  216. expect(onChangeStub.calledWith('user@d')).to.equal(true)
  217. await waitFor(() => {
  218. const shadowInput = screen.getByTestId(
  219. 'affiliations-email-shadow'
  220. ) as HTMLInputElement
  221. expect(shadowInput.value).to.equal('user@domain.edu')
  222. })
  223. })
  224. })
  225. describe('when there is a match for a blocklisted domain', function () {
  226. const [blockedDomain] = domainBlocklist
  227. afterEach(function () {
  228. clearDomainCache()
  229. fetchMock.reset()
  230. })
  231. it('should not render the suggestion with blocked domain', async function () {
  232. const blockedInstitution = [
  233. { university: { id: 1 }, hostname: blockedDomain },
  234. ]
  235. fetchMock.get('express:/institutions/domains', blockedInstitution)
  236. fireEvent.change(screen.getByTestId('affiliations-email'), {
  237. target: { value: `user@${blockedDomain.split('.')[0]}` },
  238. })
  239. await fetchMock.flush(true)
  240. expect(screen.queryByText(`user@${blockedDomain}`)).to.be.null
  241. })
  242. it('should not render the suggestion with blocked domain having a subdomain', async function () {
  243. const blockedInstitution = [
  244. {
  245. university: { id: 1 },
  246. hostname: `subdomain.${blockedDomain}`,
  247. },
  248. ]
  249. fetchMock.get('express:/institutions/domains', blockedInstitution)
  250. fireEvent.change(screen.getByTestId('affiliations-email'), {
  251. target: {
  252. value: `user@subdomain.${blockedDomain.split('.')[0]}`,
  253. },
  254. })
  255. await fetchMock.flush(true)
  256. expect(screen.queryByText(`user@subdomain.${blockedDomain}`)).to.be.null
  257. })
  258. })
  259. describe('while waiting for a response', function () {
  260. beforeEach(async function () {
  261. // type an initial suggestion
  262. fetchMock.get('express:/institutions/domains', testInstitutionData)
  263. fireEvent.change(screen.getByTestId('affiliations-email'), {
  264. target: { value: 'user@d' },
  265. })
  266. await waitFor(() => {
  267. const shadowInput = screen.getByTestId(
  268. 'affiliations-email-shadow'
  269. ) as HTMLInputElement
  270. expect(shadowInput.value).to.equal('user@domain.edu')
  271. })
  272. // make sure the next suggestions are delayed
  273. clearDomainCache()
  274. fetchMock.reset()
  275. fetchMock.get('express:/institutions/domains', 200, { delay: 1000 })
  276. })
  277. it('should keep the suggestion if the hint matches the previously matched domain', async function () {
  278. fireEvent.change(screen.getByTestId('affiliations-email'), {
  279. target: { value: 'user@do' },
  280. })
  281. const shadowInput = screen.getByTestId(
  282. 'affiliations-email-shadow'
  283. ) as HTMLInputElement
  284. expect(shadowInput.value).to.equal('user@domain.edu')
  285. })
  286. it('should remove the suggestion if the hint does not match the previously matched domain', async function () {
  287. fireEvent.change(screen.getByTestId('affiliations-email'), {
  288. target: { value: 'user@foo' },
  289. })
  290. await waitFor(() => {
  291. const shadowInput = screen.getByTestId(
  292. 'affiliations-email-shadow'
  293. ) as HTMLInputElement
  294. expect(shadowInput.value).to.equal('')
  295. })
  296. })
  297. })
  298. })
  299. describe('when the request to fetch institution domains fail', function () {
  300. let onChangeStub
  301. beforeEach(async function () {
  302. // initial request populates the suggestion
  303. fetchMock.get('express:/institutions/domains', testInstitutionData)
  304. onChangeStub = sinon.stub()
  305. render(<Input {...defaultProps} onChange={onChangeStub} />)
  306. fireEvent.change(screen.getByTestId('affiliations-email'), {
  307. target: { value: 'user@d' },
  308. })
  309. await waitFor(() => {
  310. const shadowInput = screen.getByTestId(
  311. 'affiliations-email-shadow'
  312. ) as HTMLInputElement
  313. expect(shadowInput.value).to.equal('user@domain.edu')
  314. })
  315. // subsequent requests fail
  316. fetchMock.reset()
  317. fetchMock.get('express:/institutions/domains', 500)
  318. })
  319. it('should clear suggestions', async function () {
  320. fireEvent.change(screen.getByTestId('affiliations-email'), {
  321. target: { value: 'user@dom' },
  322. })
  323. const input = screen.getByTestId('affiliations-email') as HTMLInputElement
  324. expect(input.value).to.equal('user@dom')
  325. await waitFor(() => {
  326. const shadowInput = screen.getByTestId(
  327. 'affiliations-email-shadow'
  328. ) as HTMLInputElement
  329. expect(shadowInput.value).to.equal('')
  330. })
  331. expect(fetchMock.called()).to.be.true // ensures `domainCache` hasn't been hit
  332. })
  333. })
  334. describe('when the request to fetch institution is not matching input', function () {
  335. it('should clear suggestion', async function () {
  336. fetchMock.get('express:/institutions/domains', testInstitutionData)
  337. render(<Input {...defaultProps} onChange={sinon.stub()} />)
  338. fireEvent.change(screen.getByTestId('affiliations-email'), {
  339. target: { value: 'user@other' },
  340. })
  341. await fetchMock.flush(true)
  342. const shadowInput = screen.getByTestId(
  343. 'affiliations-email-shadow'
  344. ) as HTMLInputElement
  345. expect(shadowInput.value).to.equal('')
  346. })
  347. })
  348. })