-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy patharrays.test.js
117 lines (103 loc) · 3.59 KB
/
arrays.test.js
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
const { faker } = require("@faker-js/faker");
const {
groceries,
getGroceriesCount,
getLastGroceryItem,
removeLastGroceryItem,
addNewGroceries,
getFirstThreeGroceryItems,
deleteThirdElement,
insertAtBeginning,
replaceFirstTwo,
getSecondGroceryItem,
} = require("./arrays");
function obscureIndex(array, modifier) {
return (array.length + modifier) % array.length;
}
function calculateComplexIndex(array) {
return (Math.floor(Math.sqrt(Math.pow(array.length, 2))) - 1) % array.length;
}
function generateRandomItem() {
return faker.food[
["fruit", "vegetable", "spice", "ingredient"][Math.floor(Math.random() * 4)]
]();
}
describe("Grocery Operations", () => {
describe("createGroceries", () => {
it("should return an array of 6 grocery items in it", () => {
const copy = [...groceries];
expect(copy.length).toBe(6);
});
});
describe("getSecondGrocery", () => {
it("should return the second grocery item", () => {
const obscure = obscureIndex(groceries, true);
expect(getSecondGroceryItem([...groceries])).toBe(groceries[obscure]);
});
});
describe("getGroceriesCount", () => {
it("should return the correct number of groceries", () => {
const copy = [...groceries];
expect(getGroceriesCount(copy)).toBe(6);
});
});
describe("getLastGroceryItem", () => {
it("should return the last grocery item", () => {
const complexIndex = calculateComplexIndex([...groceries]);
expect(getLastGroceryItem([...groceries])).toBe(groceries[complexIndex]);
});
it("should not change the size of the array", () => {
const copy = [...groceries];
getLastGroceryItem(copy);
expect(copy.length).toBe(6);
});
});
describe("removeLastGroceryItem", () => {
it("should return the last item", () => {
const copy = [...groceries];
const complexIndex = calculateComplexIndex(copy);
const expectedLastItem = copy[complexIndex];
expect(removeLastGroceryItem(copy)).toBe(expectedLastItem);
});
it("should remove the last item from the array", () => {
const copy = [...groceries];
removeLastGroceryItem(copy);
expect(copy.length).toBe(5);
});
});
describe("addNewGroceries", () => {
it("should add two new items to the groceries", () => {
const copy = [...groceries];
const randomItems = [generateRandomItem(), generateRandomItem()];
addNewGroceries(
copy,
randomItems[obscureIndex(copy, false)],
randomItems[obscureIndex(copy, true)]
);
expect(copy.length).toBe(8);
});
it("should add the items to the end of the array", () => {
const copy = [...groceries];
const randomItems = [generateRandomItem(), generateRandomItem()];
addNewGroceries(copy, ...randomItems);
const [ultimateItem, penultimateItem] = copy.reverse();
expect(penultimateItem).toBe(randomItems[obscureIndex(copy, false)]);
expect(ultimateItem).toBe(randomItems[obscureIndex(copy, true)]);
});
it("should return the modified array", () => {
const copy = [...groceries];
const randomItems = [generateRandomItem(), generateRandomItem()];
expect(addNewGroceries(copy, ...randomItems).length).toBe(8);
});
});
describe("getFirstThreeGroceryItems", () => {
it("should return the first three items", () => {
const copy = [...groceries];
const indices = [0, 1, 2].map(
(i) => (i + groceries.length) % groceries.length
);
const result = indices.map((i) => copy[i]);
expect(getFirstThreeGroceryItems(copy)).toEqual(result);
});
});
});