File size: 1,535 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, expect, it } from "vitest";

import {
  extractUrl,
  generateRandomName,
  getInitials,
  isEmptyString,
  isUrl,
  processUsername,
} from "../string";

describe("getInitials", () => {
  it("returns the initials of a name", () => {
    expect(getInitials("John Doe")).toBe("JD");
    expect(getInitials("Mary Jane Watson")).toBe("MW");
  });
});

describe("isUrl", () => {
  it("checks if a string is a URL", () => {
    expect(isUrl("https://example.com")).toBe(true);
    expect(isUrl("not a url")).toBe(false);
  });
});

describe("isEmptyString", () => {
  it("checks if a string is empty or only contains whitespace", () => {
    expect(isEmptyString("")).toBe(true);
    expect(isEmptyString(" ")).toBe(true);
    expect(isEmptyString("<p></p>")).toBe(true);
    expect(isEmptyString("not empty")).toBe(false);
  });
});

describe("extractUrl", () => {
  it("extracts a URL from a string", () => {
    expect(extractUrl("Visit https://example.com today!")).toBe("https://example.com");
    expect(extractUrl("No URL here.")).toBeNull();
  });
});

describe("generateRandomName", () => {
  it("generates a random name", () => {
    const name = generateRandomName();
    expect(name).toMatch(/^(?:[A-Z][a-z]+ ){2}[A-Z][a-z]+$/);
  });
});

describe("processUsername", () => {
  it("processes a username by removing non-alphanumeric characters", () => {
    expect(processUsername("User@Name!")).toBe("username");
    expect(processUsername("")).toBe("");
    expect(processUsername(null)).toBe("");
  });
});