All files / components/BasicCourseSearch CourseSearchWithCourseNumberForm.js

100% Statements 29/29
100% Branches 16/16
100% Functions 3/3
100% Lines 29/29

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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                        1x   48x     48x 48x     48x     48x 48x 48x 48x     48x             48x 48x 48x 48x 48x   48x 2x 2x     48x 8x 8x 7x 7x   1x     8x 2x 2x   6x       48x                                                                                          
import { useState } from "react";
import { Form, Button, Container, Row, Col } from "react-bootstrap";
 
import { allTheLevels } from "fixtures/levelsFixtures";
import { quarterRange } from "main/utils/quarterUtilities";
 
import { useSystemInfo } from "main/utils/systemInfo";
import SingleQuarterDropdown from "../Quarters/SingleQuarterDropdown";
import SingleSubjectDropdown from "../Subjects/SingleSubjectDropdown";
import SingleLevelDropdown from "../Levels/SingleLevelDropdown";
import { useBackend } from "main/utils/useBackend";
 
const CourseSearchWithCourseNumberForm = ({ fetchJSON }) => {
 
  const { data: systemInfo } = useSystemInfo();
 
  // Stryker disable OptionalChaining
  const startQtr = systemInfo?.startQtrYYYYQ || "20211";
  const endQtr = systemInfo?.endQtrYYYYQ || "20214";
  // Stryker restore OptionalChaining
 
  const quarters = quarterRange(startQtr, endQtr);
 
  // Stryker disable all : not sure how to test/mock local storage
  const localSubject = localStorage.getItem("CourseNumberSearch.Subject");
  const localQuarter = localStorage.getItem("CourseNumberSearch.Quarter");
  const localLevel = localStorage.getItem("CourseNumberSearch.CourseLevel");
  const localCourseNumber = localStorage.getItem("CourseNumberSearch.CourseNumber");
 
  const { data: subjects, error: _error, status: _status } =
  useBackend(
    // Stryker disable next-line all : don't test internal caching of React Query
    ["/api/UCSBSubjects/all"], 
    { method: "GET", url: "/api/UCSBSubjects/all" }, 
    []
  );
 
  const [quarter, setQuarter] = useState(localQuarter || quarters[0].yyyyq);
  const [subject, setSubject] = useState(localSubject || {});
  const [level, setLevel] = useState(localLevel || "U");
  const [courseNumber, setCourseNumber] = useState(localCourseNumber || "");
  const [courseSuf, setCourseSuf] = useState("");
 
  const handleSubmit = (event) => {
    event.preventDefault();
    fetchJSON(event, { quarter, subject, level, courseNumber, courseSuf });
  };
 
  const handleCourseNumberOnChange = (event) => {
    const rawCourse = event.target.value;
    if (rawCourse.match(/\d+/g) != null) {
        const number = rawCourse.match(/\d+/g)[0];
        setCourseNumber(number);
    } else {
        setCourseNumber("");
    }
    
    if (rawCourse.match(/[a-zA-Z]+/g) != null) {
        const suffix = rawCourse.match(/[a-zA-Z]+/g)[0];
        setCourseSuf(suffix);
    } else {
        setCourseSuf("");
    }
  };
 
  return (
    <Form onSubmit={handleSubmit}>
      <Container>
        <Row>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={quarter}
              setQuarter={setQuarter}
              controlId={"CourseNumberSearch.Quarter"}
            />
          </Col>
          <Col md="auto">
            <SingleSubjectDropdown
              subjects={subjects}
              subject={subject}
              setSubject={setSubject}
              controlId={"CourseNumberSearch.Subject"}
            />
          </Col>
          <Col md="auto">
            <SingleLevelDropdown
              levels={allTheLevels}
              level={level}
              setLevel={setLevel}
              controlId={"CourseNumberSearch.Level"}
            />
          </Col>
        </Row>
        <Form.Group controlId="CourseNumberSearch.CourseNumber">
            <Form.Label>Course Number (Try searching '16' or '130A', or leave blank to view all courses)</Form.Label>
            <Form.Control onChange={handleCourseNumberOnChange} defaultValue={courseNumber} />
        </Form.Group>
        <Row data-testid="submitRow" style={{ paddingTop: 10, paddingBottom: 10 }}>
          <Col md="auto">
            <Button variant="primary" type="submit">
              Submit
            </Button>
          </Col>
        </Row>
      </Container>
    </Form>
  );
};
 
export default CourseSearchWithCourseNumberForm;