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.

201 lines
7.0 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. describeIf(testHelpers.isLocalStorageAvailable(), "Local storage persistence tests:", function() {
  7. beforeEach(function() {
  8. window.console = {"log" : jasmine.createSpy("console.log")};
  9. this.addMatchers({
  10. "toBeAtLevel" : testHelpers.toBeAtLevel,
  11. "toBeTheStoredLevel" : testHelpers.toBeTheLevelStoredByLocalStorage,
  12. "toBeTheLevelStoredByLocalStorage": testHelpers.toBeTheLevelStoredByLocalStorage,
  13. "toBeTheLevelStoredByCookie": testHelpers.toBeTheLevelStoredByCookie
  14. });
  15. testHelpers.clearStoredLevels();
  16. });
  17. afterEach(function() {
  18. window.console = originalConsole;
  19. });
  20. describe("If no level is saved", function() {
  21. it("log level is set to warn by default", function(log) {
  22. expect(log).toBeAtLevel("warn");
  23. });
  24. it("warn is not persisted as the current level", function(log) {
  25. expect("warn").not.toBeTheStoredLevel();
  26. });
  27. it("log can be set to info level", function(log) {
  28. log.setLevel("info");
  29. expect(log).toBeAtLevel("info");
  30. });
  31. it("log.setLevel() sets a cookie with the given level", function(log) {
  32. log.setLevel("debug");
  33. expect("debug").toBeTheStoredLevel();
  34. });
  35. it("log.setLevel() does not set a cookie if `persist` argument is false", function(log) {
  36. log.setLevel("debug", false);
  37. expect("debug").not.toBeTheStoredLevel();
  38. });
  39. });
  40. describe("If trace level is saved", function () {
  41. beforeEach(function () {
  42. testHelpers.setStoredLevel("trace");
  43. });
  44. it("trace is the default log level", function (log) {
  45. expect(log).toBeAtLevel("trace");
  46. });
  47. });
  48. describe("If debug level is saved", function () {
  49. beforeEach(function () {
  50. testHelpers.setStoredLevel("debug");
  51. });
  52. it("debug is the default log level", function (log) {
  53. expect(log).toBeAtLevel("debug");
  54. });
  55. });
  56. describe("If info level is saved", function() {
  57. beforeEach(function() {
  58. testHelpers.setStoredLevel("info");
  59. });
  60. it("info is the default log level", function(log) {
  61. expect(log).toBeAtLevel("info");
  62. });
  63. it("log can be changed to warn level", function(log) {
  64. log.setLevel("warn");
  65. expect(log).toBeAtLevel("warn");
  66. });
  67. it("log.setLevel() overwrites the saved level", function(log) {
  68. log.setLevel("error");
  69. expect("error").toBeTheStoredLevel();
  70. expect("info").not.toBeTheStoredLevel();
  71. });
  72. it("log.setLevel() does not overwrite the saved level if `persist` argument is false", function(log) {
  73. log.setLevel("error", false);
  74. expect("info").toBeTheStoredLevel();
  75. expect("error").not.toBeTheStoredLevel();
  76. });
  77. });
  78. describe("If warn level is saved", function () {
  79. beforeEach(function () {
  80. testHelpers.setStoredLevel("warn");
  81. });
  82. it("warn is the default log level", function (log) {
  83. expect(log).toBeAtLevel("warn");
  84. });
  85. });
  86. describe("If error level is saved", function () {
  87. beforeEach(function () {
  88. testHelpers.setStoredLevel("error");
  89. });
  90. it("error is the default log level", function (log) {
  91. expect(log).toBeAtLevel("error");
  92. });
  93. });
  94. describe("If the level is saved with other data", function() {
  95. beforeEach(function() {
  96. window.localStorage['qwe'] = "asd";
  97. window.localStorage['loglevel'] = "ERROR";
  98. window.localStorage['msg'] = "hello world";
  99. });
  100. it("error is the default log level", function(log) {
  101. expect(log).toBeAtLevel("error");
  102. });
  103. it("log can be changed to silent level", function(log) {
  104. log.setLevel("silent");
  105. expect(log).toBeAtLevel("silent");
  106. });
  107. it("log.setLevel() overrides the saved level only", function(log) {
  108. log.setLevel("debug");
  109. expect('debug').toBeTheStoredLevel();
  110. expect(window.localStorage['msg']).toBe("hello world");
  111. });
  112. });
  113. describe("If the level is stored incorrectly", function() {
  114. beforeEach(function() {
  115. testHelpers.setLocalStorageStoredLevel('gibberish');
  116. });
  117. it("warn is the default log level", function(log) {
  118. expect(log).toBeAtLevel("warn");
  119. });
  120. it("warn is not persisted as the current level", function(log) {
  121. expect("warn").not.toBeTheStoredLevel();
  122. });
  123. it("log can be changed to info level", function(log) {
  124. log.setLevel("info");
  125. expect(log).toBeAtLevel("info");
  126. });
  127. it("log.setLevel() overrides the saved level with the new level", function(log) {
  128. expect('debug').not.toBeTheStoredLevel();
  129. log.setLevel("debug");
  130. expect('debug').toBeTheStoredLevel();
  131. });
  132. });
  133. describeIf(testHelpers.isCookieStorageAvailable() && testHelpers.isLocalStorageAvailable(),
  134. "if localStorage and cookies are both available", function () {
  135. it("the level stored in cookies is ignored if a local storage level is set", function () {
  136. testHelpers.setCookieStoredLevel("info");
  137. testHelpers.setLocalStorageStoredLevel("debug");
  138. testHelpers.withFreshLog(function (log) {
  139. expect(log).toBeAtLevel("debug");
  140. });
  141. });
  142. it("the level stored in cookies is used if no local storage level is set", function () {
  143. testHelpers.setCookieStoredLevel("info");
  144. window.localStorage.clear();
  145. testHelpers.withFreshLog(function (log) {
  146. expect(log).toBeAtLevel("info");
  147. });
  148. });
  149. it("the local storage level is set and the cookie level is not", function (log) {
  150. log.setLevel("error");
  151. expect("error").toBeTheLevelStoredByLocalStorage();
  152. expect("error").not.toBeTheLevelStoredByCookie();
  153. });
  154. });
  155. });
  156. });