|
1 | | -const permutations = require('./permutations-solution'); |
| 1 | +const permutations = require("./permutations-solution"); |
2 | 2 |
|
3 | | -describe('permutations', () => { |
4 | | - function outcome(input, expected) { |
5 | | - const actual = permutations(input); |
6 | | - |
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); |
22 | | - }); |
23 | | - |
24 | | - test('Works for array of size one', () => { |
25 | | - [actual, expected] = outcome([1], [1]); |
| 3 | +describe("permutations", () => { |
| 4 | + test("Works for array of size one", () => { |
| 5 | + expect(permutations([1]).sort()).toEqual([1].sort()); |
26 | 6 | }); |
27 | | - test('Works for array of size two', () => { |
28 | | - [actual, expected] = outcome( |
29 | | - [1, 2], |
| 7 | + test("Works for array of size two", () => { |
| 8 | + expect(permutations([1, 2]).sort()).toEqual( |
30 | 9 | [ |
31 | 10 | [1, 2], |
32 | 11 | [2, 1], |
33 | | - ], |
| 12 | + ].sort(), |
34 | 13 | ); |
35 | 14 | }); |
36 | | - test('Works for array of size three', () => { |
37 | | - [actual, expected] = outcome( |
38 | | - [1, 2, 3], |
| 15 | + test("Works for array of size three", () => { |
| 16 | + expect(permutations([1, 2, 3]).sort()).toEqual( |
39 | 17 | [ |
40 | 18 | [1, 2, 3], |
41 | 19 | [1, 3, 2], |
42 | 20 | [2, 1, 3], |
43 | 21 | [2, 3, 1], |
44 | 22 | [3, 1, 2], |
45 | 23 | [3, 2, 1], |
46 | | - ], |
| 24 | + ].sort(), |
47 | 25 | ); |
48 | 26 | }); |
49 | | - test('Works for array of size four', () => { |
50 | | - [actual, expected] = outcome( |
51 | | - [1, 2, 3, 4], |
| 27 | + test("Works for array of size four", () => { |
| 28 | + expect(permutations([1, 2, 3, 4]).sort()).toEqual( |
52 | 29 | [ |
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 | | - ], |
| 30 | + [1, 2, 3, 4], |
| 31 | + [1, 2, 4, 3], |
| 32 | + [1, 3, 2, 4], |
| 33 | + [1, 3, 4, 2], |
| 34 | + [1, 4, 2, 3], |
| 35 | + [1, 4, 3, 2], |
| 36 | + [2, 1, 3, 4], |
| 37 | + [2, 1, 4, 3], |
| 38 | + [2, 3, 1, 4], |
| 39 | + [2, 3, 4, 1], |
| 40 | + [2, 4, 1, 3], |
| 41 | + [2, 4, 3, 1], |
| 42 | + [3, 1, 2, 4], |
| 43 | + [3, 1, 4, 2], |
| 44 | + [3, 2, 1, 4], |
| 45 | + [3, 2, 4, 1], |
| 46 | + [3, 4, 1, 2], |
| 47 | + [3, 4, 2, 1], |
| 48 | + [4, 1, 2, 3], |
| 49 | + [4, 1, 3, 2], |
| 50 | + [4, 2, 1, 3], |
| 51 | + [4, 2, 3, 1], |
| 52 | + [4, 3, 1, 2], |
| 53 | + [4, 3, 2, 1], |
| 54 | + ].sort(), |
80 | 55 | ); |
81 | 56 | }); |
82 | 57 | }); |
0 commit comments