UNPKG

3.36 kBJavaScriptView Raw
1const expect = require('chai').expect;
2const loglevel = require('loglevel');
3const other = require('loglevel-plugin-mock');
4const sinon = require('sinon');
5const prefix = require('../lib/loglevel-plugin-prefix');
6
7const spy = sinon.spy();
8
9describe('', () => {
10 beforeEach(() => {
11 /* eslint-disable no-empty */
12 try {
13 prefix.disable();
14 } catch (ignore) {}
15 try {
16 other.disable();
17 } catch (ignore) {}
18 try {
19 prefix.disable();
20 } catch (ignore) {}
21 /* eslint-enable no-empty */
22 spy.reset();
23 });
24
25 describe('API', () => {
26 it('Methods', () => {
27 expect(prefix).to.have.property('apply').with.be.a('function');
28 expect(prefix).to.have.property('disable').with.be.a('function');
29 });
30
31 it('Empty arguments', () => {
32 expect(prefix.apply).to.throw(TypeError, 'Argument is not a root loglevel object');
33 });
34
35 it('Not root loglevel argument', () => {
36 expect(() => prefix.apply(loglevel.getLogger('log'))).to.throw(
37 TypeError,
38 'Argument is not a root loglevel object'
39 );
40 });
41
42 it('Disabling a not appled plugin should throw an exception', () => {
43 expect(prefix.disable).to.throw(Error, "You can't disable a not appled plugin");
44 });
45
46 it('Right applying', () => {
47 expect(() => prefix.apply(loglevel)).to.not.throw();
48 });
49
50 it('Right disabling', () => {
51 prefix.apply(loglevel);
52
53 expect(prefix.disable).to.not.throw();
54 });
55
56 it('Reapplying without applying other plugin', () => {
57 prefix.apply(loglevel);
58
59 expect(() => prefix.apply(loglevel)).to.not.throw();
60 });
61
62 it('Reapplying after using another plugin should throw an exception', () => {
63 prefix.apply(loglevel);
64 other.apply(loglevel);
65
66 expect(() => prefix.apply(loglevel)).to.throw(
67 Error,
68 "You can't reassign a plugin after appling another plugin"
69 );
70 });
71
72 it('Disabling after using another plugin should throw an exception', () => {
73 prefix.apply(loglevel);
74 other.apply(loglevel);
75
76 expect(prefix.disable).to.throw(
77 Error,
78 "You can't disable a plugin after appling another plugin"
79 );
80 });
81
82 it('Reapplying after disabling another plugin should not thrown an exception', () => {
83 prefix.apply(loglevel);
84 other.apply(loglevel);
85 other.disable();
86
87 expect(() => prefix.apply(loglevel)).to.not.throw();
88 });
89
90 it('Disabling after disabling another plugin should not thrown an exception', () => {
91 prefix.apply(loglevel);
92 other.apply(loglevel);
93 other.disable();
94
95 expect(prefix.disable).to.not.throw();
96 });
97 });
98
99 describe('Prefix', () => {
100 it('All methods of the previous plugin should be called', () => {
101 other.apply(loglevel, { method: spy });
102 prefix.apply(loglevel);
103
104 loglevel.enableAll();
105 loglevel.trace();
106 loglevel.debug();
107 loglevel.info();
108 loglevel.warn();
109 loglevel.error();
110 expect(spy.callCount).to.equal(5);
111 });
112
113 it('The prefix must be combined with the first argument, if it is a string', () => {
114 other.apply(loglevel, { method: spy });
115 prefix.apply(loglevel, { template: '%l:' });
116
117 loglevel.warn('foo %s', 'bar');
118 expect(spy.calledWith('WARN: foo %s', 'bar')).to.be.true;
119 });
120 });
121});