You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
5.7 KiB

4 years ago
  1. "use strict";
  2. define(['test/test-helpers'], function(testHelpers) {
  3. var describeIf = testHelpers.describeIf;
  4. var it = testHelpers.itWithFreshLog;
  5. var originalConsole = window.console;
  6. describe("Multiple logger instances tests:", function() {
  7. describe("log.getLogger()", function() {
  8. it("returns a new logger that is not the default one", function(log) {
  9. var newLogger = log.getLogger("newLogger");
  10. expect(newLogger).not.toEqual(log);
  11. expect(newLogger.trace).toBeDefined();
  12. expect(newLogger.debug).toBeDefined();
  13. expect(newLogger.info).toBeDefined();
  14. expect(newLogger.warn).toBeDefined();
  15. expect(newLogger.error).toBeDefined();
  16. expect(newLogger.setLevel).toBeDefined();
  17. expect(newLogger.setDefaultLevel).toBeDefined();
  18. expect(newLogger.enableAll).toBeDefined();
  19. expect(newLogger.disableAll).toBeDefined();
  20. expect(newLogger.methodFactory).toBeDefined();
  21. });
  22. it("returns loggers without `getLogger()` and `noConflict()`", function(log) {
  23. var newLogger = log.getLogger("newLogger");
  24. expect(newLogger.getLogger).toBeUndefined();
  25. expect(newLogger.noConflict).toBeUndefined();
  26. });
  27. it("returns the same instance when called repeatedly with the same name", function(log) {
  28. var logger1 = log.getLogger("newLogger");
  29. var logger2 = log.getLogger("newLogger");
  30. expect(logger1).toEqual(logger2);
  31. });
  32. it("should throw if called with no name", function(log) {
  33. expect(function() {
  34. log.getLogger();
  35. }).toThrow();
  36. });
  37. it("should throw if called with empty string for name", function(log) {
  38. expect(function() {
  39. log.getLogger("");
  40. }).toThrow();
  41. });
  42. it("should throw if called with a non-string name", function(log) {
  43. expect(function() { log.getLogger(true); }).toThrow();
  44. expect(function() { log.getLogger({}); }).toThrow();
  45. expect(function() { log.getLogger([]); }).toThrow();
  46. expect(function() { log.getLogger(10); }).toThrow();
  47. expect(function() { log.getLogger(function(){}); }).toThrow();
  48. expect(function() { log.getLogger(null); }).toThrow();
  49. expect(function() { log.getLogger(undefined); }).toThrow();
  50. if (window.Symbol) {
  51. expect(function() { log.getLogger(Symbol()); }).toThrow();
  52. }
  53. });
  54. });
  55. describe("inheritance", function() {
  56. beforeEach(function() {
  57. window.console = {"log" : jasmine.createSpy("console.log")};
  58. this.addMatchers({
  59. "toBeAtLevel" : testHelpers.toBeAtLevel
  60. });
  61. testHelpers.clearStoredLevels();
  62. });
  63. afterEach(function() {
  64. window.console = originalConsole;
  65. });
  66. it("loggers are created with the same level as the default logger", function(log) {
  67. log.setLevel("ERROR");
  68. var newLogger = log.getLogger("newLogger");
  69. expect(newLogger).toBeAtLevel("error");
  70. });
  71. it("if a logger's level is persisted, it uses that level rather than the default logger's level", function(log) {
  72. testHelpers.setStoredLevel("error", "newLogger");
  73. log.setLevel("TRACE");
  74. var newLogger = log.getLogger("newLogger");
  75. expect(newLogger).toBeAtLevel("error");
  76. });
  77. it("other loggers do not change when the default logger's level is changed", function(log) {
  78. log.setLevel("TRACE");
  79. var newLogger = log.getLogger("newLogger");
  80. log.setLevel("ERROR");
  81. expect(newLogger).toBeAtLevel("TRACE");
  82. expect(log.getLogger("newLogger")).toBeAtLevel("TRACE");
  83. });
  84. it("loggers are created with the same methodFactory as the default logger", function(log) {
  85. log.methodFactory = function(methodName, level) {
  86. return function() {};
  87. };
  88. var newLogger = log.getLogger("newLogger");
  89. expect(newLogger.methodFactory).toEqual(log.methodFactory);
  90. });
  91. it("loggers have independent method factories", function(log) {
  92. var log1 = log.getLogger('logger1');
  93. var log2 = log.getLogger('logger2');
  94. var log1Spy = jasmine.createSpy('log1spy');
  95. log1.methodFactory = function(methodName, level) {
  96. return log1Spy;
  97. };
  98. log1.setLevel(log1.getLevel());
  99. var log2Spy = jasmine.createSpy('log2spy');
  100. log2.methodFactory = function(methodName, level) {
  101. return log2Spy;
  102. };
  103. log2.setLevel(log2.getLevel());
  104. log1.error('test1');
  105. log2.error('test2');
  106. expect(log1Spy).toHaveBeenCalledWith("test1");
  107. expect(log2Spy).toHaveBeenCalledWith("test2");
  108. });
  109. it("new loggers correctly inherit a logging level of `0`", function(log) {
  110. log.setLevel(0);
  111. var newLogger = log.getLogger("newLogger");
  112. expect(newLogger).toBeAtLevel("trace");
  113. });
  114. });
  115. });
  116. });