aboutsummaryrefslogtreecommitdiff
path: root/tests/signup.spec.ts
blob: 1317049d8c7b4f67047895a61aa6cc89519a5a26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import {
  fireEvent,
  waitFor,
  getByText,
  getByTestId,
} from '@testing-library/dom'
import '@testing-library/jest-dom'
import { delay, loadHTMLFixture } from './util'

describe('signup example', () => {
  beforeEach(async () => {
    await loadHTMLFixture('signup')
    window.fetch = jest.fn() as any
  })

  it('should show form when button is clicked', async () => {
    const $showFormBtn = document.getElementById('show-form-btn')!
    const $form = document.getElementById('signup-form')!

    expect($showFormBtn).toBeVisible()
    expect($showFormBtn.nodeName).toBe('BUTTON')
    expect($form).not.toBeVisible()
    expect($form.nodeName).toBe('FORM')

    // Click and wait for button to get hidden class (handles delay)
    fireEvent.click($showFormBtn)

    await waitFor(() => expect($showFormBtn).not.toBeVisible())
    expect($form).toBeVisible()
  })

  describe('Form submit', () => {
    beforeEach(async () => {
      const $showFormBtn = document.getElementById('show-form-btn')!
      fireEvent.click($showFormBtn)
      await waitFor(() => expect($showFormBtn).not.toBeVisible())
      const $form = document.getElementById('signup-form')!
      expect($form).toBeVisible()
      await delay(100) // Wait for mounting
    })

    it('should submit form correctly', async () => {
      const $form = document.getElementById('signup-form')!

      // Set email and password field
      const $email = getByTestId<HTMLInputElement>(document.body, 'email')
      $email.value = 'no-reply@we-love-replies.com'
      const $password = getByTestId<HTMLInputElement>(document.body, 'password')
      $password.value = 'password'

      await delay(2000)

      // Submit form
      const $submitBtn = getByText(document.body, 'Submit')
      fireEvent.click($submitBtn)

      // Should add submitting class to form
      await waitFor(() =>
        expect($form.classList.contains('submitting')).toBe(true),
      )
      expect($submitBtn).toBeDisabled()

      // Should add submitted class to form and remove submitting
      await waitFor(() =>
        expect($form.classList.contains('submitted')).toBe(true),
      )
      expect($form.classList.contains('submitting')).toBe(false)
      expect($submitBtn).toBeEnabled()

      // Should have made a request to post form data
      expect(window.fetch).toHaveBeenCalledTimes(1)
      const [url, { method, body }] = (window.fetch as any).mock.calls[0]
      expect(url).toBe('http://example.com/submit/api')
      expect(method).toBe('POST')
      expect(Object.fromEntries(body.entries())).toEqual({
        email: 'no-reply@we-love-replies.com',
        password: 'password',
      })
    })
  })
})