123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- "use strict";
- var logMethods = [
- "trace",
- "debug",
- "info",
- "warn",
- "error"
- ];
- function getConsoleMethod(logMethodName) {
- if (logMethodName === 'debug') {
- return console.log;
- } else {
- return console[logMethodName];
- }
- }
- define(['../lib/loglevel'], function(log) {
- var originalConsole = window.console;
- describe("Basic log levels changing tests:", function() {
- beforeEach(function() {
- window.console = {};
- for (var ii = 0; ii < logMethods.length; ii++) {
- window.console[logMethods[ii]] = jasmine.createSpy(logMethods[ii]);
- }
- window.console.log = jasmine.createSpy('log');
- });
- afterEach(function() {
- window.console = originalConsole;
- });
- describe("log.enableAll()", function() {
- it("enables all log methods", function() {
- log.enableAll(false);
- for (var ii = 0; ii < logMethods.length; ii++) {
- var method = logMethods[ii];
- log[method]("a log message");
- expect(getConsoleMethod(method)).toHaveBeenCalled();
- }
- });
- });
- describe("log.disableAll()", function() {
- it("disables all log methods", function() {
- log.disableAll(false);
- for (var ii = 0; ii < logMethods.length; ii++) {
- var method = logMethods[ii];
- log[method]("a log message");
- expect(getConsoleMethod(method)).not.toHaveBeenCalled();
- }
- });
- });
- describe("log.setLevel() throws errors if given", function() {
- it("no level argument", function() {
- expect(function() {
- log.setLevel();
- }).toThrow("log.setLevel() called with invalid level: undefined");
- });
- it("a null level argument", function() {
- expect(function() {
- log.setLevel(null);
- }).toThrow("log.setLevel() called with invalid level: null");
- });
- it("an undefined level argument", function() {
- expect(function() {
- log.setLevel(undefined);
- }).toThrow("log.setLevel() called with invalid level: undefined");
- });
- it("an invalid log level index", function() {
- expect(function() {
- log.setLevel(-1);
- }).toThrow("log.setLevel() called with invalid level: -1");
- });
- it("an invalid log level name", function() {
- expect(function() {
- log.setLevel("InvalidLevelName");
- }).toThrow("log.setLevel() called with invalid level: InvalidLevelName");
- });
- });
- describe("setting log level by name", function() {
- function itCanSetLogLevelTo(level) {
- it("can set log level to " + level, function() {
- log.setLevel(level, false);
- log[level]("log message");
- expect(getConsoleMethod(level)).toHaveBeenCalled();
- });
- }
- itCanSetLogLevelTo("trace");
- itCanSetLogLevelTo("debug");
- itCanSetLogLevelTo("info");
- itCanSetLogLevelTo("warn");
- itCanSetLogLevelTo("error");
- });
- describe("log level settings", function() {
- describe("log.trace", function() {
- it("is enabled at trace level", function() {
- log.setLevel(log.levels.TRACE);
- log.trace("a log message");
- expect(console.trace).toHaveBeenCalled();
- });
- it("is disabled at debug level", function() {
- log.setLevel(log.levels.DEBUG);
- log.trace("a log message");
- expect(console.trace).not.toHaveBeenCalled();
- });
- it("is disabled at silent level", function() {
- log.setLevel(log.levels.SILENT);
- log.trace("a log message");
- expect(console.trace).not.toHaveBeenCalled();
- });
- });
- describe("log.debug", function() {
- it("is enabled at trace level", function() {
- log.setLevel(log.levels.TRACE);
- log.debug("a log message");
- expect(console.log).toHaveBeenCalled();
- });
- it("is enabled at debug level", function() {
- log.setLevel(log.levels.DEBUG);
- log.debug("a log message");
- expect(console.log).toHaveBeenCalled();
- });
- it("is disabled at info level", function() {
- log.setLevel(log.levels.INFO);
- log.debug("a log message");
- expect(console.log).not.toHaveBeenCalled();
- });
- it("is disabled at silent level", function() {
- log.setLevel(log.levels.SILENT);
- log.debug("a log message");
- expect(console.log).not.toHaveBeenCalled();
- });
- });
- describe("log.log", function() {
- it("is enabled at trace level", function() {
- log.setLevel(log.levels.TRACE);
- log.log("a log message");
- expect(console.log).toHaveBeenCalled();
- });
- it("is enabled at debug level", function() {
- log.setLevel(log.levels.DEBUG);
- log.log("a log message");
- expect(console.log).toHaveBeenCalled();
- });
- it("is disabled at info level", function() {
- log.setLevel(log.levels.INFO);
- log.log("a log message");
- expect(console.log).not.toHaveBeenCalled();
- });
- it("is disabled at silent level", function() {
- log.setLevel(log.levels.SILENT);
- log.log("a log message");
- expect(console.log).not.toHaveBeenCalled();
- });
- });
- describe("log.info", function() {
- it("is enabled at debug level", function() {
- log.setLevel(log.levels.DEBUG);
- log.info("a log message");
- expect(console.info).toHaveBeenCalled();
- });
- it("is enabled at info level", function() {
- log.setLevel(log.levels.INFO);
- log.info("a log message");
- expect(console.info).toHaveBeenCalled();
- });
- it("is disabled at warn level", function() {
- log.setLevel(log.levels.WARN);
- log.info("a log message");
- expect(console.info).not.toHaveBeenCalled();
- });
- it("is disabled at silent level", function() {
- log.setLevel(log.levels.SILENT);
- log.info("a log message");
- expect(console.info).not.toHaveBeenCalled();
- });
- });
- describe("log.warn", function() {
- it("is enabled at info level", function() {
- log.setLevel(log.levels.INFO);
- log.warn("a log message");
- expect(console.warn).toHaveBeenCalled();
- });
- it("is enabled at warn level", function() {
- log.setLevel(log.levels.WARN);
- log.warn("a log message");
- expect(console.warn).toHaveBeenCalled();
- });
- it("is disabled at error level", function() {
- log.setLevel(log.levels.ERROR);
- log.warn("a log message");
- expect(console.warn).not.toHaveBeenCalled();
- });
- it("is disabled at silent level", function() {
- log.setLevel(log.levels.SILENT);
- log.warn("a log message");
- expect(console.warn).not.toHaveBeenCalled();
- });
- });
- describe("log.error", function() {
- it("is enabled at warn level", function() {
- log.setLevel(log.levels.WARN);
- log.error("a log message");
- expect(console.error).toHaveBeenCalled();
- });
- it("is enabled at error level", function() {
- log.setLevel(log.levels.ERROR);
- log.error("a log message");
- expect(console.error).toHaveBeenCalled();
- });
- it("is disabled at silent level", function() {
- log.setLevel(log.levels.SILENT);
- log.error("a log message");
- expect(console.error).not.toHaveBeenCalled();
- });
- });
- });
- });
- });
|