forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.js
123 lines (110 loc) · 3.81 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
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
/**
* 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');
var LoggerRequiredException = require('../exceptions/loggerRequired');
return function() {
var defaultWatchEvents = ['alert', 'confirm', 'prompt'];
function defaultConfirmResponse() {
// Random OK or cancel
return config.randomizer.bool();
}
function defaultPromptResponse() {
// Return a random string
return config.randomizer.sentence();
}
/**
* @mixin
*/
var config = {
watchEvents: defaultWatchEvents,
confirmResponse: defaultConfirmResponse,
promptResponse: defaultPromptResponse,
logger: null,
randomizer: null
};
var alert = window.alert;
var confirm = window.confirm;
var prompt = window.prompt;
/**
* @mixes config
*/
function alertMogwai() {
if (!config.logger) {
throw new LoggerRequiredException();
}
if (config.watchEvents.indexOf('alert') !== -1) {
window.alert = function (msg) {
var event = {
message: msg,
type: 'alert',
action: 'alert',
species: 'mogwai',
timestamp: new Date().getTime()
};
config.logger.warn(event);
};
}
if (config.watchEvents.indexOf('confirm') !== -1) {
window.confirm = function (msg) {
config.confirmResponse();
var event = {
message: msg,
type: 'confirm',
action: 'alert',
species: 'mogwai',
timestamp: new Date().getTime()
};
config.logger.warn(event);
};
}
if (config.watchEvents.indexOf('prompt') !== -1) {
window.prompt = function (msg) {
config.promptResponse();
var event = {
message: msg,
type: 'prompt',
action: 'alert',
species: 'mogwai',
timestamp: new Date().getTime()
};
config.logger.warn(event);
};
}
}
alertMogwai.cleanUp = function() {
window.alert = alert;
window.confirm = confirm;
window.prompt = prompt;
return alertMogwai;
};
configurable(alertMogwai, config);
return alertMogwai;
};
});