strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4 5const pluginUtilsPublic = require('../../lib/pluginUtils.js')6const symbols = require('../../lib/symbols.js')7const pluginUtils = require('../../lib/pluginUtils')[symbols.kTestInternals]8 9test("shouldSkipOverride should check the 'skip-override' symbol", t => {10 t.plan(2)11 12 yes[Symbol.for('skip-override')] = true13 14 t.assert.ok(pluginUtils.shouldSkipOverride(yes))15 t.assert.ok(!pluginUtils.shouldSkipOverride(no))16 17 function yes () {}18 function no () {}19})20 21test('getPluginName should return plugin name if the file is cached', t => {22 t.plan(1)23 const expectedPluginName = 'example'24 const fn = () => console.log('is just an example')25 require.cache[expectedPluginName] = { exports: fn }26 const pluginName = pluginUtilsPublic.getPluginName(fn)27 28 t.assert.strictEqual(pluginName, expectedPluginName)29})30 31test('getPluginName should not throw when require.cache is undefined', t => {32 t.plan(1)33 function example () {34 console.log('is just an example')35 }36 const cache = require.cache37 require.cache = undefined38 t.after(() => {39 require.cache = cache40 })41 const pluginName = pluginUtilsPublic.getPluginName(example)42 43 t.assert.strictEqual(pluginName, 'example')44})45 46test("getMeta should return the object stored with the 'plugin-meta' symbol", t => {47 t.plan(1)48 49 const meta = { hello: 'world' }50 fn[Symbol.for('plugin-meta')] = meta51 52 t.assert.deepStrictEqual(meta, pluginUtils.getMeta(fn))53 54 function fn () {}55})56 57test('checkDecorators should check if the given decorator is present in the instance', t => {58 t.plan(1)59 60 fn[Symbol.for('plugin-meta')] = {61 decorators: {62 fastify: ['plugin'],63 reply: ['plugin'],64 request: ['plugin']65 }66 }67 68 function context () {}69 context.plugin = true70 context[symbols.kReply] = { prototype: { plugin: true }, props: [] }71 context[symbols.kRequest] = { prototype: { plugin: true }, props: [] }72 73 try {74 pluginUtils.checkDecorators.call(context, fn)75 t.assert.ok('Everything ok')76 } catch (err) {77 t.assert.fail(err)78 }79 80 function fn () {}81})82 83test('checkDecorators should check if the given decorator is present in the instance (errored)', t => {84 t.plan(1)85 86 fn[Symbol.for('plugin-meta')] = {87 decorators: {88 fastify: ['plugin'],89 reply: ['plugin'],90 request: ['plugin']91 }92 }93 94 function context () {}95 context.plugin = true96 context[symbols.kReply] = { prototype: { plugin: true }, props: [] }97 context[symbols.kRequest] = { prototype: {}, props: [] }98 99 try {100 pluginUtils.checkDecorators.call(context, fn)101 t.assert.fail('should throw')102 } catch (err) {103 t.assert.strictEqual(err.message, "The decorator 'plugin' is not present in Request")104 }105 106 function fn () {}107})108 109test('checkDecorators should accept optional decorators', t => {110 t.plan(1)111 112 fn[Symbol.for('plugin-meta')] = {113 decorators: { }114 }115 116 function context () {}117 context.plugin = true118 context[symbols.kReply] = { prototype: { plugin: true } }119 context[symbols.kRequest] = { prototype: { plugin: true } }120 121 try {122 pluginUtils.checkDecorators.call(context, fn)123 t.assert.ok('Everything ok')124 } catch (err) {125 t.assert.fail(err)126 }127 128 function fn () {}129})130 131test('checkDependencies should check if the given dependency is present in the instance', t => {132 t.plan(1)133 134 fn[Symbol.for('plugin-meta')] = {135 dependencies: ['plugin']136 }137 138 function context () {}139 context[pluginUtilsPublic.kRegisteredPlugins] = ['plugin']140 141 try {142 pluginUtils.checkDependencies.call(context, fn)143 t.assert.ok('Everything ok')144 } catch (err) {145 t.assert.fail(err)146 }147 148 function fn () {}149})150 151test('checkDependencies should check if the given dependency is present in the instance (errored)', t => {152 t.plan(1)153 154 fn[Symbol.for('plugin-meta')] = {155 name: 'test-plugin',156 dependencies: ['plugin']157 }158 159 function context () {}160 context[pluginUtilsPublic.kRegisteredPlugins] = []161 162 try {163 pluginUtils.checkDependencies.call(context, fn)164 t.assert.fail('should throw')165 } catch (err) {166 t.assert.strictEqual(err.message, "The dependency 'plugin' of plugin 'test-plugin' is not registered")167 }168 169 function fn () {}170})171 