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.

98 lines
3.3 KiB

4 years ago
  1. "use strict";
  2. function consoleLogIsCalledBy(log, methodName) {
  3. it(methodName + " calls console.log", function() {
  4. log.setLevel(log.levels.TRACE);
  5. log[methodName]("Log message for call to " + methodName);
  6. expect(console.log.calls.length).toEqual(1);
  7. });
  8. }
  9. function mockConsole() {
  10. return {"log" : jasmine.createSpy("console.log")};
  11. }
  12. define(['../lib/loglevel'], function(log) {
  13. var originalConsole = window.console;
  14. describe("Fallback functionality:", function() {
  15. describe("with no console present", function() {
  16. beforeEach(function() {
  17. window.console = undefined;
  18. });
  19. afterEach(function() {
  20. window.console = originalConsole;
  21. });
  22. it("silent method calls are allowed", function() {
  23. var result = log.setLevel(log.levels.SILENT);
  24. log.trace("hello");
  25. expect(result).toBeUndefined();
  26. });
  27. it("setting an active level gently returns an error string", function() {
  28. var result = log.setLevel(log.levels.TRACE);
  29. expect(result).toEqual("No console available for logging");
  30. });
  31. it("active method calls are allowed, once the active setLevel fails", function() {
  32. log.setLevel(log.levels.TRACE);
  33. log.trace("hello");
  34. });
  35. describe("if a console later appears", function () {
  36. it("logging is re-enabled and works correctly when next used", function () {
  37. log.setLevel(log.levels.WARN);
  38. window.console = mockConsole();
  39. log.error("error");
  40. expect(window.console.log).toHaveBeenCalled();
  41. });
  42. it("logging is re-enabled but does nothing when used at a blocked level", function () {
  43. log.setLevel(log.levels.WARN);
  44. window.console = mockConsole();
  45. log.trace("trace");
  46. expect(window.console.log).not.toHaveBeenCalled();
  47. });
  48. it("changing level works correctly from that point", function () {
  49. window.console = mockConsole();
  50. var result = log.setLevel(log.levels.WARN);
  51. expect(result).toBeUndefined();
  52. });
  53. });
  54. });
  55. describe("with a console that only supports console.log", function() {
  56. beforeEach(function() {
  57. window.console = mockConsole();
  58. });
  59. afterEach(function() {
  60. window.console = originalConsole;
  61. });
  62. it("log can be set to silent", function() {
  63. log.setLevel(log.levels.SILENT);
  64. });
  65. it("log can be set to an active level", function() {
  66. log.setLevel(log.levels.ERROR);
  67. });
  68. consoleLogIsCalledBy(log, "trace");
  69. consoleLogIsCalledBy(log, "debug");
  70. consoleLogIsCalledBy(log, "info");
  71. consoleLogIsCalledBy(log, "warn");
  72. consoleLogIsCalledBy(log, "trace");
  73. });
  74. });
  75. });