-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewListNode.test.js
More file actions
48 lines (38 loc) · 1.18 KB
/
newListNode.test.js
File metadata and controls
48 lines (38 loc) · 1.18 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
const LinkedList = require("./newListNode");
describe("#insertAtHead", () => {
test("it adds the element to the beginning of the list", () => {
const ll = new LinkedList();
ll.insertAtHead(10);
const oldHead = ll.head;
ll.insertAtHead(20);
expect(ll.head.value).toBe(20);
expect(ll.head.next).toBe(oldHead);
expect(ll.length).toBe(2);
});
});
describe("#getByIndex", () => {
describe("with index less than 0", () => {
test("it returns null", () => {
const ll = LinkedList.fromValues(10, 20);
expect(ll.getByIndex(-1)).toBeNull();
});
});
describe("with index greater than list length", () => {
test("it returns null", () => {
const ll = LinkedList.fromValues(10, 20);
expect(ll.getByIndex(5)).toBeNull();
});
});
describe("with index 0", () => {
test("it returns the head", () => {
const ll = LinkedList.fromValues(10, 20);
expect(ll.getByIndex(0).value).toBe(10);
});
});
describe("with index in the middle", () => {
test("it returns the element at that index", () => {
const ll = LinkedList.fromValues(10, 20, 30, 40);
expect(ll.getByIndex(2).value).toBe(30);
});
});
});