diff --git a/lib/time-tree.js b/lib/time-tree.js index 61e3ca3..5f02d68 100644 --- a/lib/time-tree.js +++ b/lib/time-tree.js @@ -72,8 +72,14 @@ class TimeTree { * @returns {TimeTreeNode} */ [kGetParent] (parent) { - const parentNode = this.tableLabel.get(parent) - return parentNode[parentNode.length - 1] + if (parent === null) { + return null + } else if (this.tableLabel.has(parent)) { + const parentNode = this.tableLabel.get(parent) + return parentNode[parentNode.length - 1] + } else { + return null + } } /** @@ -92,7 +98,9 @@ class TimeTree { * @returns {TimeTreeNode["id"]} */ [kAddNode] (parent, label, start) { - const isRoot = parent === null + const parentNode = this[kGetParent](parent) + const isRoot = parentNode === null + if (isRoot) { this.root = { parent: null, @@ -107,7 +115,6 @@ class TimeTree { return this.root.id } - const parentNode = this[kGetParent](parent) const nodeId = `${label}-${Math.random()}` /** * @type {TimeTreeNode} diff --git a/test/lib/time-tree.test.js b/test/lib/time-tree.test.js index c0cb1a4..8bef526 100644 --- a/test/lib/time-tree.test.js +++ b/test/lib/time-tree.test.js @@ -114,6 +114,25 @@ test('TimeTree#start is adding a root element when parent is null', t => { t.type(rootNode.diff, 'number') }) +test('TimeTree#start is adding a root element when parent does not exist', t => { + t.plan(9) + + const tree = new TimeTree() + tree.start('invalid', 'root') + + const rootNode = tree.root + + t.type(rootNode, 'object') + t.equal(Object.keys(rootNode).length, 7) + t.equal(rootNode.parent, null) + t.equal(rootNode.id, 'root') + t.equal(rootNode.label, 'root') + t.ok(Array.isArray(rootNode.nodes)) + t.equal(rootNode.nodes.length, 0) + t.ok(Number.isInteger(rootNode.start)) + t.type(rootNode.diff, 'number') +}) + test('TimeTree#start parameter start can override automatically generated start time', t => { t.plan(1) diff --git a/test/plugin-loaded-so-far.test.js b/test/plugin-loaded-so-far.test.js new file mode 100644 index 0000000..bdad133 --- /dev/null +++ b/test/plugin-loaded-so-far.test.js @@ -0,0 +1,84 @@ +'use strict' + +const { test } = require('tap') +const fastq = require('fastq') +const boot = require('..') +const { Plugin } = require('../lib/plugin') + +test('loadedSoFar resolves a Promise, if plugin.loaded is set to true', async (t) => { + t.plan(1) + const app = boot({}) + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + done() + }, false, 0) + + plugin.loaded = true + + await t.resolves(plugin.loadedSoFar()) +}) + +test('loadedSoFar resolves a Promise, if plugin was loaded by avvio', async (t) => { + t.plan(2) + const app = boot({}) + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + done() + }, false, 0) + + app._loadPlugin(plugin, function (err) { + t.equal(err, undefined) + }) + + await app.ready() + + await t.resolves(plugin.loadedSoFar()) +}) + +test('loadedSoFar resolves a Promise, if .after() has no error', async t => { + t.plan(1) + const app = boot() + + app.after = function (callback) { + callback(null, () => {}) + } + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + done() + }, false, 0) + + app._loadPlugin(plugin, function () {}) + + await t.resolves(plugin.loadedSoFar()) +}) + +test('loadedSoFar rejects a Promise, if .after() has an error', async t => { + t.plan(1) + const app = boot() + + app.after = function (fn) { + fn(new Error('ArbitraryError'), () => {}) + } + + const plugin = new Plugin(fastq(app, app._loadPluginNextTick, 1), function (instance, opts, done) { + done() + }, false, 0) + + app._loadPlugin(plugin, function () {}) + + await t.rejects(plugin.loadedSoFar(), new Error('ArbitraryError')) +}) + +test('loadedSoFar resolves a Promise, if Plugin is attached to avvio after it the Plugin was instantiated', async t => { + t.plan(1) + + const plugin = new Plugin(fastq(null, null, 1), function (instance, opts, done) { + done() + }, false, 0) + + const promise = plugin.loadedSoFar() + + plugin.server = boot() + plugin.emit('start') + await t.resolves(promise) +})