|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const test = require('tap').test |
| 4 | +const build = require('..') |
| 5 | + |
| 6 | +process.removeAllListeners('warning') |
| 7 | + |
| 8 | +test('Create warning with zero parameter', t => { |
| 9 | + t.plan(3) |
| 10 | + |
| 11 | + const { create } = build() |
| 12 | + const buildWarnOpts = create('FastifyWarning', 'CODE', 'Not available') |
| 13 | + const opts = buildWarnOpts() |
| 14 | + t.equal(opts.name, 'FastifyWarning') |
| 15 | + t.equal(opts.message, 'Not available') |
| 16 | + t.equal(opts.code, 'CODE') |
| 17 | +}) |
| 18 | + |
| 19 | +test('Create error with 1 parameter', t => { |
| 20 | + t.plan(3) |
| 21 | + |
| 22 | + const { create } = build() |
| 23 | + const buildWarningOpts = create('FastifyWarning', 'CODE', 'hey %s') |
| 24 | + const opts = buildWarningOpts('alice') |
| 25 | + t.equal(opts.name, 'FastifyWarning') |
| 26 | + t.equal(opts.message, 'hey alice') |
| 27 | + t.equal(opts.code, 'CODE') |
| 28 | +}) |
| 29 | + |
| 30 | +test('Create error with 2 parameters', t => { |
| 31 | + t.plan(3) |
| 32 | + |
| 33 | + const { create } = build() |
| 34 | + const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s') |
| 35 | + const opts = buildWarnOpts('alice', 'attitude') |
| 36 | + t.equal(opts.name, 'FastifyWarning') |
| 37 | + t.equal(opts.message, 'hey alice, I like your attitude') |
| 38 | + t.equal(opts.code, 'CODE') |
| 39 | +}) |
| 40 | + |
| 41 | +test('Create error with 3 parameters', t => { |
| 42 | + t.plan(3) |
| 43 | + |
| 44 | + const { create } = build() |
| 45 | + const buildWarnOpts = create('FastifyWarning', 'CODE', 'hey %s, I like your %s %s') |
| 46 | + const opts = buildWarnOpts('alice', 'attitude', 'see you') |
| 47 | + t.equal(opts.name, 'FastifyWarning') |
| 48 | + t.equal(opts.message, 'hey alice, I like your attitude see you') |
| 49 | + t.equal(opts.code, 'CODE') |
| 50 | +}) |
| 51 | + |
| 52 | +test('Should throw when error code has no fastify name', t => { |
| 53 | + t.plan(1) |
| 54 | + |
| 55 | + const { create } = build() |
| 56 | + |
| 57 | + t.throws(() => create(), new Error('Warning name must not be empty')) |
| 58 | +}) |
| 59 | + |
| 60 | +test('Should throw when error has no code', t => { |
| 61 | + t.plan(1) |
| 62 | + |
| 63 | + const { create } = build() |
| 64 | + |
| 65 | + t.throws(() => create('name'), new Error('Warning code must not be empty')) |
| 66 | +}) |
| 67 | + |
| 68 | +test('Should throw when error has no message', t => { |
| 69 | + t.plan(1) |
| 70 | + |
| 71 | + const { create } = build() |
| 72 | + |
| 73 | + t.throws(() => create('name', 'code'), new Error('Warning message must not be empty')) |
| 74 | +}) |
| 75 | + |
| 76 | +test('Should throw if emit is called with unknown code ', t => { |
| 77 | + t.plan(1) |
| 78 | + |
| 79 | + const { emit } = build() |
| 80 | + |
| 81 | + t.throws(() => emit('CODE'), new Error('The code \'CODE\' does not exist')) |
| 82 | +}) |
| 83 | + |
| 84 | +test('Cannot reuse the same code more than once', t => { |
| 85 | + t.plan(1) |
| 86 | + |
| 87 | + const { create } = build() |
| 88 | + create('FastifyWarning', 'CODE', 'Not available') |
| 89 | + |
| 90 | + t.throws(() => create('FastifyWarning', 'CODE', 'Not available'), new Error("The code 'CODE' already exist")) |
| 91 | +}) |
0 commit comments