forked from js-cookie/js-cookie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
126 lines (117 loc) · 3.12 KB
/
utils.js
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
// https://github.com/axemclion/grunt-saucelabs#test-result-details-with-qunit
(function () {
'use strict';
var log = [];
QUnit.done(function (test_results) {
var tests = [];
for (var i = 0, len = log.length; i < len; i++) {
var details = log[i];
tests.push({
name: details.name,
result: details.result,
expected: details.expected,
actual: details.actual,
source: details.source
});
}
test_results.tests = tests;
// Required for exposing test results to the Sauce Labs API.
// Can be removed when the following issue is fixed:
// https://github.com/axemclion/grunt-saucelabs/issues/84
window.global_test_results = test_results;
});
QUnit.testStart(function (testDetails) {
QUnit.log(function (details) {
if (!details.result) {
details.name = testDetails.name;
log.push(details);
}
});
});
window.lifecycle = {
afterEach: function () {
// Remove the cookies created using js-cookie default attributes
Object.keys(Cookies.get()).forEach(Cookies.remove);
// Remove the cookies created using browser default attributes
Object.keys(Cookies.get()).forEach(function (cookie) {
Cookies.remove(cookie, {
path: ''
});
});
}
};
window.addEvent = function (element, eventName, fn) {
var method = 'addEventListener';
if (element.attachEvent) {
eventName = 'on' + eventName;
method = 'attachEvent';
}
element[ method ](eventName, fn);
};
window.using = function (assert) {
function getQuery(key) {
var queries = location.href.split('?')[1];
if (!queries) {
return;
}
var pairs = queries.split(/&|=/);
var indexBaseURL = pairs.indexOf(key);
var result = pairs[indexBaseURL + 1];
if (result) {
return decodeURIComponent(result);
}
}
function setCookie(name, value) {
return {
then: function (callback) {
var iframe = document.getElementById('request_target');
var serverURL = getQuery('integration_baseurl');
Cookies.set(name, value);
if (!serverURL) {
callback(Cookies.get(name), document.cookie);
} else {
var requestURL = [
serverURL,
'encoding?',
'name=' + encodeURIComponent(name),
'&value=' + encodeURIComponent(value)
].join('');
var done = assert.async();
addEvent(iframe, 'load', function () {
var iframeDocument = iframe.contentWindow.document;
var root = iframeDocument.documentElement;
var content = root.textContent;
if (!content) {
ok(false, [
'"' + requestURL + '"',
'content should not be empty'
].join(' '));
done();
return;
}
try {
var result = JSON.parse(content);
callback(result.value, iframeDocument.cookie);
} finally {
done();
}
});
iframe.src = requestURL;
}
}
};
}
return {
setCookie: setCookie
};
};
window.loadFileSync = function (path) {
var xhr = new XMLHttpRequest();
xhr.open('GET', path, false);
xhr.send(null);
return xhr.status === 200 ? xhr.responseText : null;
};
window.quoted = function (input) {
return '"' + input + '"';
};
}());