File size: 1,852 Bytes
57c813c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ee0902
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
import { useState } from "react"
import { render, screen } from "@testing-library/react"
import userEvent from "@testing-library/user-event"
import { describe, it, expect, vi } from "vitest"
import VerifyForm from "../VerifyForm"
import { SAMPLE_CLAIMS } from "../../constants"

function ControlledVerifyForm({ initialClaim = "", onVerify = vi.fn(), onClear = vi.fn(), loading = false }) {
  const [claim, setClaim] = useState(initialClaim)
  return (
    <VerifyForm
      claim={claim}
      loading={loading}
      onClaimChange={setClaim}
      onVerify={onVerify}
      onClear={onClear}
    />
  )
}

describe("VerifyForm", () => {
  it("submits when Enter is pressed", async () => {
    const user = userEvent.setup()
    const onVerify = vi.fn()
    render(<ControlledVerifyForm initialClaim="Exercise lowers heart risk" onVerify={onVerify} />)

    const textarea = screen.getByLabelText(/health claim/i)
    await user.click(textarea)
    await user.keyboard("{Enter}")

    expect(onVerify).toHaveBeenCalledTimes(1)
  })

  it("inserts a newline without submitting on Shift+Enter", async () => {
    const user = userEvent.setup()
    const onVerify = vi.fn()
    render(<ControlledVerifyForm initialClaim="Line one" onVerify={onVerify} />)

    const textarea = screen.getByLabelText(/health claim/i)
    await user.click(textarea)
    await user.keyboard("{Shift>}{Enter}{/Shift}Line two")

    expect(onVerify).not.toHaveBeenCalled()
    expect(textarea).toHaveValue("Line one\nLine two")
  })

  it("fills the textarea when a sample chip is clicked", async () => {
    const user = userEvent.setup()
    render(<ControlledVerifyForm />)

    const firstSample = SAMPLE_CLAIMS[0]
    await user.click(screen.getByRole("button", { name: firstSample }))

    expect(screen.getByLabelText(/health claim/i)).toHaveValue(firstSample)
  })
})