data-structures-es6
Version:
JavaScript data structures written in es6 (es 2015)
235 lines (188 loc) • 4.75 kB
JavaScript
let expect = require('expect')
let LinkedList = require('./../index.js').LinkedList
describe('LinkedList', () => {
let list
beforeEach(() => {
list = new LinkedList()
})
describe('#push', () => {
it('check push', () => {
list.push(1)
let firstNode = list.head
expect(firstNode).toEqual(list.head)
expect(firstNode).toEqual(list.tail)
expect(firstNode.data).toBe(1)
list.push(2)
let secondNode = list.head.next
expect(secondNode.data).toBe(2)
expect(secondNode).toEqual(list.tail)
list.push(3)
let thirdNode = list.head.next.next
expect(thirdNode).toEqual(list.tail)
expect(thirdNode.data).toBe(3)
})
})
describe('#length', () => {
it('should be 0 on emty list', () => {
expect(list.length).toBe(0)
})
it('should be 0 on emty list', () => {
list.push(1)
list.push(2)
list.removeAt(0)
list.removeAt(0)
expect(list.length).toBe(0)
})
it('should be 2', () => {
list.push(1)
list.push(2)
expect(list.length).toBe(2)
})
})
describe('#head', () => {
it('should be null on empty list', () => {
expect(list.head).toBe(null)
})
it('should be null on empty list', () => {
list.push(1)
list.push(2)
list.removeAt(0)
list.removeAt(0)
expect(list.head).toBe(null)
})
it('should be first element', () => {
list.push(1)
list.push(2)
expect(list.head.data).toBe(1)
})
})
describe('#tail', () => {
it('should be null on emty list', () => {
expect(list.tail).toBe(null)
})
it('should be first item', () => {
list.push(1)
expect(list.tail.data).toBe(1)
})
it('should be last item in list', () => {
list.push(1)
list.push(2)
expect(list.tail.data).toBe(2)
})
it('should changed after removing', () => {
list.push(1)
list.push(2)
list.removeAt(1)
expect(list.tail.data).toBe(1)
list.removeAt(0)
expect(list.tail).toBe(null)
})
})
describe('#at', () => {
it('check existing elements', () => {
list.push(1)
list.push(2)
list.push(3)
expect(list.at(0).data).toBe(1)
expect(list.at(1).data).toBe(2)
expect(list.at(2).data).toBe(3)
})
it('position less than 0', () => {
list.push(1)
expect(() => {
list.at(-1)
}).toThrow()
})
it('position bigger than exist', () => {
list.push(1)
expect(() => {
list.at(1)
}).toThrow()
})
})
describe('#removeAt', () => {
it('remove first node', () => {
list.push(1)
list.push(2)
list.removeAt(0)
expect(list.head.data).toBe(2)
expect(list.length).toBe(1)
})
it('remove last node', () => {
list.push(1)
list.push(2)
list.removeAt(1) //second element
expect(list.tail.data).toBe(1)
expect(list.length).toBe(1)
})
it('remove node in the middle', () => {
list.push(1)
list.push(2)
list.push(3)
list.removeAt(1) //second element
expect(list.at(1).data).toBe(3)
expect(list.length).toBe(2)
})
it('remove not existed node', () => {
list.push(1)
expect(() => {
list.removeAt(1)
}).toThrow()
})
})
describe('#current', () => {
it('should be null on empty list', () => {
expect(list.current).toBe(null)
})
it('should be first element', () => {
list.push(1)
list.push(2)
expect(list.current.data).toBe(1)
})
it('should be second element', () => {
list.push(1)
list.push(2)
list.next()
expect(list.current.data).toBe(2)
})
it('should be last element', () => {
list.push(1)
list.push(2)
list.next()
list.next()
list.next()
expect(list.current.data).toBe(2)
})
})
describe('#next', () => {
it('should return next element', () => {
list.push(1)
list.push(2)
let next = list.next()
expect(next.data).toBe(2)
})
it('if next element doen\'t exist return null', () => {
list.push(1)
list.push(2)
list.next()
let nextNext = list.next()
expect(nextNext).toBe(null)
})
})
describe('#resetCursor', () => {
it('should be null on empty list', () => {
list.push(1)
list.removeAt(0)
list.resetCursor()
expect(list.current).toBe(null)
})
it('should be first element on non empty list', () => {
list.push(1)
list.push(2)
list.next()
list.resetCursor()
expect(list.current.data).toBe(1)
})
})
})