source: trip-planner-front/node_modules/karma/context/karma.js@ eed0bf8

Last change on this file since eed0bf8 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.8 KB
Line 
1// Load our dependencies
2var stringify = require('../common/stringify')
3
4// Define our context Karma constructor
5function ContextKarma (callParentKarmaMethod) {
6 // Define local variables
7 var hasError = false
8 var self = this
9 var isLoaded = false
10
11 // Define our loggers
12 // DEV: These are intentionally repeated in client and context
13 this.log = function (type, args) {
14 var values = []
15
16 for (var i = 0; i < args.length; i++) {
17 values.push(this.stringify(args[i], 3))
18 }
19
20 this.info({ log: values.join(', '), type: type })
21 }
22
23 this.stringify = stringify
24
25 // Define our proxy error handler
26 // DEV: We require one in our context to track `hasError`
27 this.error = function () {
28 hasError = true
29 callParentKarmaMethod('error', [].slice.call(arguments))
30 return false
31 }
32
33 // Define our start handler
34 function UNIMPLEMENTED_START () {
35 this.error('You need to include some adapter that implements __karma__.start method!')
36 }
37 // all files loaded, let's start the execution
38 this.loaded = function () {
39 // has error -> cancel
40 if (!hasError && !isLoaded) {
41 isLoaded = true
42 try {
43 this.start(this.config)
44 } catch (error) {
45 this.error(error.stack || error.toString())
46 }
47 }
48
49 // remove reference to child iframe
50 this.start = UNIMPLEMENTED_START
51 }
52 // supposed to be overridden by the context
53 // TODO(vojta): support multiple callbacks (queue)
54 this.start = UNIMPLEMENTED_START
55
56 // Define proxy methods
57 // DEV: This is a closured `for` loop (same as a `forEach`) for IE support
58 var proxyMethods = ['complete', 'info', 'result']
59 for (var i = 0; i < proxyMethods.length; i++) {
60 (function bindProxyMethod (methodName) {
61 self[methodName] = function boundProxyMethod () {
62 callParentKarmaMethod(methodName, [].slice.call(arguments))
63 }
64 }(proxyMethods[i]))
65 }
66
67 // Define bindings for context window
68 this.setupContext = function (contextWindow) {
69 // If we clear the context after every run and we already had an error
70 // then stop now. Otherwise, carry on.
71 if (self.config.clearContext && hasError) {
72 return
73 }
74
75 // Perform window level bindings
76 // DEV: We return `self.error` since we want to `return false` to ignore errors
77 contextWindow.onerror = function () {
78 return self.error.apply(self, arguments)
79 }
80
81 contextWindow.onbeforeunload = function () {
82 return self.error('Some of your tests did a full page reload!')
83 }
84
85 contextWindow.dump = function () {
86 self.log('dump', arguments)
87 }
88
89 var _confirm = contextWindow.confirm
90 var _prompt = contextWindow.prompt
91
92 contextWindow.alert = function (msg) {
93 self.log('alert', [msg])
94 }
95
96 contextWindow.confirm = function (msg) {
97 self.log('confirm', [msg])
98 return _confirm(msg)
99 }
100
101 contextWindow.prompt = function (msg, defaultVal) {
102 self.log('prompt', [msg, defaultVal])
103 return _prompt(msg, defaultVal)
104 }
105
106 // If we want to overload our console, then do it
107 function getConsole (currentWindow) {
108 return currentWindow.console || {
109 log: function () {},
110 info: function () {},
111 warn: function () {},
112 error: function () {},
113 debug: function () {}
114 }
115 }
116 if (self.config.captureConsole) {
117 // patch the console
118 var localConsole = contextWindow.console = getConsole(contextWindow)
119 var logMethods = ['log', 'info', 'warn', 'error', 'debug']
120 var patchConsoleMethod = function (method) {
121 var orig = localConsole[method]
122 if (!orig) {
123 return
124 }
125 localConsole[method] = function () {
126 self.log(method, arguments)
127 try {
128 return Function.prototype.apply.call(orig, localConsole, arguments)
129 } catch (error) {
130 self.log('warn', ['Console method ' + method + ' threw: ' + error])
131 }
132 }
133 }
134 for (var i = 0; i < logMethods.length; i++) {
135 patchConsoleMethod(logMethods[i])
136 }
137 }
138 }
139}
140
141// Define call/proxy methods
142ContextKarma.getDirectCallParentKarmaMethod = function (parentWindow) {
143 return function directCallParentKarmaMethod (method, args) {
144 // If the method doesn't exist, then error out
145 if (!parentWindow.karma[method]) {
146 parentWindow.karma.error('Expected Karma method "' + method + '" to exist but it doesn\'t')
147 return
148 }
149
150 // Otherwise, run our method
151 parentWindow.karma[method].apply(parentWindow.karma, args)
152 }
153}
154ContextKarma.getPostMessageCallParentKarmaMethod = function (parentWindow) {
155 return function postMessageCallParentKarmaMethod (method, args) {
156 parentWindow.postMessage({ __karmaMethod: method, __karmaArguments: args }, window.location.origin)
157 }
158}
159
160// Export our module
161module.exports = ContextKarma
Note: See TracBrowser for help on using the repository browser.