All files / components/BasicCourseSearch CourseOverTimeSearchForm.js

100% Statements 36/36
100% Branches 34/34
100% Functions 3/3
100% Lines 36/36

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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                    2x 91x     91x 91x     91x     91x     91x     91x 91x               91x             91x   91x     91x     91x     91x 91x   91x 10x 10x                 91x 74x 74x   74x 49x 49x 27x         27x     22x       4x         4x       18x 18x 18x     56x         56x       91x                                                                                                        
import { useState } from "react";
import { Form, Button, Container, Row, Col } from "react-bootstrap";
 
import { quarterRange } from "main/utils/quarterUtilities";
 
import { useSystemInfo } from "main/utils/systemInfo";
import SingleQuarterDropdown from "../Quarters/SingleQuarterDropdown";
import SingleSubjectDropdown from "../Subjects/SingleSubjectDropdown";
import { useBackend } from "main/utils/useBackend";
 
const CourseOverTimeSearchForm = ({ fetchJSON }) => {
  const { data: systemInfo } = useSystemInfo();
 
  // Stryker disable OptionalChaining
  const startQtr = systemInfo?.startQtrYYYYQ || "20211";
  const endQtr = systemInfo?.endQtrYYYYQ || "20214";
  // Stryker enable OptionalChaining
 
  const quarters = quarterRange(startQtr, endQtr);
 
  // Stryker disable all : not sure how to test/mock local storage
  const localStartQuarter = localStorage.getItem(
    "CourseOverTimeSearch.StartQuarter",
  );
  const localEndQuarter = localStorage.getItem(
    "CourseOverTimeSearch.EndQuarter",
  );
  const localSubject = localStorage.getItem("CourseOverTimeSearch.Subject");
  const localCourseNumber = localStorage.getItem(
    "CourseOverTimeSearch.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 defaultSubjectArea = "ANTH";
 
  const [startQuarter, setStartQuarter] = useState(
    localStartQuarter || quarters[0].yyyyq,
  );
  const [endQuarter, setEndQuarter] = useState(
    localEndQuarter || quarters[0].yyyyq,
  );
  const [subject, setSubject] = useState(
    localSubject || subjects[0]?.subjectCode || defaultSubjectArea,
  );
  const [courseNumber, setCourseNumber] = useState(localCourseNumber || "");
  const [courseSuf, setCourseSuf] = useState("");
 
  const handleSubmit = (event) => {
    event.preventDefault();
    fetchJSON(event, {
      startQuarter,
      endQuarter,
      subject,
      courseNumber,
      courseSuf,
    });
  };
 
  const handleCourseNumberOnChange = (event) => {
    const rawCourse = event.target.value;
    const inputtedSubject = rawCourse.match(/^[a-zA-Z]+/);
 
    if (inputtedSubject) {
      const upperSubject = inputtedSubject[0].toUpperCase().replace(/s/g, "");
      if (subject.toUpperCase() === upperSubject) {
        setCourseSuf(
          rawCourse.match(/[a-zA-Z]+$/)
            ? rawCourse.match(/[a-zA-Z]+$/)[0].toUpperCase()
            : "",
        );
        setCourseNumber(
          rawCourse.match(/\d+/) ? rawCourse.match(/\d+/)[0] : "",
        );
      } else if (
        subject.toUpperCase() === "CMPSC" &&
        (upperSubject === "CS" || upperSubject === "COMS")
      ) {
        setCourseSuf(
          rawCourse.match(/[a-zA-Z]+$/)
            ? rawCourse.match(/[a-zA-Z]+$/)[0].toUpperCase()
            : "",
        );
        setCourseNumber(
          rawCourse.match(/\d+/) ? rawCourse.match(/\d+/)[0] : "",
        );
      } else {
        setCourseNumber("");
        setCourseSuf("");
        return;
      }
    }
    setCourseSuf(
      rawCourse.match(/[a-zA-Z]+$/)
        ? rawCourse.match(/[a-zA-Z]+$/)[0].toUpperCase()
        : "",
    );
    setCourseNumber(rawCourse.match(/\d+/) ? rawCourse.match(/\d+/)[0] : "");
  };
 
  // Stryker disable all : Stryker is testing by changing the padding to 0. But this is simply a visual optimization as it makes it look better
  return (
    <Form onSubmit={handleSubmit}>
      <Container>
        <Row>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={startQuarter}
              setQuarter={setStartQuarter}
              controlId={"CourseOverTimeSearch.StartQuarter"}
              label={"Start Quarter"}
            />
          </Col>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={endQuarter}
              setQuarter={setEndQuarter}
              controlId={"CourseOverTimeSearch.EndQuarter"}
              label={"End Quarter"}
            />
          </Col>
          <Col md="auto">
            <SingleSubjectDropdown
              subjects={subjects}
              subject={subject}
              setSubject={setSubject}
              controlId={"CourseOverTimeSearch.Subject"}
              label={"Subject Area"}
            />
          </Col>
        </Row>
        <Form.Group controlId="CourseOverTimeSearch.CourseNumber">
          <Form.Label>Course Number (Try searching '16' or '130A')</Form.Label>
          <Form.Control
            onChange={handleCourseNumberOnChange}
            defaultValue={courseNumber}
          />
        </Form.Group>
        <Row style={{ paddingTop: 10, paddingBottom: 10 }}>
          <Col md="auto">
            <Button variant="primary" type="submit">
              Submit
            </Button>
          </Col>
        </Row>
      </Container>
    </Form>
  );
};
 
export default CourseOverTimeSearchForm;