forked from marmelab/gremlins.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.js
executable file
·57 lines (49 loc) · 1.56 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
const getDefaultConfig = (randomizer) => {
const defaultWatchEvents = ['alert', 'confirm', 'prompt'];
const defaultConfirmResponse = () => {
return randomizer.bool();
};
const defaultPromptResponse = () => {
return randomizer.sentence();
};
return {
watchEvents: defaultWatchEvents,
confirmResponse: defaultConfirmResponse,
promptResponse: defaultPromptResponse,
};
};
export default (userConfig) => ({ logger, randomizer, window }) => {
const config = { ...getDefaultConfig(randomizer), ...userConfig };
const alert = window.alert;
const confirm = window.confirm;
const prompt = window.prompt;
const alertMogwai = () => {
if (!logger) {
return;
}
if (config.watchEvents.includes('alert')) {
window.alert = (msg) => {
logger.warn('mogwai ', 'alert ', msg, 'alert');
};
}
if (config.watchEvents.includes('confirm')) {
window.confirm = (msg) => {
config.confirmResponse();
logger.warn('mogwai ', 'alert ', msg, 'confirm');
};
}
if (config.watchEvents.includes('prompt')) {
window.prompt = (msg) => {
config.promptResponse();
logger.warn('mogwai ', 'alert ', msg, 'prompt');
};
}
};
alertMogwai.cleanUp = () => {
window.alert = alert;
window.confirm = confirm;
window.prompt = prompt;
return alertMogwai;
};
return alertMogwai;
};