level-setting-test.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. "use strict";
  2. var logMethods = [
  3. "trace",
  4. "debug",
  5. "info",
  6. "warn",
  7. "error"
  8. ];
  9. function getConsoleMethod(logMethodName) {
  10. if (logMethodName === 'debug') {
  11. return console.log;
  12. } else {
  13. return console[logMethodName];
  14. }
  15. }
  16. define(['../lib/loglevel'], function(log) {
  17. var originalConsole = window.console;
  18. describe("Basic log levels changing tests:", function() {
  19. beforeEach(function() {
  20. window.console = {};
  21. for (var ii = 0; ii < logMethods.length; ii++) {
  22. window.console[logMethods[ii]] = jasmine.createSpy(logMethods[ii]);
  23. }
  24. window.console.log = jasmine.createSpy('log');
  25. });
  26. afterEach(function() {
  27. window.console = originalConsole;
  28. });
  29. describe("log.enableAll()", function() {
  30. it("enables all log methods", function() {
  31. log.enableAll(false);
  32. for (var ii = 0; ii < logMethods.length; ii++) {
  33. var method = logMethods[ii];
  34. log[method]("a log message");
  35. expect(getConsoleMethod(method)).toHaveBeenCalled();
  36. }
  37. });
  38. });
  39. describe("log.disableAll()", function() {
  40. it("disables all log methods", function() {
  41. log.disableAll(false);
  42. for (var ii = 0; ii < logMethods.length; ii++) {
  43. var method = logMethods[ii];
  44. log[method]("a log message");
  45. expect(getConsoleMethod(method)).not.toHaveBeenCalled();
  46. }
  47. });
  48. });
  49. describe("log.setLevel() throws errors if given", function() {
  50. it("no level argument", function() {
  51. expect(function() {
  52. log.setLevel();
  53. }).toThrow("log.setLevel() called with invalid level: undefined");
  54. });
  55. it("a null level argument", function() {
  56. expect(function() {
  57. log.setLevel(null);
  58. }).toThrow("log.setLevel() called with invalid level: null");
  59. });
  60. it("an undefined level argument", function() {
  61. expect(function() {
  62. log.setLevel(undefined);
  63. }).toThrow("log.setLevel() called with invalid level: undefined");
  64. });
  65. it("an invalid log level index", function() {
  66. expect(function() {
  67. log.setLevel(-1);
  68. }).toThrow("log.setLevel() called with invalid level: -1");
  69. });
  70. it("an invalid log level name", function() {
  71. expect(function() {
  72. log.setLevel("InvalidLevelName");
  73. }).toThrow("log.setLevel() called with invalid level: InvalidLevelName");
  74. });
  75. });
  76. describe("setting log level by name", function() {
  77. function itCanSetLogLevelTo(level) {
  78. it("can set log level to " + level, function() {
  79. log.setLevel(level, false);
  80. log[level]("log message");
  81. expect(getConsoleMethod(level)).toHaveBeenCalled();
  82. });
  83. }
  84. itCanSetLogLevelTo("trace");
  85. itCanSetLogLevelTo("debug");
  86. itCanSetLogLevelTo("info");
  87. itCanSetLogLevelTo("warn");
  88. itCanSetLogLevelTo("error");
  89. });
  90. describe("log level settings", function() {
  91. describe("log.trace", function() {
  92. it("is enabled at trace level", function() {
  93. log.setLevel(log.levels.TRACE);
  94. log.trace("a log message");
  95. expect(console.trace).toHaveBeenCalled();
  96. });
  97. it("is disabled at debug level", function() {
  98. log.setLevel(log.levels.DEBUG);
  99. log.trace("a log message");
  100. expect(console.trace).not.toHaveBeenCalled();
  101. });
  102. it("is disabled at silent level", function() {
  103. log.setLevel(log.levels.SILENT);
  104. log.trace("a log message");
  105. expect(console.trace).not.toHaveBeenCalled();
  106. });
  107. });
  108. describe("log.debug", function() {
  109. it("is enabled at trace level", function() {
  110. log.setLevel(log.levels.TRACE);
  111. log.debug("a log message");
  112. expect(console.log).toHaveBeenCalled();
  113. });
  114. it("is enabled at debug level", function() {
  115. log.setLevel(log.levels.DEBUG);
  116. log.debug("a log message");
  117. expect(console.log).toHaveBeenCalled();
  118. });
  119. it("is disabled at info level", function() {
  120. log.setLevel(log.levels.INFO);
  121. log.debug("a log message");
  122. expect(console.log).not.toHaveBeenCalled();
  123. });
  124. it("is disabled at silent level", function() {
  125. log.setLevel(log.levels.SILENT);
  126. log.debug("a log message");
  127. expect(console.log).not.toHaveBeenCalled();
  128. });
  129. });
  130. describe("log.log", function() {
  131. it("is enabled at trace level", function() {
  132. log.setLevel(log.levels.TRACE);
  133. log.log("a log message");
  134. expect(console.log).toHaveBeenCalled();
  135. });
  136. it("is enabled at debug level", function() {
  137. log.setLevel(log.levels.DEBUG);
  138. log.log("a log message");
  139. expect(console.log).toHaveBeenCalled();
  140. });
  141. it("is disabled at info level", function() {
  142. log.setLevel(log.levels.INFO);
  143. log.log("a log message");
  144. expect(console.log).not.toHaveBeenCalled();
  145. });
  146. it("is disabled at silent level", function() {
  147. log.setLevel(log.levels.SILENT);
  148. log.log("a log message");
  149. expect(console.log).not.toHaveBeenCalled();
  150. });
  151. });
  152. describe("log.info", function() {
  153. it("is enabled at debug level", function() {
  154. log.setLevel(log.levels.DEBUG);
  155. log.info("a log message");
  156. expect(console.info).toHaveBeenCalled();
  157. });
  158. it("is enabled at info level", function() {
  159. log.setLevel(log.levels.INFO);
  160. log.info("a log message");
  161. expect(console.info).toHaveBeenCalled();
  162. });
  163. it("is disabled at warn level", function() {
  164. log.setLevel(log.levels.WARN);
  165. log.info("a log message");
  166. expect(console.info).not.toHaveBeenCalled();
  167. });
  168. it("is disabled at silent level", function() {
  169. log.setLevel(log.levels.SILENT);
  170. log.info("a log message");
  171. expect(console.info).not.toHaveBeenCalled();
  172. });
  173. });
  174. describe("log.warn", function() {
  175. it("is enabled at info level", function() {
  176. log.setLevel(log.levels.INFO);
  177. log.warn("a log message");
  178. expect(console.warn).toHaveBeenCalled();
  179. });
  180. it("is enabled at warn level", function() {
  181. log.setLevel(log.levels.WARN);
  182. log.warn("a log message");
  183. expect(console.warn).toHaveBeenCalled();
  184. });
  185. it("is disabled at error level", function() {
  186. log.setLevel(log.levels.ERROR);
  187. log.warn("a log message");
  188. expect(console.warn).not.toHaveBeenCalled();
  189. });
  190. it("is disabled at silent level", function() {
  191. log.setLevel(log.levels.SILENT);
  192. log.warn("a log message");
  193. expect(console.warn).not.toHaveBeenCalled();
  194. });
  195. });
  196. describe("log.error", function() {
  197. it("is enabled at warn level", function() {
  198. log.setLevel(log.levels.WARN);
  199. log.error("a log message");
  200. expect(console.error).toHaveBeenCalled();
  201. });
  202. it("is enabled at error level", function() {
  203. log.setLevel(log.levels.ERROR);
  204. log.error("a log message");
  205. expect(console.error).toHaveBeenCalled();
  206. });
  207. it("is disabled at silent level", function() {
  208. log.setLevel(log.levels.SILENT);
  209. log.error("a log message");
  210. expect(console.error).not.toHaveBeenCalled();
  211. });
  212. });
  213. });
  214. });
  215. });