All files / components/Subjects SingleSubjectDropdown.js

100% Statements 14/14
100% Branches 6/6
100% Functions 3/3
100% Lines 14/14

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        8x               236x   236x         236x 13x 13x 13x 13x 1x       236x   236x                 22536x 22536x 22536x                        
import { compareValues } from "main/utils/sortHelper";
import React, { useState } from "react";
import { Form } from "react-bootstrap";
 
const SingleSubjectDropdown = ({
  subjects,
  subject,
  setSubject,
  controlId,
  onChange = null,
  label = "Subject Area",
}) => {
  const localSearchSubject = localStorage.getItem(controlId);
 
  const [subjectState, setSubjectState] = useState(
    // Stryker disable next-line all : not sure how to test/mock local storage
    localSearchSubject || subject,
  );
 
  const handleSubjectOnChange = (event) => {
    localStorage.setItem(controlId, event.target.value);
    setSubjectState(event.target.value);
    setSubject(event.target.value);
    if (onChange != null) {
      onChange(event);
    }
  };
 
  subjects.sort(compareValues("subjectCode"));
 
  return (
    <Form.Group controlId={controlId}>
      <Form.Label>{label}</Form.Label>
      <Form.Control
        as="select"
        value={subjectState}
        onChange={handleSubjectOnChange}
      >
        {subjects.map(function (object) {
          const subjectCode = object.subjectCode.replace(/ /g, "-");
          const key = `${controlId}-option-${subjectCode}`;
          return (
            <option key={key} data-testid={key} value={object.subjectCode}>
              {object.subjectCode} - {object.subjectTranslation}
            </option>
          );
        })}
      </Form.Control>
    </Form.Group>
  );
};
 
export default SingleSubjectDropdown;