CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
override.test.js375 linesDownload Raw Back to test
1'use strict'2 3/* eslint no-prototype-builtins: off */4 5const { test } = require('tap')6const boot = require('..')7 8test('custom inheritance', (t) => {9  t.plan(3)10 11  const server = { my: 'server' }12  const app = boot(server)13 14  app.override = function (s) {15    t.equal(s, server)16 17    const res = Object.create(s)18    res.b = 4219 20    return res21  }22 23  app.use(function first (s, opts, cb) {24    t.not(s, server)25    t.ok(Object.prototype.isPrototypeOf.call(server, s))26    cb()27  })28})29 30test('custom inheritance multiple levels', (t) => {31  t.plan(6)32 33  const server = { count: 0 }34  const app = boot(server)35 36  app.override = function (s) {37    const res = Object.create(s)38    res.count = res.count + 139 40    return res41  }42 43  app.use(function first (s1, opts, cb) {44    t.not(s1, server)45    t.ok(Object.prototype.isPrototypeOf.call(server, s1))46    t.equal(s1.count, 1)47    s1.use(second)48 49    cb()50 51    function second (s2, opts, cb) {52      t.not(s2, s1)53      t.ok(Object.prototype.isPrototypeOf.call(s1, s2))54      t.equal(s2.count, 2)55      cb()56    }57  })58})59 60test('custom inheritance multiple levels twice', (t) => {61  t.plan(10)62 63  const server = { count: 0 }64  const app = boot(server)65 66  app.override = function (s) {67    const res = Object.create(s)68    res.count = res.count + 169 70    return res71  }72 73  app.use(function first (s1, opts, cb) {74    t.not(s1, server)75    t.ok(Object.prototype.isPrototypeOf.call(server, s1))76    t.equal(s1.count, 1)77    s1.use(second)78    s1.use(third)79    let prev80 81    cb()82 83    function second (s2, opts, cb) {84      prev = s285      t.not(s2, s1)86      t.ok(Object.prototype.isPrototypeOf.call(s1, s2))87      t.equal(s2.count, 2)88      cb()89    }90 91    function third (s3, opts, cb) {92      t.not(s3, s1)93      t.ok(Object.prototype.isPrototypeOf.call(s1, s3))94      t.notOk(Object.prototype.isPrototypeOf.call(prev, s3))95      t.equal(s3.count, 2)96      cb()97    }98  })99})100 101test('custom inheritance multiple levels with multiple heads', (t) => {102  t.plan(13)103 104  const server = { count: 0 }105  const app = boot(server)106 107  app.override = function (s) {108    const res = Object.create(s)109    res.count = res.count + 1110 111    return res112  }113 114  app.use(function first (s1, opts, cb) {115    t.not(s1, server)116    t.ok(Object.prototype.isPrototypeOf.call(server, s1))117    t.equal(s1.count, 1)118    s1.use(second)119 120    cb()121 122    function second (s2, opts, cb) {123      t.not(s2, s1)124      t.ok(Object.prototype.isPrototypeOf.call(s1, s2))125      t.equal(s2.count, 2)126      cb()127    }128  })129 130  app.use(function third (s1, opts, cb) {131    t.not(s1, server)132    t.ok(Object.prototype.isPrototypeOf.call(server, s1))133    t.equal(s1.count, 1)134    s1.use(fourth)135 136    cb()137 138    function fourth (s2, opts, cb) {139      t.not(s2, s1)140      t.ok(Object.prototype.isPrototypeOf.call(s1, s2))141      t.equal(s2.count, 2)142      cb()143    }144  })145 146  app.ready(function () {147    t.equal(server.count, 0)148  })149})150 151test('fastify test case', (t) => {152  t.plan(7)153 154  const noop = () => {}155 156  function build () {157    const app = boot(server, {})158    app.override = function (s) {159      return Object.create(s)160    }161 162    server.add = function (name, fn, cb) {163      if (this[name]) return cb(new Error('already existent'))164      this[name] = fn165      cb()166    }167 168    return server169 170    function server (req, res) {}171  }172 173  const instance = build()174  t.ok(instance.add)175  t.ok(instance.use)176 177  instance.use((i, opts, cb) => {178    t.not(i, instance)179    t.ok(Object.prototype.isPrototypeOf.call(instance, i))180 181    i.add('test', noop, (err) => {182      t.error(err)183      t.ok(i.test)184      cb()185    })186  })187 188  instance.ready(() => {189    t.notOk(instance.test)190  })191})192 193test('override should pass also the plugin function', (t) => {194  t.plan(3)195 196  const server = { my: 'server' }197  const app = boot(server)198 199  app.override = function (s, fn) {200    t.type(fn, 'function')201    t.equal(fn, first)202    return s203  }204 205  app.use(first)206 207  function first (s, opts, cb) {208    t.equal(s, server)209    cb()210  }211})212 213test('skip override - fastify test case', (t) => {214  t.plan(2)215 216  const server = { my: 'server' }217  const app = boot(server)218 219  app.override = function (s, func) {220    if (func[Symbol.for('skip-override')]) {221      return s222    }223    return Object.create(s)224  }225 226  first[Symbol.for('skip-override')] = true227  app.use(first)228 229  function first (s, opts, cb) {230    t.equal(s, server)231    t.notOk(Object.prototype.isPrototypeOf.call(server, s))232    cb()233  }234})235 236test('override can receive options object', (t) => {237  t.plan(4)238 239  const server = { my: 'server' }240  const options = { hello: 'world' }241  const app = boot(server)242 243  app.override = function (s, fn, opts) {244    t.equal(s, server)245    t.same(opts, options)246 247    const res = Object.create(s)248    res.b = 42249 250    return res251  }252 253  app.use(function first (s, opts, cb) {254    t.not(s, server)255    t.ok(Object.prototype.isPrototypeOf.call(server, s))256    cb()257  }, options)258})259 260test('override can receive options function', (t) => {261  t.plan(8)262 263  const server = { my: 'server' }264  const options = { hello: 'world' }265  const app = boot(server)266 267  app.override = function (s, fn, opts) {268    t.equal(s, server)269    if (typeof opts !== 'function') {270      t.same(opts, options)271    }272 273    const res = Object.create(s)274    res.b = 42275    res.bar = 'world'276 277    return res278  }279 280  app.use(function first (s, opts, cb) {281    t.not(s, server)282    t.ok(Object.prototype.isPrototypeOf.call(server, s))283    s.foo = 'bar'284    cb()285  }, options)286 287  app.use(function second (s, opts, cb) {288    t.notOk(s.foo)289    t.same(opts, { hello: 'world' })290    t.ok(Object.prototype.isPrototypeOf.call(server, s))291    cb()292  }, p => ({ hello: p.bar }))293})294 295test('after trigger override', t => {296  t.plan(8)297 298  const server = { count: 0 }299  const app = boot(server)300 301  let overrideCalls = 0302  app.override = function (s, fn, opts) {303    overrideCalls++304    const res = Object.create(s)305    res.count = res.count + 1306    return res307  }308 309  app310    .use(function first (s, opts, cb) {311      t.equal(s.count, 1, 'should trigger override')312      cb()313    })314    .after(function () {315      t.equal(overrideCalls, 1, 'after with 0 parameter should not trigger override')316    })317    .after(function (err) {318      if (err) throw err319      t.equal(overrideCalls, 1, 'after with 1 parameter should not trigger override')320    })321    .after(function (err, done) {322      if (err) throw err323      t.equal(overrideCalls, 1, 'after with 2 parameters should not trigger override')324      done()325    })326    .after(function (err, context, done) {327      if (err) throw err328      t.equal(overrideCalls, 1, 'after with 3 parameters should not trigger override')329      done()330    })331    .after(async function () {332      t.equal(overrideCalls, 1, 'async after with 0 parameter should not trigger override')333    })334    .after(async function (err) {335      if (err) throw err336      t.equal(overrideCalls, 1, 'async after with 1 parameter should not trigger override')337    })338    .after(async function (err, context) {339      if (err) throw err340      t.equal(overrideCalls, 1, 'async after with 2 parameters should not trigger override')341    })342})343 344test('custom inheritance override in after', (t) => {345  t.plan(6)346 347  const server = { count: 0 }348  const app = boot(server)349 350  app.override = function (s) {351    const res = Object.create(s)352    res.count = res.count + 1353 354    return res355  }356 357  app.use(function first (s1, opts, cb) {358    t.not(s1, server)359    t.ok(Object.prototype.isPrototypeOf.call(server, s1))360    t.equal(s1.count, 1)361    s1.after(() => {362      s1.use(second)363    })364 365    cb()366 367    function second (s2, opts, cb) {368      t.not(s2, s1)369      t.ok(Object.prototype.isPrototypeOf.call(s1, s2))370      t.equal(s2.count, 2)371      cb()372    }373  })374})375