add-email-input.test.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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.removeRoutes().clearHistory()
  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.callHistory.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(async function () {
  105. fetchMock.get('express:/institutions/domains', testInstitutionData)
  106. fireEvent.change(screen.getByTestId('affiliations-email'), {
  107. target: { value: 'user@d' },
  108. })
  109. // Wait for component to process the change and update the shadow input
  110. await waitFor(() => {
  111. const shadowInput = screen.getByTestId(
  112. 'affiliations-email-shadow'
  113. ) as HTMLInputElement
  114. expect(shadowInput.value).to.equal('user@domain.edu')
  115. })
  116. })
  117. it('should render the text being typed along with the suggestion', async function () {
  118. const input = screen.getByTestId(
  119. 'affiliations-email'
  120. ) as HTMLInputElement
  121. expect(input.value).to.equal('user@d')
  122. await waitFor(() => {
  123. const shadowInput = screen.getByTestId(
  124. 'affiliations-email-shadow'
  125. ) as HTMLInputElement
  126. expect(shadowInput.value).to.equal('user@domain.edu')
  127. })
  128. })
  129. it('should dispatch a `change` event with the typed text', function () {
  130. expect(onChangeStub.calledWith('user@d')).to.equal(true)
  131. })
  132. it('should dispatch a `change` event with institution data when the typed email contains the institution domain', async function () {
  133. fireEvent.change(screen.getByTestId('affiliations-email'), {
  134. target: { value: 'user@domain.edu' },
  135. })
  136. await fetchMock.callHistory.flush(true)
  137. expect(
  138. onChangeStub.calledWith(
  139. 'user@domain.edu',
  140. sinon.match(testInstitutionData[0])
  141. )
  142. ).to.equal(true)
  143. })
  144. it('should clear the suggestion when the potential domain match is completely deleted', async function () {
  145. await waitFor(() => {
  146. const shadowInput = screen.getByTestId(
  147. 'affiliations-email-shadow'
  148. ) as HTMLInputElement
  149. expect(shadowInput.value).to.equal('user@domain.edu')
  150. })
  151. fireEvent.change(screen.getByTestId('affiliations-email'), {
  152. target: { value: '' },
  153. })
  154. expect(screen.queryByText('user@domain.edu')).to.be.null
  155. })
  156. describe('when there is a suggestion and "Tab" key is pressed', function () {
  157. beforeEach(async function () {
  158. // wait until autocompletion available
  159. await waitFor(() => {
  160. const shadowInput = screen.getByTestId(
  161. 'affiliations-email-shadow'
  162. ) as HTMLInputElement
  163. expect(shadowInput.value).to.equal('user@domain.edu')
  164. })
  165. fireEvent.keyDown(screen.getByTestId('affiliations-email'), {
  166. key: 'Tab',
  167. })
  168. })
  169. it('it should autocomplete the input', async function () {
  170. const input = screen.getByTestId(
  171. 'affiliations-email'
  172. ) as HTMLInputElement
  173. expect(input.value).to.equal('user@domain.edu')
  174. })
  175. it('should dispatch a `change` event with the domain matched', async function () {
  176. expect(
  177. onChangeStub.calledWith(
  178. 'user@domain.edu',
  179. sinon.match(testInstitutionData[0])
  180. )
  181. ).to.equal(true)
  182. })
  183. })
  184. describe('when there is a suggestion and "Enter" key is pressed', function () {
  185. beforeEach(async function () {
  186. // wait until autocompletion available
  187. await waitFor(() => {
  188. const shadowInput = screen.getByTestId(
  189. 'affiliations-email-shadow'
  190. ) as HTMLInputElement
  191. expect(shadowInput.value).to.equal('user@domain.edu')
  192. })
  193. fireEvent.keyDown(screen.getByTestId('affiliations-email'), {
  194. key: 'Enter',
  195. })
  196. })
  197. it('it should autocomplete the input', async function () {
  198. const input = screen.getByTestId(
  199. 'affiliations-email'
  200. ) as HTMLInputElement
  201. expect(input.value).to.equal('user@domain.edu')
  202. })
  203. it('should dispatch a `change` event with the domain matched', async function () {
  204. expect(
  205. onChangeStub.calledWith(
  206. 'user@domain.edu',
  207. sinon.match(testInstitutionData[0])
  208. )
  209. ).to.equal(true)
  210. })
  211. })
  212. it('should cache the result and skip subsequent requests', async function () {
  213. fetchMock.removeRoutes().clearHistory()
  214. // clear input
  215. fireEvent.change(screen.getByTestId('affiliations-email'), {
  216. target: { value: '' },
  217. })
  218. // type a hint to trigger the domain search
  219. fireEvent.change(screen.getByTestId('affiliations-email'), {
  220. target: { value: 'user@d' },
  221. })
  222. expect(fetchMock.callHistory.called()).to.be.false
  223. expect(onChangeStub.calledWith('user@d')).to.equal(true)
  224. await waitFor(() => {
  225. const shadowInput = screen.getByTestId(
  226. 'affiliations-email-shadow'
  227. ) as HTMLInputElement
  228. expect(shadowInput.value).to.equal('user@domain.edu')
  229. })
  230. })
  231. })
  232. describe('when there is a match for a blocklisted domain', function () {
  233. const [blockedDomain] = domainBlocklist
  234. afterEach(function () {
  235. clearDomainCache()
  236. fetchMock.removeRoutes().clearHistory()
  237. })
  238. it('should not render the suggestion with blocked domain', async function () {
  239. const blockedInstitution = [
  240. { university: { id: 1 }, hostname: blockedDomain },
  241. ]
  242. fetchMock.get('express:/institutions/domains', blockedInstitution)
  243. fireEvent.change(screen.getByTestId('affiliations-email'), {
  244. target: { value: `user@${blockedDomain.split('.')[0]}` },
  245. })
  246. await fetchMock.callHistory.flush(true)
  247. expect(screen.queryByText(`user@${blockedDomain}`)).to.be.null
  248. })
  249. it('should not render the suggestion with blocked domain having a subdomain', async function () {
  250. const blockedInstitution = [
  251. {
  252. university: { id: 1 },
  253. hostname: `subdomain.${blockedDomain}`,
  254. },
  255. ]
  256. fetchMock.get('express:/institutions/domains', blockedInstitution)
  257. fireEvent.change(screen.getByTestId('affiliations-email'), {
  258. target: {
  259. value: `user@subdomain.${blockedDomain.split('.')[0]}`,
  260. },
  261. })
  262. await fetchMock.callHistory.flush(true)
  263. expect(screen.queryByText(`user@subdomain.${blockedDomain}`)).to.be.null
  264. })
  265. })
  266. describe('while waiting for a response', function () {
  267. beforeEach(async function () {
  268. // type an initial suggestion
  269. fetchMock.get('express:/institutions/domains', testInstitutionData)
  270. fireEvent.change(screen.getByTestId('affiliations-email'), {
  271. target: { value: 'user@d' },
  272. })
  273. await waitFor(() => {
  274. const shadowInput = screen.getByTestId(
  275. 'affiliations-email-shadow'
  276. ) as HTMLInputElement
  277. expect(shadowInput.value).to.equal('user@domain.edu')
  278. })
  279. // make sure the next suggestions are delayed
  280. clearDomainCache()
  281. fetchMock.removeRoutes().clearHistory()
  282. fetchMock.get('express:/institutions/domains', 200, { delay: 1000 })
  283. })
  284. it('should keep the suggestion if the hint matches the previously matched domain', async function () {
  285. fireEvent.change(screen.getByTestId('affiliations-email'), {
  286. target: { value: 'user@do' },
  287. })
  288. const shadowInput = screen.getByTestId(
  289. 'affiliations-email-shadow'
  290. ) as HTMLInputElement
  291. expect(shadowInput.value).to.equal('user@domain.edu')
  292. })
  293. it('should remove the suggestion if the hint does not match the previously matched domain', async function () {
  294. fireEvent.change(screen.getByTestId('affiliations-email'), {
  295. target: { value: 'user@foo' },
  296. })
  297. await waitFor(() => {
  298. const shadowInput = screen.getByTestId(
  299. 'affiliations-email-shadow'
  300. ) as HTMLInputElement
  301. expect(shadowInput.value).to.equal('')
  302. })
  303. })
  304. })
  305. })
  306. describe('when the request to fetch institution domains fail', function () {
  307. let onChangeStub
  308. beforeEach(async function () {
  309. // initial request populates the suggestion
  310. fetchMock.get('express:/institutions/domains', testInstitutionData)
  311. onChangeStub = sinon.stub()
  312. render(<Input {...defaultProps} onChange={onChangeStub} />)
  313. fireEvent.change(screen.getByTestId('affiliations-email'), {
  314. target: { value: 'user@d' },
  315. })
  316. await waitFor(() => {
  317. const shadowInput = screen.getByTestId(
  318. 'affiliations-email-shadow'
  319. ) as HTMLInputElement
  320. expect(shadowInput.value).to.equal('user@domain.edu')
  321. })
  322. // subsequent requests fail
  323. fetchMock.removeRoutes().clearHistory()
  324. fetchMock.get('express:/institutions/domains', 500)
  325. })
  326. it('should clear suggestions', async function () {
  327. fireEvent.change(screen.getByTestId('affiliations-email'), {
  328. target: { value: 'user@dom' },
  329. })
  330. const input = screen.getByTestId('affiliations-email') as HTMLInputElement
  331. expect(input.value).to.equal('user@dom')
  332. await waitFor(() => {
  333. const shadowInput = screen.getByTestId(
  334. 'affiliations-email-shadow'
  335. ) as HTMLInputElement
  336. expect(shadowInput.value).to.equal('')
  337. })
  338. expect(fetchMock.callHistory.called()).to.be.true // ensures `domainCache` hasn't been hit
  339. })
  340. })
  341. describe('when the request to fetch institution is not matching input', function () {
  342. it('should clear suggestion', async function () {
  343. fetchMock.get('express:/institutions/domains', testInstitutionData)
  344. render(<Input {...defaultProps} onChange={sinon.stub()} />)
  345. fireEvent.change(screen.getByTestId('affiliations-email'), {
  346. target: { value: 'user@other' },
  347. })
  348. await fetchMock.callHistory.flush(true)
  349. const shadowInput = screen.getByTestId(
  350. 'affiliations-email-shadow'
  351. ) as HTMLInputElement
  352. expect(shadowInput.value).to.equal('')
  353. })
  354. })
  355. })