1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 |
|
6 | const pluginUtilsPublic = require('../../lib/pluginUtils.js')
|
7 | const pluginUtils = require('../../lib/pluginUtils')[Symbol.for('internals')]
|
8 | const symbols = require('../../lib/symbols.js')
|
9 |
|
10 | test("shouldSkipOverride should check the 'skip-override' symbol", t => {
|
11 | t.plan(2)
|
12 |
|
13 | yes[Symbol.for('skip-override')] = true
|
14 |
|
15 | t.true(pluginUtils.shouldSkipOverride(yes))
|
16 | t.false(pluginUtils.shouldSkipOverride(no))
|
17 |
|
18 | function yes () {}
|
19 | function no () {}
|
20 | })
|
21 |
|
22 | test("getMeta should return the object stored with the 'plugin-meta' symbol", t => {
|
23 | t.plan(1)
|
24 |
|
25 | const meta = { hello: 'world' }
|
26 | fn[Symbol.for('plugin-meta')] = meta
|
27 |
|
28 | t.deepEqual(meta, pluginUtils.getMeta(fn))
|
29 |
|
30 | function fn () {}
|
31 | })
|
32 |
|
33 | test('checkDecorators should check if the given decorator is present in the instance', t => {
|
34 | t.plan(1)
|
35 |
|
36 | fn[Symbol.for('plugin-meta')] = {
|
37 | decorators: {
|
38 | fastify: ['plugin'],
|
39 | reply: ['plugin'],
|
40 | request: ['plugin']
|
41 | }
|
42 | }
|
43 |
|
44 | function context () {}
|
45 | context.plugin = true
|
46 | context[symbols.kReply] = { prototype: { plugin: true } }
|
47 | context[symbols.kRequest] = { prototype: { plugin: true } }
|
48 |
|
49 | try {
|
50 | pluginUtils.checkDecorators.call(context, fn)
|
51 | t.pass('Everything ok')
|
52 | } catch (err) {
|
53 | t.fail(err)
|
54 | }
|
55 |
|
56 | function fn () {}
|
57 | })
|
58 |
|
59 | test('checkDecorators should check if the given decorator is present in the instance (errored)', t => {
|
60 | t.plan(1)
|
61 |
|
62 | fn[Symbol.for('plugin-meta')] = {
|
63 | decorators: {
|
64 | fastify: ['plugin'],
|
65 | reply: ['plugin'],
|
66 | request: ['plugin']
|
67 | }
|
68 | }
|
69 |
|
70 | function context () {}
|
71 | context.plugin = true
|
72 | context[symbols.kReply] = { prototype: { plugin: true } }
|
73 | context[symbols.kRequest] = { prototype: {} }
|
74 |
|
75 | try {
|
76 | pluginUtils.checkDecorators.call(context, fn)
|
77 | t.fail('should throw')
|
78 | } catch (err) {
|
79 | t.is(err.message, "The decorator 'plugin' is not present in Request")
|
80 | }
|
81 |
|
82 | function fn () {}
|
83 | })
|
84 |
|
85 | test('checkDependencies should check if the given dependency is present in the instance', t => {
|
86 | t.plan(1)
|
87 |
|
88 | fn[Symbol.for('plugin-meta')] = {
|
89 | dependencies: ['plugin']
|
90 | }
|
91 |
|
92 | function context () {}
|
93 | context[pluginUtilsPublic.registeredPlugins] = ['plugin']
|
94 |
|
95 | try {
|
96 | pluginUtils.checkDependencies.call(context, fn)
|
97 | t.pass('Everything ok')
|
98 | } catch (err) {
|
99 | t.fail(err)
|
100 | }
|
101 |
|
102 | function fn () {}
|
103 | })
|
104 |
|
105 | test('checkDependencies should check if the given dependency is present in the instance (errored)', t => {
|
106 | t.plan(1)
|
107 |
|
108 | fn[Symbol.for('plugin-meta')] = {
|
109 | name: 'test-plugin',
|
110 | dependencies: ['plugin']
|
111 | }
|
112 |
|
113 | function context () {}
|
114 | context[pluginUtilsPublic.registeredPlugins] = []
|
115 |
|
116 | try {
|
117 | pluginUtils.checkDependencies.call(context, fn)
|
118 | t.fail('should throw')
|
119 | } catch (err) {
|
120 | t.is(err.message, "The dependency 'plugin' of plugin 'test-plugin' is not registered")
|
121 | }
|
122 |
|
123 | function fn () {}
|
124 | })
|