-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
199 lines (187 loc) · 6.21 KB
/
Copy pathindex.test.js
File metadata and controls
199 lines (187 loc) · 6.21 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { describe, it, expect } from "vitest";
import {
getCohort,
sortStudents,
makeFlag,
increment,
getTaxicabDistance,
getHerbivores,
getCarnivoreNames,
getTotalCost,
zip,
countCharacters,
} from "./index.js";
describe("getCohort", function () {
it("returns the cohort of a student", function () {
expect(getCohort({ cohort: 2012, name: "Alice" })).toBe(2012);
});
it("works with negative cohort numbers", function () {
expect(getCohort({ cohort: -1, name: "Bob" })).toBe(-1);
});
it("works when cohort is 0", function () {
expect(getCohort({ cohort: 0, name: "Charlie" })).toBe(0);
});
});
describe("sortStudents", function () {
it("returns the student whose name comes first alphabetically", function () {
const studentA = { cohort: 1, name: "Alice" };
const studentB = { cohort: 1, name: "Bob" };
expect(sortStudents(studentA, studentB)).toEqual(studentA);
});
it("returns studentA if names are equal", function () {
const studentA = { cohort: 2, name: "Charlie" };
const studentB = { cohort: 3, name: "Charlie" };
expect(sortStudents(studentA, studentB)).toBe(studentA);
});
it("returns student with empty name", function () {
const studentA = { cohort: 2, name: "Dami" };
const studentB = { cohort: 3, name: "" };
expect(sortStudents(studentA, studentB)).toBe(studentB);
});
it("handles case sensitivity", function () {
const studentA = { cohort: 1, name: "ekun" };
const studentB = { cohort: 1, name: "ekun" };
expect(sortStudents(studentA, studentB)).toEqual(studentB);
});
});
describe("makeFlag", function () {
it("returns an object with the given color and icon", function () {
expect(makeFlag("red", "circle")).toEqual({ color: "red", icon: "circle" });
});
it("works with empty strings", function () {
expect(makeFlag("", "")).toEqual({ color: "", icon: "" });
});
});
describe("increment", function () {
it("increments the value by 1", function () {
expect(increment({ value: 1 })).toEqual({ value: 2 });
});
it("works with negative numbers", function () {
expect(increment({ value: -5 })).toEqual({ value: -4 });
});
it("works with 0", function () {
expect(increment({ value: 0 })).toEqual({ value: 1 });
});
it("works with floats", function () {
expect(increment({ value: 9.8 })).toEqual({ value: 10.8 });
});
});
describe("getTaxicabDistance", function () {
it("returns the correct taxicab distance", function () {
expect(getTaxicabDistance({ x: 0, y: 0 }, { x: 3, y: 4 })).toBe(7);
});
it("returns 0 if from and to are the same location", function () {
expect(getTaxicabDistance({ x: 5, y: 5 }, { x: 5, y: 5 })).toBe(0);
});
it("works with negative coordinates", function () {
expect(getTaxicabDistance({ x: 1, y: 2 }, { x: -1, y: -2 })).toBe(6);
});
});
describe("getHerbivores", function () {
it("returns herbivores", function () {
expect(
getHerbivores([
{ name: "Cow", isHerbivore: true },
{ name: "Sheep", isHerbivore: true },
{ name: "Lion", isHerbivore: false },
]),
).toEqual([
{ name: "Cow", isHerbivore: true },
{ name: "Sheep", isHerbivore: true },
]);
});
it("returns single herbivore", function () {
expect(
getHerbivores([
{ name: "Cow", isHerbivore: true },
{ name: "Lion", isHerbivore: false },
]),
).toEqual([{ name: "Cow", isHerbivore: true }]);
});
it("returns empty array for no herbivores", function () {
expect(getHerbivores([{ name: "Lion", isHerbivore: false }])).toEqual([]);
});
it("returns empty array for empty input", function () {
expect(getHerbivores([])).toEqual([]);
});
});
describe("getCarnivoreNames", function () {
it("returns names of carnivorous animals", function () {
expect(
getCarnivoreNames([
{ name: "Lion", isCarnivore: true },
{ name: "Cow", isCarnivore: false },
{ name: "Wolf", isCarnivore: true },
]),
).toEqual(["Lion", "Wolf"]);
});
it("returns name of single carnivore", function () {
expect(
getCarnivoreNames([
{ name: "Lion", isCarnivore: true },
{ name: "Cow", isCarnivore: false },
]),
).toEqual(["Lion"]);
});
it("returns empty array for no carnivores", function () {
expect(getCarnivoreNames([{ name: "Cow", isCarnivore: false }])).toEqual(
[],
);
});
it("returns empty array for empty input", function () {
expect(getCarnivoreNames([])).toEqual([]);
});
});
describe("getTotalCost", function () {
it("returns 0 for empty cart", function () {
expect(getTotalCost([])).toBe(0);
});
it("returns 0 for items with zero quantity", function () {
expect(getTotalCost([{ name: "A", quantity: 0, price: 5 }])).toBe(0);
});
it("returns cost of single item", function () {
expect(getTotalCost([{ name: "A", quantity: 1, price: 5 }])).toBe(5);
});
it("returns cost of items with integer prices", function () {
expect(
getTotalCost([
{ name: "A", quantity: 1, price: 5 },
{ name: "B", quantity: 2, price: 3 },
]),
).toBe(11);
});
it("returns the total cost of cart", function () {
expect(
getTotalCost([
{ name: "A", quantity: 3, price: 3.12 },
{ name: "B", quantity: 1, price: 2.5 },
{ name: "B", quantity: 0, price: 6.7 },
]),
).toBeCloseTo(11.86);
});
});
describe("zip", function () {
it("zips two arrays into an object", function () {
expect(zip(["x", "y"], [6, 7])).toEqual({ x: 6, y: 7 });
});
it("handles string values", function () {
expect(zip(["x"], ["x"])).toEqual({ x: "x" });
});
it("returns empty object for empty arrays", function () {
expect(zip([], [])).toEqual({});
});
});
describe("countCharacters", function () {
it("returns empty object for empty string", function () {
expect(countCharacters("")).toEqual({});
});
it("counts each character in a word", function () {
expect(countCharacters("hello")).toEqual({ h: 1, e: 1, l: 2, o: 1 });
});
it("is case sensitive", function () {
expect(countCharacters("bbBbbCcC")).toEqual({ b: 4, B: 1, c: 1, C: 2 });
});
it("handles non-letter characters", function () {
expect(countCharacters("a!a!!")).toEqual({ a: 2, "!": 3 });
});
});