UNPKG

3.68 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const proxyquire = require('proxyquire')
5const test = t.test
6const fp = require('./')
7
8test('fastify-plugin is a function', t => {
9 t.plan(1)
10 t.type(fp, 'function')
11})
12
13test('should return the function with the skip-override Symbol', t => {
14 t.plan(1)
15
16 function plugin (fastify, opts, next) {
17 next()
18 }
19
20 fp(plugin)
21 t.ok(plugin[Symbol.for('skip-override')])
22})
23
24test('should throw if the plugin is not a function', t => {
25 t.plan(1)
26
27 try {
28 fp('plugin')
29 t.fail()
30 } catch (e) {
31 t.is(e.message, 'fastify-plugin expects a function, instead got a \'string\'')
32 }
33})
34
35test('should check the fastify version', t => {
36 t.plan(1)
37
38 function plugin (fastify, opts, next) {
39 next()
40 }
41
42 try {
43 fp(plugin, { fastify: '>=0.10.0' })
44 t.pass()
45 } catch (e) {
46 t.fail()
47 }
48})
49
50test('should check the fastify version', t => {
51 t.plan(1)
52
53 function plugin (fastify, opts, next) {
54 next()
55 }
56
57 try {
58 fp(plugin, '>=0.10.0')
59 t.pass()
60 } catch (e) {
61 t.fail()
62 }
63})
64
65test('the options object should be an object', t => {
66 t.plan(2)
67
68 try {
69 fp(() => {}, null)
70 t.fail()
71 } catch (e) {
72 t.is(e.message, 'The options object should be an object')
73 }
74
75 try {
76 fp(() => {}, [])
77 t.fail()
78 } catch (e) {
79 t.is(e.message, 'The options object should be an object')
80 }
81})
82
83test('should throw if the fastify version does not satisfies the plugin requested version', t => {
84 t.plan(1)
85
86 function plugin (fastify, opts, next) {
87 next()
88 }
89
90 const v = require('fastify/package.json').version.replace(/-rc\.\d+/, '')
91 try {
92 fp(plugin, { fastify: '1000.1000.1000' })
93 t.fail()
94 } catch (e) {
95 t.is(e.message, `fastify-plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`)
96 }
97})
98
99test('should throw if the version number is not a string', t => {
100 t.plan(1)
101
102 try {
103 fp(() => {}, { fastify: 12 })
104 t.fail()
105 } catch (e) {
106 t.is(e.message, 'fastify-plugin expects a version string, instead got \'number\'')
107 }
108})
109
110test('should not throw if fastify is not found', t => {
111 t.plan(1)
112
113 const fp = proxyquire('./index.js', {
114 'fastify/package.json': null,
115 console: {
116 info: function (msg) {
117 t.is(msg, 'fastify not found, proceeding anyway')
118 }
119 }
120 })
121
122 function plugin (fastify, opts, next) {
123 next()
124 }
125
126 fp(plugin, { fastify: '>= 0' })
127})
128
129test('Should accept an option object', t => {
130 t.plan(2)
131
132 const opts = { hello: 'world' }
133
134 function plugin (fastify, opts, next) {
135 next()
136 }
137
138 fp(plugin, opts)
139 t.ok(plugin[Symbol.for('skip-override')])
140 t.deepEqual(plugin[Symbol.for('plugin-meta')], opts)
141})
142
143test('Should accept an option object and checks the version', t => {
144 t.plan(2)
145
146 const opts = { hello: 'world', fastify: '>=0.10.0' }
147
148 function plugin (fastify, opts, next) {
149 next()
150 }
151
152 fp(plugin, opts)
153 t.ok(plugin[Symbol.for('skip-override')])
154 delete opts.version
155 t.deepEqual(plugin[Symbol.for('plugin-meta')], opts)
156})
157
158test('should throw if the fastify version does not satisfies the plugin requested version', t => {
159 t.plan(1)
160
161 function plugin (fastify, opts, next) {
162 next()
163 }
164
165 const v = require('fastify/package.json').version.replace(/-rc\.\d+/, '')
166 try {
167 fp(plugin, { fastify: '1000.1000.1000' })
168 t.fail()
169 } catch (e) {
170 t.is(e.message, `fastify-plugin - expected '1000.1000.1000' fastify version, '${v}' is installed`)
171 }
172})
173
174test('should set anonymous function name to file it was called from', t => {
175 t.plan(1)
176
177 const fn = fp((fastify, opts, next) => {
178 next()
179 })
180
181 t.is(fn[Symbol.for('fastify.display-name')], 'test')
182})