forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.js
102 lines (89 loc) · 3.19 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
/**
* 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) {
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;
};
});