Skip to content

Commit 01d2ec5

Browse files
committed
feat: create tests for solution
1 parent 10c7bc9 commit 01d2ec5

File tree

1 file changed

+74
-7
lines changed

1 file changed

+74
-7
lines changed
Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,82 @@
11
const permutations = require('./permutations-solution');
22

33
describe('permutations', () => {
4-
test('First test description', () => {
5-
// Replace this comment with any other necessary code, and update the expect line as necessary
4+
function outcome(input, expected) {
5+
const actual = permutations(input);
66

7-
expect(permutations()).toBe('');
7+
// Convert both arrays to strings to compare them later, excluding the order the arrays elements are in
8+
9+
const sterilise = (input) =>
10+
input
11+
.map((el) => el.toString())
12+
.toSorted()
13+
.toString();
14+
15+
return [sterilise(actual), sterilise(expected)];
16+
}
17+
18+
let actual, expected;
19+
20+
afterEach(() => {
21+
expect(actual).toBe(expected);
822
});
9-
10-
test('Second test description', () => {
11-
// Replace this comment with any other necessary code, and update the expect line as necessary
1223

13-
expect(permutations()).toBe('');
24+
test('Works for array of size one', () => {
25+
[actual, expected] = outcome([1], [1]);
26+
});
27+
test('Works for array of size two', () => {
28+
[actual, expected] = outcome(
29+
[1, 2],
30+
[
31+
[1, 2],
32+
[2, 1],
33+
],
34+
);
35+
});
36+
test('Works for array of size three', () => {
37+
[actual, expected] = outcome(
38+
[1, 2, 3],
39+
[
40+
[1, 2, 3],
41+
[1, 3, 2],
42+
[2, 1, 3],
43+
[2, 3, 1],
44+
[3, 1, 2],
45+
[3, 2, 1],
46+
],
47+
);
48+
});
49+
test('Works for array of size four', () => {
50+
[actual, expected] = outcome(
51+
[1, 2, 3, 4],
52+
[
53+
[
54+
[1, 2, 3, 4],
55+
[1, 2, 4, 3],
56+
[1, 3, 2, 4],
57+
[1, 3, 4, 2],
58+
[1, 4, 2, 3],
59+
[1, 4, 3, 2],
60+
[2, 1, 3, 4],
61+
[2, 1, 4, 3],
62+
[2, 3, 1, 4],
63+
[2, 3, 4, 1],
64+
[2, 4, 1, 3],
65+
[2, 4, 3, 1],
66+
[3, 1, 2, 4],
67+
[3, 1, 4, 2],
68+
[3, 2, 1, 4],
69+
[3, 2, 4, 1],
70+
[3, 4, 1, 2],
71+
[3, 4, 2, 1],
72+
[4, 1, 2, 3],
73+
[4, 1, 3, 2],
74+
[4, 2, 1, 3],
75+
[4, 2, 3, 1],
76+
[4, 3, 1, 2],
77+
[4, 3, 2, 1],
78+
],
79+
],
80+
);
1481
});
1582
});

0 commit comments

Comments
 (0)