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