forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.js
98 lines (86 loc) · 3.09 KB
/
alert.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
/**
* The alert mogwai answers calls to alert()
*
* The alert mogwai overrides window.alert, window.confirm, and window.prompt
* to avoid stopping the stress test with blocking JavaScript calls. Instead
* of displaying a dialog, these methods are simply replaced by a write in the
* logger.
*
* var alertMogwai = gremlins.mogwais.alert();
* horde.mogwai(alertMogwai);
*
* The alert mogwai can be customized as follows:
*
* alertMogwai.watchEvents(['alert', 'confirm', 'prompt']); // select the events to catch
* alertMogwai.confirmResponse(function() { // what a call to confirm() should return });
* alertMogwai.promptResponse(function() { // what a call to prompt() should return });
* alertMogwai.logger(loggerObject); // inject a logger
* alertMogwai.randomizer(randomizerObject); // inject a randomizer
*
* Example usage:
*
* horde.mogwai(gremlins.mogwais.alert()
* .watchEvents(['prompt'])
* .promptResponse(function() { return 'I typed garbage'; })
* );
*/
define(function(require) {
"use strict";
var configurable = require('../utils/configurable');
var Chance = require('../vendor/chance');
return function() {
var defaultWatchEvents = ['alert', 'confirm', 'prompt'];
var defaultConfirmResponse = function() {
// Random OK or cancel
return config.randomizer.bool();
};
var defaultPromptResponse = function() {
// Return a random string
return config.randomizer.sentence();
};
var defaultLogger = { warn: function() {} };
/**
* @mixin
*/
var config = {
watchEvents: defaultWatchEvents,
confirmResponse: defaultConfirmResponse,
promptResponse: defaultPromptResponse,
logger: defaultLogger,
randomizer: new Chance()
};
var alert = window.alert;
var confirm = window.confirm;
var prompt = window.prompt;
/**
* @mixes config
*/
function alertMogwai() {
if (config.watchEvents.indexOf('alert') !== -1) {
window.alert = function (msg) {
config.logger.warn('mogwai ', 'alert ', msg, 'alert');
};
}
if (config.watchEvents.indexOf('confirm') !== -1) {
window.confirm = function (msg) {
config.confirmResponse();
config.logger.warn('mogwai ', 'alert ', msg, 'confirm');
};
}
if (config.watchEvents.indexOf('prompt') !== -1) {
window.prompt = function (msg) {
config.promptResponse();
config.logger.warn('mogwai ', 'alert ', msg, 'prompt');
};
}
}
alertMogwai.cleanUp = function() {
window.alert = alert;
window.confirm = confirm;
window.prompt = prompt;
return alertMogwai;
};
configurable(alertMogwai, config);
return alertMogwai;
};
});