Skip to content

Commit 5a26d13

Browse files
committed
test: update tests and throw error if strings more than two chars
1 parent 78acbdb commit 5a26d13

2 files changed

Lines changed: 114 additions & 141 deletions

File tree

src/helpers/sort.test.ts

Lines changed: 104 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -9,157 +9,124 @@ describe("swap", () => {
99
});
1010
});
1111

12-
// describe("defaultCompare", () => {
13-
// it("should return -1 if a < b", () => {
14-
// const result = defaultCompare(1, 2);
15-
// expect(result).toBe(-1);
16-
// });
17-
// it("should return 1 if a > b", () => {
18-
// const result = defaultCompare(2, 1);
19-
// expect(result).toBe(1);
20-
// });
21-
// it("should return 0 if a === b", () => {
22-
// const result = defaultCompare(1, 1);
23-
// expect(result).toBe(0);
24-
// });
25-
// });
26-
27-
// describe("array sort", () => {
28-
// it("should sort an array of numbers", () => {
29-
// const array = [4, 2, 3, 1, 5];
30-
// const result = array.sort((a, b) => defaultCompare(a, b));
31-
// expect(result).toEqual([1, 2, 3, 4, 5]);
32-
// });
33-
34-
// it("should sort an array of strings", () => {
35-
// const array = ["a", "z", "ag", "ac", "ab", "ae", "ba"];
36-
// const result = array.sort((a, b) => defaultCompare(a, b));
37-
// expect(result).toEqual(["a", "ab", "ac", "ae", "ag", "ba", "z"]);
38-
// });
12+
describe("array sort", () => {
13+
it("should sort an array of strings", () => {
14+
const array = ["a", "z", "ag", "ac", "ab", "ae", "ba"];
15+
const result = array.sort((a, b) => compareStrings(a, b));
16+
expect(result).toEqual(["a", "z", "ab", "ac", "ae", "ag", "ba"]);
17+
});
3918

40-
// it("should sort an array of strings that are numbers", () => {
41-
// const array = ["5", "4", "3", "2", "1"];
42-
// const result = array.sort((a, b) => defaultCompare(a, b));
43-
// expect(result).toEqual(["1", "2", "3", "4", "5"]);
44-
// });
45-
// });
19+
it("should sort an array of strings that are numbers", () => {
20+
const array = ["5", "4", "3", "2", "1"];
21+
const result = array.sort((a, b) => compareStrings(a, b));
22+
expect(result).toEqual(["1", "2", "3", "4", "5"]);
23+
});
24+
});
4625

47-
// describe("sortProducts", () => {
48-
// it("should sort an array of products by pick location in ascending order from A 1 to A 10", () => {
49-
// const data: ProductTuple[] = [
50-
// ["product_code", "quantity", "pick_location"],
51-
// ["B1237", "2", "A 10"],
52-
// ["B1234", "2", "A 3"],
53-
// ["B1235", "3", "A 2"],
54-
// ["B1236", "4", "A 1"]
55-
// ];
56-
// const [columns, ...rows] = data;
26+
describe("sortProducts", () => {
27+
it("should sort an array of products by pick location in ascending order from A 1 to A 10", () => {
28+
const data: ProductTuple[] = [
29+
["product_code", "quantity", "pick_location"],
30+
["B1237", "2", "A 10"],
31+
["B1234", "2", "A 3"],
32+
["B1235", "3", "A 2"],
33+
["B1236", "4", "A 1"]
34+
];
35+
const [columns, ...rows] = data;
5736

58-
// const sortedRows = sortProducts(rows);
59-
// const result = [columns, ...sortedRows];
37+
const sortedRows = sortProducts(rows, "ascending");
38+
const result = [columns, ...sortedRows];
6039

61-
// expect(result).toEqual([
62-
// ["product_code", "quantity", "pick_location"],
63-
// ["B1236", "4", "A 1"],
64-
// ["B1235", "3", "A 2"],
65-
// ["B1234", "2", "A 3"],
66-
// ["B1237", "2", "A 10"]
67-
// ]);
68-
// });
69-
// it("should sort an array of products by pick location (bay and shelf height) in ascending order", () => {
70-
// const data: ProductTuple[] = [
71-
// ["product_code", "quantity", "pick_location"],
72-
// ["A", "10", "Z 1"],
73-
// ["B", "5", "Z 10"],
74-
// ["C", "10", "A 1"],
75-
// ["G", "1", "A 7"]
76-
// ];
77-
// const [columns, ...rows] = data;
40+
expect(result).toEqual([
41+
["product_code", "quantity", "pick_location"],
42+
["B1236", "4", "A 1"],
43+
["B1235", "3", "A 2"],
44+
["B1234", "2", "A 3"],
45+
["B1237", "2", "A 10"]
46+
]);
47+
});
48+
it("should sort an array of products by pick location (bay and shelf height) in ascending order", () => {
49+
const data: ProductTuple[] = [
50+
["product_code", "quantity", "pick_location"],
51+
["A", "10", "Z 1"],
52+
["B", "5", "Z 10"],
53+
["C", "10", "A 1"],
54+
["G", "1", "A 7"]
55+
];
56+
const [columns, ...rows] = data;
7857

79-
// const sortedRows = sortProducts(rows);
80-
// const result = [columns, ...sortedRows];
58+
const sortedRows = sortProducts(rows, "ascending");
59+
const result = [columns, ...sortedRows];
8160

82-
// expect(result).toEqual([
83-
// ["product_code", "quantity", "pick_location"],
84-
// ["C", "10", "A 1"],
85-
// ["G", "1", "A 7"],
86-
// ["A", "10", "Z 1"],
87-
// ["B", "5", "Z 10"]
88-
// ]);
89-
// });
90-
// it("should sort an array of products by bay and shelf height in ascending order WHEN bays are the same FROM lowest to highest shelf", () => {
91-
// const data: ProductTuple[] = [
92-
// ["product_code", "quantity", "pick_location"],
93-
// ["E", "1", "AB 1"],
94-
// ["F", "1", "AB 10"],
95-
// ["H", "1", "AB 9"],
96-
// ["H", "1", "AB 7"]
97-
// ];
98-
// const [columns, ...rows] = data;
61+
expect(result).toEqual([
62+
["product_code", "quantity", "pick_location"],
63+
["C", "10", "A 1"],
64+
["G", "1", "A 7"],
65+
["A", "10", "Z 1"],
66+
["B", "5", "Z 10"]
67+
]);
68+
});
69+
it("should sort an array of products by bay and shelf height in ascending order WHEN bays are the same FROM lowest to highest shelf", () => {
70+
const data: ProductTuple[] = [
71+
["product_code", "quantity", "pick_location"],
72+
["E", "1", "AB 1"],
73+
["F", "1", "AB 10"],
74+
["H", "1", "AB 9"],
75+
["H", "1", "AB 7"]
76+
];
77+
const [columns, ...rows] = data;
9978

100-
// const sortedRows = sortProducts(rows);
101-
// const result = [columns, ...sortedRows];
79+
const sortedRows = sortProducts(rows, "ascending");
80+
const result = [columns, ...sortedRows];
10281

103-
// expect(result).toEqual([
104-
// ["product_code", "quantity", "pick_location"],
105-
// ["E", "1", "AB 1"],
106-
// ["H", "1", "AB 7"],
107-
// ["H", "1", "AB 9"],
108-
// ["F", "1", "AB 10"]
109-
// ]);
110-
// });
82+
expect(result).toEqual([
83+
["product_code", "quantity", "pick_location"],
84+
["E", "1", "AB 1"],
85+
["H", "1", "AB 7"],
86+
["H", "1", "AB 9"],
87+
["F", "1", "AB 10"]
88+
]);
89+
});
11190

112-
// it("should sort by shelf height if the bays are not the same", () => {
113-
// const data: ProductTuple[] = [
114-
// ["product_code", "quantity", "pick_location"],
115-
// ["B1237", "2", "A 10"],
116-
// ["B1237", "2", "Z 10"],
117-
// ["B1234", "2", "Z 3"],
118-
// ["B1234", "2", "A 3"],
119-
// ["B1235", "3", "Z 2"],
120-
// ["B1236", "4", "Z 1"],
121-
// ["B1235", "3", "A 2"],
122-
// ["B1236", "4", "A 1"]
123-
// ];
91+
it("should sort by shelf height if the bays are not the same", () => {
92+
const data: ProductTuple[] = [
93+
["product_code", "quantity", "pick_location"],
94+
["B1237", "2", "A 10"],
95+
["B1237", "2", "Z 10"],
96+
["B1234", "2", "Z 3"],
97+
["B1234", "2", "A 3"],
98+
["B1235", "3", "Z 2"],
99+
["B1236", "4", "Z 1"],
100+
["B1235", "3", "A 2"],
101+
["B1236", "4", "A 1"]
102+
];
124103

125-
// const [columns, ...rows] = data;
104+
const [columns, ...rows] = data;
126105

127-
// const sortedRows = sortProducts(rows);
128-
// const result = [columns, ...sortedRows];
129-
// expect(result).toEqual([
130-
// ["product_code", "quantity", "pick_location"],
131-
// ["B1236", "4", "A 1"],
132-
// ["B1235", "3", "A 2"],
133-
// ["B1234", "2", "A 3"],
134-
// ["B1237", "2", "A 10"],
135-
// ["B1236", "4", "Z 1"],
136-
// ["B1235", "3", "Z 2"],
137-
// ["B1234", "2", "Z 3"],
138-
// ["B1237", "2", "Z 10"]
139-
// ]);
140-
// });
141-
// });
106+
const sortedRows = sortProducts(rows, "ascending");
107+
const result = [columns, ...sortedRows];
108+
expect(result).toEqual([
109+
["product_code", "quantity", "pick_location"],
110+
["B1236", "4", "A 1"],
111+
["B1235", "3", "A 2"],
112+
["B1234", "2", "A 3"],
113+
["B1237", "2", "A 10"],
114+
["B1236", "4", "Z 1"],
115+
["B1235", "3", "Z 2"],
116+
["B1234", "2", "Z 3"],
117+
["B1237", "2", "Z 10"]
118+
]);
119+
});
120+
});
142121

143122
describe("sortProducts", () => {
144-
// TODO a to az
145123
const data: ProductTuple[] = [
146124
["product_code", "quantity", "pick_location"],
147125
["1", "1", "AB 1"],
148126
["2", "1", "AB 10"],
149127
["3", "1", "AB 9"],
150128
["4", "1", "AB 7"]
151129
];
152-
// it("should throw an error if the pick location is invalid", () => {
153-
// //
154-
// // const [columns, ...rows] = data;
155-
// // const result = newSortProducts(rows, "ascending");
156-
// // // console.log('');
157-
// // console.log("result", result);
158-
// });
159-
160-
it("should correctly split a pick location into bay and shelf height", () => {
161-
//
162-
});
163130

164131
it("should sort products with the same bay by shelf height", () => {
165132
const [, ...rows] = data;
@@ -205,8 +172,6 @@ describe("sortProducts", () => {
205172
it("should sort products that do not have the same bay by bay and then by shelf height", () => {
206173
const data: ProductTuple[] = [
207174
["product_code", "quantity", "pick_location"],
208-
// ["1", "1", "A 1"],
209-
// ["2", "1", "Z 1"],
210175
["3", "1", "AB 10"],
211176
["4", "1", "AB 9"],
212177
["5", "1", "AB 7"],
@@ -248,8 +213,12 @@ describe("sortProducts", () => {
248213
});
249214

250215
describe("compareStrings", () => {
251-
// it("should return undefined if nothing matches", () => {
252-
// // TODO empty case
216+
it("should throw an error if the string is more than two characters", () => {
217+
expect(() => compareStrings("ABC", "ABC")).toThrowError();
218+
expect(() => compareStrings("ABC", "AB")).toThrowError();
219+
expect(() => compareStrings("AB", "ABC")).toThrowError();
220+
expect(() => compareStrings("AB", "AB")).not.toThrowError();
221+
});
253222
// });
254223
describe("single char", () => {
255224
// Case 1: A, A - single chars, both match
@@ -344,8 +313,4 @@ describe("compareStrings", () => {
344313
expect(result).toEqual(Compare.LESS_THAN);
345314
});
346315
});
347-
348-
// describe("numeric shelf height", () => {
349-
// //
350-
// });
351316
});

src/helpers/sort.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ export const compareStrings = (a: string, b: string) => {
8080
// Case 7: AA, Z - multiple chars, and single char
8181
// Case 8: Z, AA - single char, and multiple chars
8282

83+
if (a.length > 2 || b.length > 2) {
84+
throw new Error("Invalid string length; string must be 2 characters or less.");
85+
}
86+
8387
if (a.length === 1 && b.length === 1) {
8488
// Case 1: A, A - single chars, both match
8589
if (a === b) {
@@ -122,8 +126,9 @@ export const compareStrings = (a: string, b: string) => {
122126
return Compare.LESS_THAN;
123127
}
124128

125-
// TODO default case and return value
126-
return undefined;
129+
// default case and return value
130+
// should never be hit
131+
return Compare.EQUALS;
127132
};
128133

129134
export const sortProducts = (products: ProductTuple[], method: SortMethod) => {
@@ -149,11 +154,13 @@ export const sortProducts = (products: ProductTuple[], method: SortMethod) => {
149154
const compare = compareStrings(currentBay, nextBay);
150155

151156
// top of the sort order
157+
// Case 1: skip all less than results
152158
if (compare === Compare.LESS_THAN) {
153159
continue;
154160
}
155161

156162
// swap based on shelf if both match
163+
// Case 2: swap based on shelf if both match
157164
if (compare === Compare.EQUALS) {
158165
if (Number(currentShelf) > Number(nextShelf)) {
159166
swap(result, i, i + 1);
@@ -162,6 +169,7 @@ export const sortProducts = (products: ProductTuple[], method: SortMethod) => {
162169

163170
// swap based on bay
164171
if (compare === Compare.BIGGER_THAN) {
172+
// TODO it was saying this wasn't tested before.
165173
swap(result, i, i + 1);
166174
}
167175
}

0 commit comments

Comments
 (0)