CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
decorator.test.js157 linesDownload Raw Back to internals
1'use strict'2 3const { test } = require('node:test')4const decorator = require('../../lib/decorate')5const {6  kState7} = require('../../lib/symbols')8 9test('decorate should add the given method to its instance', t => {10  t.plan(1)11  function build () {12    server.add = decorator.add13    server[kState] = {14      listening: false,15      closing: false,16      started: false17    }18    return server19    function server () {}20  }21 22  const server = build()23  server.add('test', () => {})24  t.assert.ok(server.test)25})26 27test('decorate is chainable', t => {28  t.plan(3)29  function build () {30    server.add = decorator.add31    server[kState] = {32      listening: false,33      closing: false,34      started: false35    }36    return server37    function server () {}38  }39 40  const server = build()41  server42    .add('test1', () => {})43    .add('test2', () => {})44    .add('test3', () => {})45 46  t.assert.ok(server.test1)47  t.assert.ok(server.test2)48  t.assert.ok(server.test3)49})50 51test('checkExistence should check if a property is part of the given instance', t => {52  t.plan(1)53  const instance = { test: () => {} }54  t.assert.ok(decorator.exist(instance, 'test'))55})56 57test('checkExistence should find the instance if not given', t => {58  t.plan(1)59  function build () {60    server.add = decorator.add61    server.check = decorator.exist62    server[kState] = {63      listening: false,64      closing: false,65      started: false66    }67    return server68    function server () {}69  }70 71  const server = build()72  server.add('test', () => {})73  t.assert.ok(server.check('test'))74})75 76test('checkExistence should check the prototype as well', t => {77  t.plan(1)78  function Instance () {}79  Instance.prototype.test = () => {}80 81  const instance = new Instance()82  t.assert.ok(decorator.exist(instance, 'test'))83})84 85test('checkDependencies should throw if a dependency is not present', t => {86  t.plan(2)87  const instance = {}88  try {89    decorator.dependencies(instance, 'foo', ['test'])90    t.assert.fail()91  } catch (e) {92    t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')93    t.assert.strictEqual(e.message, 'The decorator is missing dependency \'test\'.')94  }95})96 97test('decorate should internally call checkDependencies', t => {98  t.plan(2)99  function build () {100    server.add = decorator.add101    server[kState] = {102      listening: false,103      closing: false,104      started: false105    }106    return server107    function server () {}108  }109 110  const server = build()111 112  try {113    server.add('method', () => {}, ['test'])114    t.assert.fail()115  } catch (e) {116    t.assert.strictEqual(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')117    t.assert.strictEqual(e.message, 'The decorator is missing dependency \'test\'.')118  }119})120 121test('decorate should recognize getter/setter objects', t => {122  t.plan(6)123 124  const one = {125    [kState]: {126      listening: false,127      closing: false,128      started: false129    }130  }131  decorator.add.call(one, 'foo', {132    getter: () => this._a,133    setter: (val) => {134      t.assert.ok(true)135      this._a = val136    }137  })138  t.assert.strictEqual(Object.hasOwn(one, 'foo'), true)139  t.assert.strictEqual(one.foo, undefined)140  one.foo = 'a'141  t.assert.strictEqual(one.foo, 'a')142 143  // getter only144  const two = {145    [kState]: {146      listening: false,147      closing: false,148      started: false149    }150  }151  decorator.add.call(two, 'foo', {152    getter: () => 'a getter'153  })154  t.assert.strictEqual(Object.hasOwn(two, 'foo'), true)155  t.assert.strictEqual(two.foo, 'a getter')156})157