From 3a618b3ce51f7fb8da94636562ba2ed785da0579 Mon Sep 17 00:00:00 2001 From: Wesley Cho Date: Sat, 14 Jan 2017 11:40:56 -0800 Subject: [PATCH] feat(client): capture confirm & prompt Capture `window.confirm` & `window.prompt` usages in client Fixes #694 --- context/karma.js | 13 +++++++++++++ test/client/karma.spec.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/context/karma.js b/context/karma.js index 75eac7a8c..681c556b8 100644 --- a/context/karma.js +++ b/context/karma.js @@ -80,10 +80,23 @@ var ContextKarma = function (callParentKarmaMethod) { self.log('dump', arguments) } + var _confirm = contextWindow.confirm + var _prompt = contextWindow.prompt + contextWindow.alert = function (msg) { self.log('alert', [msg]) } + contextWindow.confirm = function (msg) { + self.log('confirm', [msg]) + _confirm(msg) + } + + contextWindow.prompt = function (msg, defaultVal) { + self.log('prompt', [msg, defaultVal]) + _prompt(msg, defaultVal) + } + // If we want to overload our console, then do it var getConsole = function (currentWindow) { return currentWindow.console || { diff --git a/test/client/karma.spec.js b/test/client/karma.spec.js index 79c390150..7954243ca 100644 --- a/test/client/karma.spec.js +++ b/test/client/karma.spec.js @@ -252,6 +252,38 @@ describe('Karma', function () { mockWindow.alert('What?') assert(ck.log.calledWith('alert', ['What?'])) }) + + it('should capture confirm', function () { + sinon.spy(ck, 'log') + var confirmCalled = false + + var mockWindow = { + confirm: function () { + confirmCalled = true + } + } + + ck.setupContext(mockWindow) + mockWindow.confirm('What?') + assert(ck.log.calledWith('confirm', ['What?'])) + assert.equal(confirmCalled, true) + }) + + it('should capture prompt', function () { + sinon.spy(ck, 'log') + var promptCalled = false + + var mockWindow = { + prompt: function () { + promptCalled = true + } + } + + ck.setupContext(mockWindow) + mockWindow.prompt('What is your favorite color?', 'blue') + assert(ck.log.calledWith('prompt', ['What is your favorite color?', 'blue'])) + assert.equal(promptCalled, true) + }) }) describe('complete', function () {