Completed
Push — dev ( d30456...4fd03f )
by Fike
33s
created

test/suites/integration/logger/slf4j.Context.spec.js   A

Complexity

Total Complexity 22
Complexity/F 1.38

Size

Lines of Code 98
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
nc 1
dl 0
loc 98
rs 10
c 1
b 0
f 0
wmc 22
mnd 0
bc 16
fnc 16
bpm 1
cpm 1.375
noi 0
1
/* eslint-env mocha */
2
/* eslint-disable no-unused-expressions */
3
/* global allure, Logger */
4
5
var sinon = require('sinon')
6
var Chai = require('chai')
7
var Loggers = require('../../../../lib/logger/index')
8
var slf4j = Loggers.slf4j
9
var Slf4j = slf4j.Slf4j
10
var Context = slf4j.Context
11
var Level = Loggers.Level
12
var expect = Chai.expect
13
14
Chai.use(require('chai-string'))
15
16
describe('Integration', function () {
17
  describe('/logger', function () {
18
    describe('/slf4j.js', function () {
19
      describe('.Slf4j', function () {
20
        describe('.Context', function () {
21
          var writer
22
          var context
23
          var loggerName = 'dummy.dummy'
24
25
          beforeEach(function () {
26
            writer = {
27
              write: sinon.spy(Logger.write.bind(Logger))
28
            }
29
            context = new Context(Level.All, writer)
30
          })
31
32
          it('provides logger creation method', function () {
33
            var logger = context.create('dummy')
34
            expect(logger).to.be.instanceOf(Slf4j)
35
          })
36
37
          it('does not reuse loggers', function () {
38
            allure.description('Context should not reuse loggers because that ' +
39
              'would render diagnostic context useless')
40
            expect(context.create('dummy')).to.not.eq(context.create('dummy'))
41
          })
42
43
          it('creates logger with expected writer', function () {
44
            var writer = {
45
              write: sinon.spy(function () {})
46
            }
47
            var logger = context.create(loggerName, Level.All, writer)
48
            expect(logger.getWriter()).to.eq(writer)
49
          })
50
51
          it('allows in-flight logger threshold adjustments', function () {
52
            var logger = context.create(loggerName)
53
            logger.info('message-a')
54
            context.setLevel(loggerName, Level.Error)
55
            expect(context.getLevel(loggerName)).to.eq(Level.Error)
56
            logger.info('message-b')
57
            context.setLevel(loggerName, Level.Info)
58
            expect(context.getLevel(loggerName)).to.eq(Level.Info)
59
            logger.info('message-c')
60
61
            expect(writer.write.callCount).to.eq(2)
62
            expect(writer.write.getCall(0).args[0]).to.contain('message-a')
63
            expect(writer.write.getCall(1).args[0]).to.contain('message-c')
64
          })
65
66
          it('allows setting root threshold by not specifying logger name', function () {
67
            context.setLevel(Level.All)
68
            expect(context.getLevel()).to.eq(Level.All)
69
            context.setLevel(Level.Error)
70
            expect(context.getLevel()).to.eq(Level.Error)
71
          })
72
73
          // todo testing one object through another is quite bad
74
          it('forces logger to rely on it\'s parent threshold and level unless specified directly', function () {
75
            context.setLevel(Level.All)
76
            var logger = context.create(loggerName)
77
            logger.info('test')
78
            expect(logger.getLevel()).to.eq(Level.All)
79
            context.setLevel(Level.Error)
80
            expect(logger.getLevel()).to.eq(Level.Error)
81
          })
82
83
          it('allows setting root writer by not specifying logger name', function () {
84
            context.setWriter(writer)
85
            expect(context.getWriter()).to.eq(writer)
86
            writer = {}
87
            context.setWriter(writer)
88
            expect(context.getWriter()).to.eq(writer)
89
          })
90
91
          var inputs = [true, undefined, 132, '', '...']
92
          inputs.forEach(function (name) {
93
            it('does not throw on unexpected logger name', function () {
94
              allure.addArgument('name', name)
95
              context.create(name).info('test')
96
            })
97
          })
98
        })
99
      })
100
    })
101
  })
102
})
103