generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathdedupe.test.js
63 lines (50 loc) · 2.24 KB
/
dedupe.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
const dedupe = require("./dedupe.js");
/*
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:
describe('dedupe', () => {
// Given an empty array
// When passed to the dedupe function
// Then it should return an empty array
it("given an empty array, it returns an empty array", () => {
expect(dedupe([])).toEqual([]);
});
// Given an array with no duplicates
// When passed to the dedupe function
// Then it should return a copy of the original array
it("given an array with no duplicates, it returns a copy of the original array", () => {
const arr = [1, 2, 3];
expect(dedupe(arr)).toEqual(arr);
});
it("given an array of strings with no duplicates, it returns a copy of the original array", () => {
const arr = ['a', 'b', 'c'];
expect(dedupe(arr)).toEqual(arr);
});
// 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
it("given an array with duplicate numbers, it removes the duplicate values, preserving the first occurrence", () => {
expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]);
});
it("given an array with duplicate strings, it removes the duplicate values, preserving the first occurrence", () => {
expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']);
});
it("given an array with mixed duplicate types, it removes the duplicate values, preserving the first occurrence", () => {
expect(dedupe([1, '1', 1, '2', 2])).toEqual([1, '1', '2', 2]);
});
it("given an array with duplicate values in different order, it removes the duplicate values, preserving the first occurrence", () => {
expect(dedupe([1, 2, 1])).toEqual([1, 2]);
});
it("given an invalid input, it returns an empty array", () => {
expect(dedupe(null)).toEqual([]);
expect(dedupe(undefined)).toEqual([]);
expect(dedupe({})).toEqual([]);
expect(dedupe("string")).toEqual([]);
});
});