CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
close.test.js545 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('tap')4const boot = require('..')5const { AVV_ERR_CALLBACK_NOT_FN } = require('../lib/errors')6 7test('boot an app with a plugin', (t) => {8  t.plan(4)9 10  const app = boot()11  let last = false12 13  app.use(function (server, opts, done) {14    app.onClose(() => {15      t.ok('onClose called')16      t.notOk(last)17      last = true18    })19    done()20  })21 22  app.on('start', () => {23    app.close(() => {24      t.ok(last)25      t.pass('Closed in the correct order')26    })27  })28})29 30test('onClose arguments', (t) => {31  t.plan(5)32 33  const app = boot()34 35  app.use(function (server, opts, next) {36    server.onClose((instance, done) => {37      t.ok('called')38      t.equal(server, instance)39      done()40    })41    next()42  })43 44  app.use(function (server, opts, next) {45    server.onClose((instance) => {46      t.ok('called')47      t.equal(server, instance)48    })49    next()50  })51 52  app.on('start', () => {53    app.close(() => {54      t.pass('Closed in the correct order')55    })56  })57})58 59test('onClose arguments - fastify encapsulation test case', (t) => {60  t.plan(5)61 62  const server = { my: 'server' }63  const app = boot(server)64 65  app.override = function (s, fn, opts) {66    s = Object.create(s)67    return s68  }69 70  app.use(function (instance, opts, next) {71    instance.test = true72    instance.onClose((i, done) => {73      t.ok(i.test)74      done()75    })76    next()77  })78 79  app.use(function (instance, opts, next) {80    t.notOk(instance.test)81    instance.onClose((i, done) => {82      t.notOk(i.test)83      done()84    })85    next()86  })87 88  app.on('start', () => {89    t.notOk(app.test)90    app.close(() => {91      t.pass('Closed in the correct order')92    })93  })94})95 96test('onClose arguments - fastify encapsulation test case / 2', (t) => {97  t.plan(5)98 99  const server = { my: 'server' }100  const app = boot(server)101 102  app.override = function (s, fn, opts) {103    s = Object.create(s)104    return s105  }106 107  server.use(function (instance, opts, next) {108    instance.test = true109    instance.onClose((i, done) => {110      t.ok(i.test)111      done()112    })113    next()114  })115 116  server.use(function (instance, opts, next) {117    t.notOk(instance.test)118    instance.onClose((i, done) => {119      t.notOk(i.test)120      done()121    })122    next()123  })124 125  app.on('start', () => {126    t.notOk(server.test)127    try {128      server.close()129      t.pass()130    } catch (err) {131      t.fail(err)132    }133  })134})135 136test('onClose arguments - encapsulation test case no server', (t) => {137  t.plan(5)138 139  const app = boot()140 141  app.override = function (s, fn, opts) {142    s = Object.create(s)143    return s144  }145 146  app.use(function (instance, opts, next) {147    instance.test = true148    instance.onClose((i, done) => {149      t.notOk(i.test)150      done()151    })152    next()153  })154 155  app.use(function (instance, opts, next) {156    t.notOk(instance.test)157    instance.onClose((i) => {158      t.notOk(i.test)159    })160    next()161  })162 163  app.on('start', () => {164    t.notOk(app.test)165    app.close(() => {166      t.pass('Closed in the correct order')167    })168  })169})170 171test('onClose should handle errors', (t) => {172  t.plan(3)173 174  const app = boot()175 176  app.use(function (server, opts, done) {177    app.onClose((instance, done) => {178      t.ok('called')179      done(new Error('some error'))180    })181    done()182  })183 184  app.on('start', () => {185    app.close(err => {186      t.equal(err.message, 'some error')187      t.pass('Closed in the correct order')188    })189  })190})191 192test('#54 close handlers should receive same parameters when queue is not empty', (t) => {193  t.plan(6)194 195  const context = { test: true }196  const app = boot(context)197 198  app.use(function (server, opts, done) {199    done()200  })201  app.on('start', () => {202    app.close((err, done) => {203      t.equal(err, null)204      t.pass('Closed in the correct order')205      setImmediate(done)206    })207    app.close(err => {208      t.equal(err, null)209      t.pass('Closed in the correct order')210    })211    app.close(err => {212      t.equal(err, null)213      t.pass('Closed in the correct order')214    })215  })216})217 218test('onClose should handle errors / 2', (t) => {219  t.plan(4)220 221  const app = boot()222 223  app.onClose((instance, done) => {224    t.ok('called')225    done(new Error('some error'))226  })227 228  app.use(function (server, opts, done) {229    app.onClose((instance, done) => {230      t.ok('called')231      done()232    })233    done()234  })235 236  app.on('start', () => {237    app.close(err => {238      t.equal(err.message, 'some error')239      t.pass('Closed in the correct order')240    })241  })242})243 244test('close arguments', (t) => {245  t.plan(4)246 247  const app = boot()248 249  app.use(function (server, opts, done) {250    app.onClose((instance, done) => {251      t.ok('called')252      done()253    })254    done()255  })256 257  app.on('start', () => {258    app.close((err, instance, done) => {259      t.error(err)260      t.equal(instance, app)261      done()262      t.pass('Closed in the correct order')263    })264  })265})266 267test('close event', (t) => {268  t.plan(3)269 270  const app = boot()271  let last = false272 273  app.on('start', () => {274    app.close(() => {275      t.notOk(last)276      last = true277    })278  })279 280  app.on('close', () => {281    t.ok(last)282    t.pass('event fired')283  })284})285 286test('close order', (t) => {287  t.plan(5)288 289  const app = boot()290  const order = [1, 2, 3, 4]291 292  app.use(function (server, opts, done) {293    app.onClose(() => {294      t.equal(order.shift(), 3)295    })296 297    app.use(function (server, opts, done) {298      app.onClose(() => {299        t.equal(order.shift(), 2)300      })301      done()302    })303    done()304  })305 306  app.use(function (server, opts, done) {307    app.onClose(() => {308      t.equal(order.shift(), 1)309    })310    done()311  })312 313  app.on('start', () => {314    app.close(() => {315      t.equal(order.shift(), 4)316      t.pass('Closed in the correct order')317    })318  })319})320 321test('close without a cb', (t) => {322  t.plan(1)323 324  const app = boot()325 326  app.onClose((instance, done) => {327    t.ok('called')328    done()329  })330 331  app.close()332})333 334test('onClose with 0 parameters', (t) => {335  t.plan(4)336 337  const server = { my: 'server' }338  const app = boot(server)339 340  app.use(function (instance, opts, next) {341    instance.onClose(function () {342      t.ok('called')343      t.equal(arguments.length, 0)344    })345    next()346  })347 348  app.close(err => {349    t.error(err)350    t.pass('Closed')351  })352})353 354test('onClose with 1 parameter', (t) => {355  t.plan(3)356 357  const server = { my: 'server' }358  const app = boot(server)359 360  app.use(function (instance, opts, next) {361    instance.onClose(function (context) {362      t.equal(arguments.length, 1)363    })364    next()365  })366 367  app.close(err => {368    t.error(err)369    t.pass('Closed')370  })371})372 373test('close passing not a function', (t) => {374  t.plan(1)375 376  const app = boot()377 378  app.onClose((instance, done) => {379    t.ok('called')380    done()381  })382 383  t.throws(() => app.close({}), { message: 'not a function' })384})385 386test('close passing not a function', (t) => {387  t.plan(1)388 389  const app = boot()390 391  app.onClose((instance, done) => {392    t.ok('called')393    done()394  })395 396  t.throws(() => app.close({}), { message: 'not a function' })397})398 399test('close passing not a function when wrapping', (t) => {400  t.plan(1)401 402  const app = {}403  boot(app)404 405  app.onClose((instance, done) => {406    t.ok('called')407    done()408  })409 410  t.throws(() => app.close({}), { message: 'not a function' })411})412 413test('close should trigger ready()', (t) => {414  t.plan(2)415 416  const app = boot(null, {417    autostart: false418  })419 420  app.on('start', () => {421    // this will be emitted after the422    // callback in close() is fired423    t.pass('started')424  })425 426  app.close(() => {427    t.pass('closed')428  })429})430 431test('close without a cb returns a promise', (t) => {432  t.plan(1)433 434  const app = boot()435  app.close().then(() => {436    t.pass('promise resolves')437  })438})439 440test('close without a cb returns a promise when attaching to a server', (t) => {441  t.plan(1)442 443  const server = {}444  boot(server)445  server.close().then(() => {446    t.pass('promise resolves')447  })448})449 450test('close with async onClose handlers', t => {451  t.plan(7)452 453  const app = boot()454  const order = [1, 2, 3, 4, 5, 6]455 456  app.onClose(() => {457    return new Promise(resolve => setTimeout(resolve, 500)).then(() => {458      t.equal(order.shift(), 5)459    })460  })461 462  app.onClose(() => {463    t.equal(order.shift(), 4)464  })465 466  app.onClose(instance => {467    return new Promise(resolve => setTimeout(resolve, 500)).then(() => {468      t.equal(order.shift(), 3)469    })470  })471 472  app.onClose(async instance => {473    return new Promise(resolve => setTimeout(resolve, 500)).then(() => {474      t.equal(order.shift(), 2)475    })476  })477 478  app.onClose(async () => {479    return new Promise(resolve => setTimeout(resolve, 500)).then(() => {480      t.equal(order.shift(), 1)481    })482  })483 484  app.on('start', () => {485    app.close(() => {486      t.equal(order.shift(), 6)487      t.pass('Closed in the correct order')488    })489  })490})491 492test('onClose callback must be a function', (t) => {493  t.plan(1)494 495  const app = boot()496 497  app.use(function (server, opts, done) {498    t.throws(() => app.onClose({}), new AVV_ERR_CALLBACK_NOT_FN('onClose', 'object'))499    done()500  })501})502 503test('close custom server with async onClose handlers', t => {504  t.plan(7)505 506  const server = {}507  const app = boot(server)508  const order = [1, 2, 3, 4, 5, 6]509 510  server.onClose(() => {511    return new Promise(resolve => setTimeout(resolve, 500)).then(() => {512      t.equal(order.shift(), 5)513    })514  })515 516  server.onClose(() => {517    t.equal(order.shift(), 4)518  })519 520  server.onClose(instance => {521    return new Promise(resolve => setTimeout(resolve, 500)).then(() => {522      t.equal(order.shift(), 3)523    })524  })525 526  server.onClose(async instance => {527    return new Promise(resolve => setTimeout(resolve, 500)).then(() => {528      t.equal(order.shift(), 2)529    })530  })531 532  server.onClose(async () => {533    return new Promise(resolve => setTimeout(resolve, 500)).then(() => {534      t.equal(order.shift(), 1)535    })536  })537 538  app.on('start', () => {539    app.close(() => {540      t.equal(order.shift(), 6)541      t.pass('Closed in the correct order')542    })543  })544})545