Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loadedSoFar, add unit tests #226

Merged
merged 6 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/time-tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

/**
Expand All @@ -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,
Expand All @@ -107,7 +115,6 @@ class TimeTree {
return this.root.id
}

const parentNode = this[kGetParent](parent)
const nodeId = `${label}-${Math.random()}`
/**
* @type {TimeTreeNode}
Expand Down
19 changes: 19 additions & 0 deletions test/lib/time-tree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
84 changes: 84 additions & 0 deletions test/plugin-loaded-so-far.test.js
Original file line number Diff line number Diff line change
@@ -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)
})