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.

168 lines
5.1 KiB

4 years ago
  1. "use strict";
  2. if (typeof window === "undefined") {
  3. window = {};
  4. }
  5. var logMethods = [
  6. "trace",
  7. "debug",
  8. "info",
  9. "warn",
  10. "error"
  11. ];
  12. define(function () {
  13. function getStorageKey(loggerName) {
  14. var key = "loglevel";
  15. if (loggerName) {
  16. key += ":" + loggerName;
  17. }
  18. return key;
  19. }
  20. var self = {};
  21. // Jasmine matcher to check the log level of a log object
  22. self.toBeAtLevel = function toBeAtLevel(level) {
  23. var log = this.actual;
  24. var expectedWorkingCalls = log.levels.SILENT - log.levels[level.toUpperCase()];
  25. var realLogMethod = window.console.log;
  26. var priorCalls = realLogMethod.calls.length;
  27. for (var ii = 0; ii < logMethods.length; ii++) {
  28. var methodName = logMethods[ii];
  29. log[methodName](methodName);
  30. }
  31. expect(realLogMethod.calls.length - priorCalls).toEqual(expectedWorkingCalls);
  32. return true;
  33. };
  34. self.isCookieStorageAvailable = function isCookieStorageAvailable() {
  35. if (window && window.document && window.document.cookie) {
  36. // We need to check not just that the cookie objects are available, but that they work, because
  37. // if we run from file:// URLs they appear present but are non-functional
  38. window.document.cookie = "test=hi;";
  39. var result = window.document.cookie.indexOf('test=hi') !== -1;
  40. window.document.cookie = "test=; expires=Thu, 01 Jan 1970 00:00:01 GMT;";
  41. return result;
  42. } else {
  43. return false;
  44. }
  45. };
  46. self.isLocalStorageAvailable = function isLocalStorageAvailable() {
  47. try {
  48. return !!window.localStorage;
  49. } catch (e){
  50. return false;
  51. }
  52. };
  53. self.isAnyLevelStoragePossible = function isAnyLevelStoragePossible() {
  54. return self.isCookieStorageAvailable() || self.isLocalStorageAvailable();
  55. };
  56. self.toBeTheLevelStoredByCookie = function toBeTheLevelStoredByCookie(name) {
  57. var level = this.actual.toUpperCase();
  58. var storageKey = encodeURIComponent(getStorageKey(name));
  59. if (window.document.cookie.indexOf(storageKey + "=" + level) !== -1) {
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. };
  65. self.toBeTheLevelStoredByLocalStorage = function toBeTheLevelStoredByLocalStorage(name) {
  66. var level = this.actual.toUpperCase();
  67. if (window.localStorage[getStorageKey(name)] === level) {
  68. return true;
  69. }
  70. return false;
  71. };
  72. // Jasmine matcher to check whether a given string was saved by loglevel
  73. self.toBeTheStoredLevel = function toBeTheStoredLevel(name) {
  74. return self.toBeTheLevelStoredByLocalStorage.call(this, name) ||
  75. self.toBeTheLevelStoredByCookie.call(this, name);
  76. };
  77. self.setCookieStoredLevel = function setCookieStoredLevel(level, name) {
  78. window.document.cookie =
  79. encodeURIComponent(getStorageKey(name)) + "=" +
  80. level.toUpperCase() + ";";
  81. };
  82. self.setLocalStorageStoredLevel = function setLocalStorageStoredLevel(level, name) {
  83. window.localStorage[getStorageKey(name)] = level.toUpperCase();
  84. };
  85. self.setStoredLevel = function setStoredLevel(level, name) {
  86. if (self.isCookieStorageAvailable()) {
  87. self.setCookieStoredLevel(level, name);
  88. }
  89. if (self.isLocalStorageAvailable()) {
  90. self.setLocalStorageStoredLevel(level, name);
  91. }
  92. };
  93. self.clearStoredLevels = function clearStoredLevels() {
  94. if (self.isLocalStorageAvailable()) {
  95. window.localStorage.clear();
  96. }
  97. if (self.isCookieStorageAvailable()) {
  98. var storedKeys = window.document.cookie.match(/(?:^|;\s)(loglevel(\:\w+)?)(?=\=)/g);
  99. if (storedKeys) {
  100. for (var i = 0; i < storedKeys.length; i++) {
  101. window.document.cookie = storedKeys[i] + "=; expires=Thu, 01 Jan 1970 00:00:01 GMT;";
  102. }
  103. }
  104. }
  105. };
  106. self.describeIf = function describeIf(condition, name, test) {
  107. if (condition) {
  108. jasmine.getEnv().describe(name, test);
  109. }
  110. };
  111. self.itIf = function itIf(condition, name, test) {
  112. if (condition) {
  113. jasmine.getEnv().it(name, test);
  114. }
  115. };
  116. // Forcibly reloads loglevel, and asynchronously hands the resulting log back to the given callback
  117. // via Jasmine async magic
  118. self.withFreshLog = function withFreshLog(toRun) {
  119. require.undef("lib/loglevel");
  120. var freshLog;
  121. waitsFor(function() {
  122. require(['lib/loglevel'], function(log) {
  123. freshLog = log;
  124. });
  125. return typeof freshLog !== "undefined";
  126. });
  127. runs(function() {
  128. toRun(freshLog);
  129. });
  130. };
  131. // Wraps Jasmine's it(name, test) call to reload the loglevel dependency for the given test
  132. self.itWithFreshLog = function itWithFreshLog(name, test) {
  133. jasmine.getEnv().it(name, function() {
  134. self.withFreshLog(test);
  135. });
  136. };
  137. return self;
  138. });