All files / components/GeneralEducation SingleAreaDropdown.js

100% Statements 18/18
100% Branches 11/11
100% Functions 4/4
100% Lines 17/17

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 58 59 60 61 62 63 64 65 66 67            1x                 19x   19x         19x 4x 4x 4x 4x 2x       19x 34x 34x 17x     19x                   52x 52x 52x                                  
import React, { useState } from "react";
import { Form } from "react-bootstrap";
 
// showAll is defaulted to false, to ensure the "ALL" option
// doesn't showdown to pre-existing dropdowns
 
const SingleAreaDropdown = ({
  areas,
  area,
  setArea,
  controlId,
  onChange = null,
  label = "General Education Area",
  showAll = false,
}) => {
  const localGEArea = localStorage.getItem(controlId);
 
  const [areaState, setAreaState] = useState(
    // Stryker disable next-line all : not sure how to test/mock local storage
    localGEArea || area,
  );
 
  const handleAreaOnChange = (event) => {
    localStorage.setItem(controlId, event.target.value);
    setAreaState(event.target.value);
    setArea(event.target.value);
    if (onChange != null) {
      onChange(event);
    }
  };
 
  areas.sort((a, b) => {
    const codeCompare = a.requirementCode.localeCompare(b.requirementCode);
    if (codeCompare !== 0) return codeCompare;
    return a.collegeCode.localeCompare(b.collegeCode);
  });
 
  return (
    <Form.Group controlId={controlId}>
      <Form.Label>{label}</Form.Label>
      <Form.Control as="select" value={areaState} onChange={handleAreaOnChange}>
        {showAll && (
          <option data-testid={`${controlId}-option-all`} value="ALL">
            ALL
          </option>
        )}
        {areas.map(function (object) {
          const areaCode = object.requirementCode;
          const key = `${controlId}-option-${areaCode}-${object.collegeCode}`;
          return (
            <option
              key={key}
              data-testid={key}
              value={object.requirementCode + "-" + object.collegeCode}
            >
              {object.requirementCode} - {object.requirementTranslation} (
              {object.collegeCode})
            </option>
          );
        })}
      </Form.Control>
    </Form.Group>
  );
};
 
export default SingleAreaDropdown;