summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Tests/operators/logical-nullish-coalescing.js
blob: f8d778208f06036faff13145f37fd2a521d63585 (plain)
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
test("booleans", () => {
    expect(true ?? true).toBeTrue();
    expect(false ?? false).toBeFalse();
    expect(true ?? false).toBeTrue();
    expect(false ?? true).toBeFalse();
});

test("strings", () => {
    expect("" ?? "").toBe("");
    expect("" ?? false).toBe("");
    expect("" ?? true).toBe("");
    expect(false ?? "").toBeFalse();
    expect(true ?? "").toBeTrue();
    expect("foo" ?? "bar").toBe("foo");
    expect("foo" ?? false).toBe("foo");
    expect("foo" ?? true).toBe("foo");
    expect(false ?? "bar").toBeFalse();
    expect(true ?? "bar").toBeTrue();
});

test("numbers", () => {
    expect(false ?? 1 === 2).toBeFalse();
    expect(true ?? 1 === 2).toBeTrue();
    expect(0 ?? false).toBe(0);
    expect(0 ?? true).toBe(0);
    expect(42 ?? false).toBe(42);
    expect(42 ?? true).toBe(42);
    expect(false ?? 0).toBeFalse();
    expect(true ?? 0).toBeTrue();
    expect(false ?? 42).toBeFalse();
    expect(true ?? 42).toBeTrue();
});

test("objects", () => {
    expect([] ?? false).toHaveLength(0);
    expect([] ?? true).toHaveLength(0);
    expect(false ?? []).toBeFalse();
    expect(true ?? []).toBeTrue();
});

test("null & undefined", () => {
    expect(null ?? false).toBeFalse();
    expect(null ?? true).toBeTrue();
    expect(false ?? null).toBeFalse();
    expect(true ?? null).toBeTrue();
    expect(undefined ?? false).toBeFalse();
    expect(undefined ?? true).toBeTrue();
    expect(false ?? undefined).toBeFalse();
    expect(true ?? undefined).toBeTrue();
});