|
1 | | -const dedupe = require("./dedupe.js"); |
2 | | -/* |
3 | | -Dedupe Array |
| 1 | +const dedupe = require("./dedupe"); |
4 | 2 |
|
5 | | -📖 Dedupe means **deduplicate** |
| 3 | +describe("dedupe", () => { |
| 4 | + // Given an empty array |
| 5 | + // When passed to the dedupe function |
| 6 | + // Then it should return an empty array |
| 7 | + test.todo("given an empty array, it returns an empty array"); |
| 8 | + test("given an empty array, it returns an empty array", () => { |
| 9 | + const result = dedupe([]); |
| 10 | + expect(result).toEqual([]); |
| 11 | + }); |
6 | 12 |
|
7 | | -In this kata, you will need to deduplicate the elements of an array |
| 13 | + // Given an array with no duplicates |
| 14 | + // When passed to the dedupe function |
| 15 | + // Then it should return a copy of the original array |
| 16 | + test("given an array with no duplicates, it returns a copy of the original array", () => { |
| 17 | + const input = [1, 2, 3, 4]; |
| 18 | + const result = dedupe(input); |
8 | 19 |
|
9 | | -E.g. dedupe(['a','a','a','b','b','c']) target output: ['a','b','c'] |
10 | | -E.g. dedupe([5, 1, 1, 2, 3, 2, 5, 8]) target output: [5, 1, 2, 3, 8] |
11 | | -E.g. dedupe([1, 2, 1]) target output: [1, 2] |
12 | | -*/ |
| 20 | + expect(result).toEqual([1, 2, 3, 4]); |
| 21 | + expect(result).not.toBe(input); // should be a new copy |
| 22 | + }); |
13 | 23 |
|
14 | | -// Acceptance Criteria: |
| 24 | + // Given an array with strings |
| 25 | + // When passed to the dedupe function |
| 26 | + // Then it should remove duplicates preserving first occurrence |
| 27 | + test("removes duplicate strings preserving first occurrence", () => { |
| 28 | + const input = ["a", "a", "a", "b", "b", "c"]; |
| 29 | + const result = dedupe(input); |
15 | 30 |
|
16 | | -// Given an empty array |
17 | | -// When passed to the dedupe function |
18 | | -// Then it should return an empty array |
19 | | -test.todo("given an empty array, it returns an empty array"); |
| 31 | + expect(result).toEqual(["a", "b", "c"]); |
| 32 | + }); |
20 | 33 |
|
21 | | -// Given an array with no duplicates |
22 | | -// When passed to the dedupe function |
23 | | -// Then it should return a copy of the original array |
| 34 | + // Given an array with numbers |
| 35 | + // When passed to the dedupe function |
| 36 | + // Then it should remove the duplicate values, preserving the first occurence of each element |
| 37 | + test("removes duplicate numbers preserving first occurrence", () => { |
| 38 | + const input = [5, 1, 1, 2, 3, 2, 5, 8]; |
| 39 | + const result = dedupe(input); |
24 | 40 |
|
25 | | -// Given an array with strings or numbers |
26 | | -// When passed to the dedupe function |
27 | | -// Then it should remove the duplicate values, preserving the first occurence of each element |
| 41 | + expect(result).toEqual([5, 1, 2, 3, 8]); |
| 42 | + }); |
| 43 | + |
| 44 | + test("removes duplicates in mixed order", () => { |
| 45 | + const input = [1, 2, 1]; |
| 46 | + const result = dedupe(input); |
| 47 | + |
| 48 | + expect(result).toEqual([1, 2]); |
| 49 | + }); |
| 50 | +}); |
0 commit comments