-
-
Notifications
You must be signed in to change notification settings - Fork 283
Expand file tree
/
Copy pathdedupe.test.js
More file actions
72 lines (59 loc) · 2.15 KB
/
dedupe.test.js
File metadata and controls
72 lines (59 loc) · 2.15 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
const dedupe = require("./dedupe");
/*
Dedupe Array
📖 Dedupe means **deduplicate**
In this kata, you will need to deduplicate the elements of an array
E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c']
E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8]
E.g. dedupe([1, 2, 1]) target output: [1, 2]
*/
// Acceptance Criteria:
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
describe("dedupe()", () => {
[{ input: [], expected: [] }].forEach(({ input, expected }) =>
it(`given an empty array, it returns an empty array [${input}]`, () => {
expect(dedupe(input)).toStrictEqual(expected);
})
);
// Given an array with no duplicates
// Then it should return a copy of the original array
[{ input: [1, 2, 3, 4], expected: [1, 2, 3, 4] }].forEach(
({ input, expected }) =>
it(`should return same input values [${input}] without duplicate`, () => {
expect(dedupe(input)).toStrictEqual(expected);
expect(dedupe(input)).not.toBe(expected);
})
);
// When passed to the dedupe function
// Given an array with strings or numbers
// When passed to the dedupe function
// Then it should remove the duplicate values, preserving the first occurence of each element
[
{ input: [1, 2, 5, 5, "a", 5, 10, 10, "a"], expected: [1, 2, 5, "a", 10] },
{
input: ["apple", "banana", "orange", "apple", "banana", 1, 3, 4, 1],
expected: ["apple", "banana", "orange", 1, 3, 4],
},
].forEach(({ input, expected }) =>
it(`should return deduplicated array for [${input}]`, () => {
expect(dedupe(input)).toStrictEqual(expected);
})
);
});
test("returns a copy, not the original array", () => {
const input = [1, 1, 2];
const result = dedupe(input);
expect(result).toStrictEqual([1, 2]);
expect(result).not.toBe(input);
});
it("does not mutate the original array", () => {
const input = [2, 3, 5];
dedupe(input);
expect(input).toStrictEqual([2, 3, 5]);
});
test("result has only distinct elements", () => {
const result = dedupe([1, 1, 2, 2, 3]);
expect(new Set(result).size).toBe(result.length);
});