Add jest tests.

This commit is contained in:
Oleksii Trekhleb 2018-03-27 13:03:44 +03:00
parent eb3eadaf10
commit ad0921d05e
11 changed files with 5715 additions and 86 deletions

View File

@ -1,11 +1,3 @@
{
"presets": [
[
"env", {
"targets": {
"node": "current"
}
}
]
]
"presets": ["env"]
}

8
.eslintrc Normal file
View File

@ -0,0 +1,8 @@
{
"root": true,
"extends": "airbnb",
"plugins": ["jest"],
"env": {
"jest/globals": true
}
}

1
.gitignore vendored
View File

@ -1,3 +1,2 @@
node_modules
build
.idea

5681
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -4,8 +4,8 @@
"description": "Data structures on JavaScript",
"main": "index.js",
"scripts": {
"build": "babel src -d build",
"babel-node": "babel-node"
"lint": "eslint ./src/*",
"test": "jest"
},
"repository": {
"type": "git",
@ -19,7 +19,15 @@
},
"homepage": "https://github.com/trekhleb/data-structures#readme",
"devDependencies": {
"@types/jest": "^22.2.2",
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1"
"babel-preset-env": "^1.6.1",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.9.0",
"eslint-plugin-jest": "^21.15.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"jest": "^22.4.3"
}
}

View File

@ -1,6 +1,6 @@
import { Node } from './Node';
import Node from './Node';
export class LinkedList {
export default class LinkedList {
constructor() {
this.head = null;
}
@ -17,12 +17,12 @@ export class LinkedList {
// Rewind to last node.
let currentNode = this.head;
while (currentNode.nextNode !== null) {
currentNode = currentNode.nextNode;
while (currentNode.next !== null) {
currentNode = currentNode.next;
}
// Attach new node to the end of linked list.
currentNode.nextNode = newNode;
currentNode.next = newNode;
return newNode;
}
@ -31,21 +31,19 @@ export class LinkedList {
this.head = new Node(value, this.head);
}
deleteWithValue(value) {
}
render() {
toArray() {
const listArray = [];
let currentNode = this.head;
let index = 0;
while(currentNode.nextNode !== null) {
console.log(`Node #${index} data: ${currentNode.value}`);
currentNode = currentNode.nextNode;
index++;
while (currentNode) {
listArray.push(currentNode.value);
currentNode = currentNode.next;
}
// Print the tail.
console.log(`Node #${index} data: ${currentNode.value}`);
return listArray;
}
toString() {
return this.toArray().toString();
}
}

View File

@ -0,0 +1,6 @@
export default class Node {
constructor(value, next = null) {
this.value = value;
this.next = next;
}
}

View File

@ -0,0 +1,23 @@
import LinkedList from '../LinkedList';
describe('LinkedList', () => {
it('should create empty linked list', () => {
const linkedList = new LinkedList();
expect(linkedList).toBeDefined();
});
it('should append node to linked list', () => {
const linkedList = new LinkedList();
linkedList.append(1);
linkedList.append(2);
expect(linkedList.toString()).toBe('1,2');
});
it('should prepend node to linked list', () => {
const linkedList = new LinkedList();
linkedList.append(1);
linkedList.append(2);
linkedList.prepend(3);
expect(linkedList.toString()).toBe('3,1,2');
});
});

View File

@ -0,0 +1,9 @@
import Node from '../Node';
describe('Node', () => {
it('should create list node with value', () => {
const node = new Node(1);
expect(node.value).toBe(1);
expect(node.next).toBeNull();
});
});

View File

@ -1,6 +0,0 @@
export class Node {
constructor(value, nextNode = null) {
this.value = value;
this.nextNode = nextNode;
}
}

View File

@ -1,11 +0,0 @@
import { LinkedList } from "./LinkedList";
const linkedList = new LinkedList();
linkedList.append(1);
linkedList.append(2);
linkedList.append(3);
linkedList.prepend(4);
linkedList.prepend(5);
linkedList.render();