github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/public/src/__tests__/isValid.test.tsx (about)

     1  import { User } from "../../proto/qf/types_pb"
     2  import { initializeOvermind } from "./TestHelpers"
     3  
     4  describe("User and enrollment validation", () => {
     5  
     6      const tests = [
     7          {
     8              desc: "User should be valid",
     9              user: new User({
    10                  ID: BigInt(1),
    11                  Name: "Test User",
    12                  Email: "mail@mail.com",
    13                  StudentID: "1234567"
    14              }),
    15              expect: true
    16          },
    17          {
    18              desc: "User should not be valid if name is empty",
    19              user: new User({
    20                  ID: BigInt(2),
    21                  Email: "mail@mail.com",
    22                  StudentID: "1234567"
    23              }),
    24              expect: false
    25          },
    26          {
    27              desc: "User should not be valid if email is empty",
    28              user: new User({
    29                  ID: BigInt(1),
    30                  Name: "Test User 3",
    31                  StudentID: "1234567"
    32              }),
    33              expect: false
    34          },
    35          {
    36              desc: "User should not be valid if studentId is empty",
    37              user: new User({
    38                  ID: BigInt(4),
    39                  Name: "Test User 4",
    40                  Email: "mail@mail.com"
    41              }),
    42              expect: false
    43          },
    44          {
    45              desc: "User should not be valid if name, email and studentId is empty",
    46              user: new User({
    47                  ID: BigInt(5)
    48              }),
    49              expect: false
    50          },
    51      ]
    52  
    53      test.each(tests)(`$desc`, (test) => {
    54          const { state } = initializeOvermind({ self: test.user })
    55          expect(state.isValid).toBe(test.expect)
    56      })
    57  
    58      const emailTests = [
    59          {
    60              desc: "Email should be valid",
    61              email: "hei@mail.com",
    62              expect: true
    63          },
    64          {
    65              desc: "Email should not be valid",
    66              email: "hei@mail",
    67              expect: false
    68          }
    69      ]
    70      test.each(emailTests)(`$desc`, (test) => {
    71          const regex = /\S+@\S+\.\S+/
    72          const match = test.email.match(regex)
    73          // If no match is found, match is null, otherwise it is an array
    74          // Converting to boolean, null is false, array is true
    75          expect(Boolean(match)).toBe(test.expect)
    76      })
    77  })