UNPKG

1.68 kBJavaScriptView Raw
1const hapi = require('hapi')
2const akaya = require('akaya')
3const qs = require('qs')
4const url = require('url')
5const plugin = require('../src')
6const mockResponse = require('./fixtures/index')
7
8/**
9 * @type {Object}
10 * @private
11 *
12 * @description
13 * Mock the plugin options
14 */
15const mockPluginOptions = {
16 absolute: false,
17 paramNames: {
18 perPage: 'per_page',
19 page: 'page',
20 total: 'total'
21 }
22}
23
24/**
25 * @function
26 * @public
27 *
28 * @description
29 * Setup and expose an Hapi server connection
30 *
31 * @param {Object} options The route specific options
32 * @param {Object} pluginOptions The plugin specific options
33 * @returns {Object} The needed fixtures
34 */
35const setup = async (options, pluginOptions) => {
36 pluginOptions = pluginOptions || mockPluginOptions
37
38 const key = (options && options.key) || 'result'
39 const fixtures = {
40 server: hapi.server({
41 port: 1337,
42 host: 'localhost'
43 }),
44 host: 'http://localhost:1337/'
45 }
46
47 fixtures.server.route([
48 {
49 method: 'GET',
50 path: '/',
51 config: {
52 id: 'foo',
53 handler (req, h) {
54 return h.bissle({ [key]: Array.from(mockResponse) }, options)
55 }
56 }
57 },
58 {
59 method: 'GET',
60 path: '/page',
61 config: {
62 handler (req, h) {
63 return h.bissle({ [key]: Array.from(mockResponse) }, options)
64 }
65 }
66 }
67 ])
68
69 await fixtures.server.register([akaya, {
70 plugin,
71 options: pluginOptions
72 }])
73
74 return fixtures
75}
76
77const getQueries = path => qs.parse(url.parse(path).query).query
78const getParams = path => qs.parse(url.parse(path).query).params
79
80module.exports = {
81 setup,
82 getQueries,
83 getParams
84}