Projekt

Obecné

Profil

Stáhnout (3.28 KB) Statistiky
| Větev: | Revize:
1
"use strict";
2

    
3
function consoleLogIsCalledBy(log, methodName) {
4
    it(methodName + " calls console.log", function() {
5
        log.setLevel(log.levels.TRACE);
6
        log[methodName]("Log message for call to " + methodName);
7
        expect(console.log.calls.length).toEqual(1);
8
    });
9
}
10

    
11
function mockConsole() {
12
    return {"log" : jasmine.createSpy("console.log")};
13
}
14

    
15
define(['../lib/loglevel'], function(log) {
16
    var originalConsole = window.console;
17

    
18
    describe("Fallback functionality:", function() {
19
        describe("with no console present", function() {
20
            beforeEach(function() {
21
                window.console = undefined;
22
            });
23

    
24
            afterEach(function() {
25
                window.console = originalConsole;
26
            });
27

    
28
            it("silent method calls are allowed", function() {
29
                var result = log.setLevel(log.levels.SILENT);
30
                log.trace("hello");
31

    
32
                expect(result).toBeUndefined();
33
            });
34

    
35
            it("setting an active level gently returns an error string", function() {
36
                var result = log.setLevel(log.levels.TRACE);
37
                expect(result).toEqual("No console available for logging");
38
            });
39

    
40
            it("active method calls are allowed, once the active setLevel fails", function() {
41
                log.setLevel(log.levels.TRACE);
42
                log.trace("hello");
43
            });
44

    
45
            describe("if a console later appears", function () {
46
                it("logging is re-enabled and works correctly when next used", function () {
47
                    log.setLevel(log.levels.WARN);
48

    
49
                    window.console = mockConsole();
50
                    log.error("error");
51

    
52
                    expect(window.console.log).toHaveBeenCalled();
53
                });
54

    
55
                it("logging is re-enabled but does nothing when used at a blocked level", function () {
56
                    log.setLevel(log.levels.WARN);
57

    
58
                    window.console = mockConsole();
59
                    log.trace("trace");
60

    
61
                    expect(window.console.log).not.toHaveBeenCalled();
62
                });
63

    
64
                it("changing level works correctly from that point", function () {
65
                    window.console = mockConsole();
66
                    var result = log.setLevel(log.levels.WARN);
67

    
68
                    expect(result).toBeUndefined();
69
                });
70
            });
71
        });
72

    
73
        describe("with a console that only supports console.log", function() {
74
            beforeEach(function() {
75
                window.console = mockConsole();
76
            });
77

    
78
            afterEach(function() {
79
                window.console = originalConsole;
80
            });
81

    
82
            it("log can be set to silent", function() {
83
                log.setLevel(log.levels.SILENT);
84
            });
85

    
86
            it("log can be set to an active level", function() {
87
                log.setLevel(log.levels.ERROR);
88
            });
89

    
90
            consoleLogIsCalledBy(log, "trace");
91
            consoleLogIsCalledBy(log, "debug");
92
            consoleLogIsCalledBy(log, "info");
93
            consoleLogIsCalledBy(log, "warn");
94
            consoleLogIsCalledBy(log, "trace");
95
        });
96
    });
97
});
98

    
(2-2/19)