CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
hooks.test.js80 linesDownload Raw Back to internals
1'use strict'2 3const { test } = require('node:test')4const { Hooks } = require('../../lib/hooks')5const noop = () => {}6 7test('hooks should have 4 array with the registered hooks', t => {8  const hooks = new Hooks()9  t.assert.strictEqual(typeof hooks, 'object')10  t.assert.ok(Array.isArray(hooks.onRequest))11  t.assert.ok(Array.isArray(hooks.onSend))12  t.assert.ok(Array.isArray(hooks.preParsing))13  t.assert.ok(Array.isArray(hooks.preValidation))14  t.assert.ok(Array.isArray(hooks.preHandler))15  t.assert.ok(Array.isArray(hooks.onResponse))16  t.assert.ok(Array.isArray(hooks.onError))17})18 19test('hooks.add should add a hook to the given hook', t => {20  const hooks = new Hooks()21  hooks.add('onRequest', noop)22  t.assert.strictEqual(hooks.onRequest.length, 1)23  t.assert.strictEqual(typeof hooks.onRequest[0], 'function')24 25  hooks.add('preParsing', noop)26  t.assert.strictEqual(hooks.preParsing.length, 1)27  t.assert.strictEqual(typeof hooks.preParsing[0], 'function')28 29  hooks.add('preValidation', noop)30  t.assert.strictEqual(hooks.preValidation.length, 1)31  t.assert.strictEqual(typeof hooks.preValidation[0], 'function')32 33  hooks.add('preHandler', noop)34  t.assert.strictEqual(hooks.preHandler.length, 1)35  t.assert.strictEqual(typeof hooks.preHandler[0], 'function')36 37  hooks.add('onResponse', noop)38  t.assert.strictEqual(hooks.onResponse.length, 1)39  t.assert.strictEqual(typeof hooks.onResponse[0], 'function')40 41  hooks.add('onSend', noop)42  t.assert.strictEqual(hooks.onSend.length, 1)43  t.assert.strictEqual(typeof hooks.onSend[0], 'function')44 45  hooks.add('onError', noop)46  t.assert.strictEqual(hooks.onError.length, 1)47  t.assert.strictEqual(typeof hooks.onError[0], 'function')48})49 50test('hooks should throw on unexisting handler', t => {51  t.plan(1)52  const hooks = new Hooks()53  try {54    hooks.add('onUnexistingHook', noop)55    t.assert.fail()56  } catch (e) {57    t.assert.ok(true)58  }59})60 61test('should throw on wrong parameters', t => {62  const hooks = new Hooks()63  t.plan(4)64  try {65    hooks.add(null, () => {})66    t.assert.fail()67  } catch (e) {68    t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_TYPE')69    t.assert.strictEqual(e.message, 'The hook name must be a string')70  }71 72  try {73    hooks.add('onSend', null)74    t.assert.fail()75  } catch (e) {76    t.assert.strictEqual(e.code, 'FST_ERR_HOOK_INVALID_HANDLER')77    t.assert.strictEqual(e.message, 'onSend hook should be a function, instead got [object Null]')78  }79})80