All files / components/ChatFilterWords ChatFilterWordForm.jsx

100% Statements 7/7
100% Branches 3/3
100% Functions 4/4
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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        2x                       49x           49x   49x                       6x                                        
import { Button, Form, InputGroup } from "react-bootstrap";
import { useForm } from "react-hook-form";
 
// Matches the VARCHAR(255) WORD column on the backend.
export const MAX_WORD_LENGTH = 255;
 
export default function ChatFilterWordForm({
  submitAction,
  testIdPrefix = "ChatFilterWordForm",
}) {
  // Stryker disable all
  const {
    register,
    formState: { errors },
    handleSubmit,
    reset,
  } = useForm({ defaultValues: { word: "" } });
  // Stryker restore all
 
  // submitAction receives a callback to clear the input, so the page can
  // clear it only after the backend accepts the word. If the backend rejects
  // it (e.g. a duplicate), the admin's text stays in the box.
  const onSubmit = (data) => submitAction(data, () => reset());
 
  return (
    <Form onSubmit={handleSubmit(onSubmit)} className="mb-3">
      <Form.Label htmlFor="word">Add a word or phrase to censor</Form.Label>
      <InputGroup hasValidation>
        <Form.Control
          data-testid={`${testIdPrefix}-word`}
          id="word"
          type="text"
          placeholder="e.g. badword"
          isInvalid={Boolean(errors.word)}
          {...register("word", {
            validate: (value) =>
              value.trim() !== "" || "Word must not be blank.",
            maxLength: {
              value: MAX_WORD_LENGTH,
              message: `Word must be at most ${MAX_WORD_LENGTH} characters.`,
            },
          })}
        />
        <Button type="submit" data-testid={`${testIdPrefix}-submit`}>
          Add
        </Button>
        <Form.Control.Feedback
          type="invalid"
          data-testid={`${testIdPrefix}-error`}
        >
          {errors.word?.message}
        </Form.Control.Feedback>
      </InputGroup>
    </Form>
  );
}