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