-
-
Save uchilaka/02af1d03adda97d3752e7ffa397885b5 to your computer and use it in GitHub Desktop.
mock javascript alert, confirm, prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MockPrompt = { | |
install: function() { | |
MockPrompt.message = undefined; | |
MockPrompt.value = undefined; | |
MockPrompt.returnValue = undefined; | |
MockPrompt.originalPrompt = window.prompt; | |
window.prompt = function(message, value) { | |
MockPrompt.message = message; | |
MockPrompt.value = value; | |
return MockPrompt.returnValue; | |
}; | |
}, | |
uninstall: function() { | |
window.prompt = MockPrompt.originalPrompt; | |
}, | |
willReturn: function(returnValue) { | |
MockPrompt.returnValue = returnValue; | |
} | |
}; | |
var MockConfirm = { | |
install: function() { | |
MockConfirm.message = undefined; | |
MockConfirm.returnValue = true; | |
MockConfirm.originalConfirm = window.confirm; | |
window.confirm = function(message) { | |
MockConfirm.message = message; | |
return MockConfirm.returnValue; | |
}; | |
}, | |
uninstall: function() { | |
window.confirm = MockConfirm.originalConfirm; | |
}, | |
willReturn: function(returnValue) { | |
MockConfirm.returnValue = returnValue; | |
} | |
}; | |
var MockAlert = { | |
install: function() { | |
MockAlert.message = undefined; | |
MockAlert.originalAlert = window.alert; | |
window.alert = function(message) { | |
MockAlert.message = message; | |
}; | |
}, | |
uninstall: function() { | |
MockAlert.originalAlert = window.alert; | |
} | |
}; | |
var MockDialogs = { | |
install: function() { | |
MockAlert.install(); | |
MockPrompt.install(); | |
MockConfirm.install(); | |
}, | |
uninstall: function() { | |
MockAlert.uninstall(); | |
MockPrompt.uninstall(); | |
MockConfirm.uninstall(); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setUp() { | |
MockDialogs.install(); | |
} | |
function tearDown() { | |
MockDialogs.uninstall(); | |
} | |
function testMockPrompt() { | |
MockPrompt.willReturn("goodbye"); | |
assertEquals("goodbye", prompt("hello", "default")); | |
assertEquals("hello", MockPrompt.message); | |
assertEquals("default", MockPrompt.value); | |
} | |
function testMockConfirm() { | |
assertTrue(confirm("are you sure")); | |
assertEquals("are you sure", MockConfirm.message); | |
} | |
function testMockConfirmReturnsFalse() { | |
MockConfirm.willReturn(false); | |
assertFalse(confirm("are you sure")); | |
} | |
function testMockAlert() { | |
alert("ouch!"); | |
assertEquals("ouch!", MockAlert.message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment