Skip to content

Commit ad0921d

Browse files
committedMar 27, 2018
Add jest tests.
1 parent eb3eada commit ad0921d

File tree

11 files changed

+6561
-932
lines changed

11 files changed

+6561
-932
lines changed
 

‎.babelrc

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
11
{
2-
"presets": [
3-
[
4-
"env", {
5-
"targets": {
6-
"node": "current"
7-
}
8-
}
9-
]
10-
]
2+
"presets": ["env"]
113
}

‎.eslintrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"root": true,
3+
"extends": "airbnb",
4+
"plugins": ["jest"],
5+
"env": {
6+
"jest/globals": true
7+
}
8+
}

‎.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
node_modules
2-
build
32
.idea

‎package-lock.json

+6,488-885
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+11-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"description": "Data structures on JavaScript",
55
"main": "index.js",
66
"scripts": {
7-
"build": "babel src -d build",
8-
"babel-node": "babel-node"
7+
"lint": "eslint ./src/*",
8+
"test": "jest"
99
},
1010
"repository": {
1111
"type": "git",
@@ -19,7 +19,15 @@
1919
},
2020
"homepage": "https://github.com/trekhleb/data-structures#readme",
2121
"devDependencies": {
22+
"@types/jest": "^22.2.2",
2223
"babel-cli": "^6.26.0",
23-
"babel-preset-env": "^1.6.1"
24+
"babel-preset-env": "^1.6.1",
25+
"eslint": "^4.19.1",
26+
"eslint-config-airbnb": "^16.1.0",
27+
"eslint-plugin-import": "^2.9.0",
28+
"eslint-plugin-jest": "^21.15.0",
29+
"eslint-plugin-jsx-a11y": "^6.0.3",
30+
"eslint-plugin-react": "^7.7.0",
31+
"jest": "^22.4.3"
2432
}
2533
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Node } from './Node';
1+
import Node from './Node';
22

3-
export class LinkedList {
3+
export default class LinkedList {
44
constructor() {
55
this.head = null;
66
}
@@ -17,12 +17,12 @@ export class LinkedList {
1717

1818
// Rewind to last node.
1919
let currentNode = this.head;
20-
while (currentNode.nextNode !== null) {
21-
currentNode = currentNode.nextNode;
20+
while (currentNode.next !== null) {
21+
currentNode = currentNode.next;
2222
}
2323

2424
// Attach new node to the end of linked list.
25-
currentNode.nextNode = newNode;
25+
currentNode.next = newNode;
2626

2727
return newNode;
2828
}
@@ -31,21 +31,19 @@ export class LinkedList {
3131
this.head = new Node(value, this.head);
3232
}
3333

34-
deleteWithValue(value) {
35-
36-
}
37-
38-
render() {
34+
toArray() {
35+
const listArray = [];
3936
let currentNode = this.head;
40-
let index = 0;
4137

42-
while(currentNode.nextNode !== null) {
43-
console.log(`Node #${index} data: ${currentNode.value}`);
44-
currentNode = currentNode.nextNode;
45-
index++;
38+
while (currentNode) {
39+
listArray.push(currentNode.value);
40+
currentNode = currentNode.next;
4641
}
4742

48-
// Print the tail.
49-
console.log(`Node #${index} data: ${currentNode.value}`);
43+
return listArray;
44+
}
45+
46+
toString() {
47+
return this.toArray().toString();
5048
}
5149
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default class Node {
2+
constructor(value, next = null) {
3+
this.value = value;
4+
this.next = next;
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import LinkedList from '../LinkedList';
2+
3+
describe('LinkedList', () => {
4+
it('should create empty linked list', () => {
5+
const linkedList = new LinkedList();
6+
expect(linkedList).toBeDefined();
7+
});
8+
9+
it('should append node to linked list', () => {
10+
const linkedList = new LinkedList();
11+
linkedList.append(1);
12+
linkedList.append(2);
13+
expect(linkedList.toString()).toBe('1,2');
14+
});
15+
16+
it('should prepend node to linked list', () => {
17+
const linkedList = new LinkedList();
18+
linkedList.append(1);
19+
linkedList.append(2);
20+
linkedList.prepend(3);
21+
expect(linkedList.toString()).toBe('3,1,2');
22+
});
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Node from '../Node';
2+
3+
describe('Node', () => {
4+
it('should create list node with value', () => {
5+
const node = new Node(1);
6+
expect(node.value).toBe(1);
7+
expect(node.next).toBeNull();
8+
});
9+
});

‎src/data_structures/linked_list/Node.js

-6
This file was deleted.

‎src/data_structures/linked_list/index.js

-11
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.