Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasnordquist committed Apr 3, 2019
1 parent b9eb54d commit acbaced
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
25 changes: 14 additions & 11 deletions backend/src/Model/spec/TreeNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import 'mocha'

import { TreeNodeFactory } from '../'
import { expect } from 'chai'
import { Base64Message } from '../Base64Message';

describe('TreeNode', () => {
const number3 = Base64Message.fromString("3")
const number5 = Base64Message.fromString("5")
it('firstNode should retrieve first node', () => {
const topics = 'foo/bar'.split('/')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, undefined)
Expand All @@ -13,40 +16,40 @@ describe('TreeNode', () => {

it('updateWithNode should update value', () => {
const topics = 'foo/bar'.split('/')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, '3')
expect(leaf.message && leaf.message.value).to.eq('3')
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics, '5')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics, Base64Message.fromString('3'))
expect(Base64Message.toUnicodeString(leaf.message!.value!)).to.eq('3')
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics, Base64Message.fromString('5'))

const root = leaf.firstNode()
root.updateWithNode(updateLeave.firstNode())

expect(root.sourceEdge).to.eq(undefined)
expect(leaf.message && leaf.message.value).to.eq('5')
expect(Base64Message.toUnicodeString(leaf.message!.value!)).to.eq('5')
})

it('updateWithNode should update intermediate nodes', () => {
const topics1 = 'foo/bar/baz'.split('/')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics1, '3')
expect(leaf.message && leaf.message.value).to.eq('3')
const leaf = TreeNodeFactory.fromEdgesAndValue(topics1, Base64Message.fromString('3'))
expect(Base64Message.toUnicodeString(leaf.message!.value!)).to.eq('3')

const topics2 = 'foo/bar'.split('/')
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics2, '5')
const updateLeave = TreeNodeFactory.fromEdgesAndValue(topics2, Base64Message.fromString('5'))
leaf.firstNode().updateWithNode(updateLeave.firstNode())

const barNode = leaf.firstNode().findNode('foo/bar')
expect(barNode && barNode.sourceEdge && barNode.sourceEdge.name).to.eq('bar')
expect(barNode && barNode.message && barNode.message.value).to.eq('5')
expect(Base64Message.toUnicodeString(barNode!.message!.value!)).to.eq('5')

expect(leaf.sourceEdge && leaf.sourceEdge.name).to.eq('baz')
expect(leaf.message && leaf.message.value).to.eq('3')
expect(Base64Message.toUnicodeString(leaf.message!.value!)).to.eq('3')
})

it('updateWithNode should add nodes to the tree', () => {
const topics1 = 'foo/bar'.split('/')
const leaf1 = TreeNodeFactory.fromEdgesAndValue(topics1, 'foo')
const leaf1 = TreeNodeFactory.fromEdgesAndValue(topics1, Base64Message.fromString('foo'))

const topics2 = 'foo/bar/baz'.split('/')
const leaf2 = TreeNodeFactory.fromEdgesAndValue(topics2, 'bar')
const leaf2 = TreeNodeFactory.fromEdgesAndValue(topics2, Base64Message.fromString('bar'))

leaf1.firstNode().updateWithNode(leaf2.firstNode())

Expand Down
9 changes: 5 additions & 4 deletions backend/src/Model/spec/TreeNodeFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'mocha'

import { TreeNodeFactory } from '../'
import { expect } from 'chai'
import { Base64Message } from '../Base64Message';

describe('TreeNodeFactory', () => {
it('root node must not have a sourceEdge', () => {
Expand All @@ -15,7 +16,7 @@ describe('TreeNodeFactory', () => {
it('should create node', () => {
const topic = 'foo/bar'
const edges = topic.split('/')
const node = TreeNodeFactory.fromEdgesAndValue(edges, '5')
const node = TreeNodeFactory.fromEdgesAndValue(edges, Base64Message.fromString('5'))

if (!node.sourceEdge || !node.sourceEdge.source || !node.message) {
expect.fail('should not happen')
Expand All @@ -24,7 +25,7 @@ describe('TreeNodeFactory', () => {

expect(node).to.not.eq(undefined)
expect(node.sourceEdge.name).to.eq('bar')
expect(node.message.value).to.eq('5')
expect(Base64Message.toUnicodeString(node.message.value!)).to.eq('5')

const foo = node.firstNode().findNode('foo')
expect(foo && foo.sourceEdge && foo.sourceEdge.name).to.eq('foo')
Expand All @@ -33,14 +34,14 @@ describe('TreeNodeFactory', () => {
it('node should contain edges in order', () => {
const topic = 'foo/bar/baz'
const edges = topic.split('/')
const node = TreeNodeFactory.fromEdgesAndValue(edges, '5')
const node = TreeNodeFactory.fromEdgesAndValue(edges, Base64Message.fromString('5'))

if (!node.sourceEdge || !node.sourceEdge.source || !node.message) {
expect.fail('should not happen')
return
}

expect(node.message.value).to.eq('5')
expect(Base64Message.toUnicodeString(node.message.value!)).to.eq('5')
expect(node.sourceEdge.name).to.eq('baz')

const barNode = node.sourceEdge.source
Expand Down

0 comments on commit acbaced

Please sign in to comment.