All files / components/GEAreas SingleAreaDropdown.js

100% Statements 13/13
100% Branches 8/8
100% Functions 3/3
100% Lines 13/13

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          1x               24x   24x         24x             24x 4x 4x 4x 4x 2x       24x                     196x 196x                        
import React, { _useEffect, useState } from "react";
import { Form } from "react-bootstrap";
import { useBackend } from "main/utils/useBackend";
 
// A dropdown component for selecting General Education Areas
const SingleAreaDropdown = ({
  area,
  setArea,
  controlId,
  onChange = null,
  label = "GE Area",
  //showAll = false,
}) => {
  const localSearchArea = localStorage.getItem(controlId);
 
  const [areaState, setAreaState] = useState(
    // Stryker disable next-line all : not sure how to test/mock local storage
    localSearchArea || area,
  );
 
  const { data: areas, error: _error } = useBackend(
    // Stryker disable next-line all : can't test api calls
    ["/api/public/generalEducationInfo"],
    { method: "GET", url: "/api/public/generalEducationInfo" },
    [],
  );
 
  const handleAreaOnChange = (event) => {
    localStorage.setItem(controlId, event.target.value);
    setAreaState(event.target.value);
    setArea(event.target.value);
    if (onChange != null) {
      onChange(event);
    }
  };
 
  return (
    <Form.Group controlId={controlId}>
      <Form.Label>{label}</Form.Label>
      <Form.Control as="select" value={areaState} onChange={handleAreaOnChange}>
        {
          <option data-testid={`${controlId}-option-all`} value="ALL">
            ALL
          </option>
        }
        {areas &&
          areas.map((areaCode) => {
            const key = `${controlId}-option-${areaCode}`;
            return (
              <option key={key} data-testid={key} value={areaCode}>
                {areaCode}
              </option>
            );
          })}
      </Form.Control>
    </Form.Group>
  );
};
 
export default SingleAreaDropdown;