From 571191cfbe41010c6fa4283bd7d8447d5fcbb4ba Mon Sep 17 00:00:00 2001
From: Chelsea Urquhart <curquhart@users.noreply.github.com>
Date: Fri, 18 Jan 2019 15:08:14 -0800
Subject: [PATCH] fix(client): fix issue with loaded on safari 10 (#3252)

Safari 10 supports type=module but ignores nomodule which causes loaded() to execute twice.

Fixes #3198
---
 context/karma.js          |  4 +++-
 test/client/karma.spec.js | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/context/karma.js b/context/karma.js
index a013b2adb..3a12ca4f7 100644
--- a/context/karma.js
+++ b/context/karma.js
@@ -6,6 +6,7 @@ function ContextKarma (callParentKarmaMethod) {
   // Define local variables
   var hasError = false
   var self = this
+  var isLoaded = false
 
   // Define our loggers
   // DEV: These are intentionally repeated in client and context
@@ -36,7 +37,8 @@ function ContextKarma (callParentKarmaMethod) {
   // all files loaded, let's start the execution
   this.loaded = function () {
     // has error -> cancel
-    if (!hasError) {
+    if (!hasError && !isLoaded) {
+      isLoaded = true
       try {
         this.start(this.config)
       } catch (error) {
diff --git a/test/client/karma.spec.js b/test/client/karma.spec.js
index d07995a99..1994775e4 100644
--- a/test/client/karma.spec.js
+++ b/test/client/karma.spec.js
@@ -433,5 +433,20 @@ describe('Karma', function () {
 
       assert.equal(iframe.src, CURRENT_URL)
     })
+
+    it('should accept multiple calls to loaded', function () {
+      // support for Safari 10 since it supports type=module but not nomodule.
+      var config = ck.config = {
+        useIframe: true
+      }
+
+      socket.emit('execute', config)
+      assert(!startSpy.called)
+
+      ck.loaded()
+      ck.loaded()
+      assert(startSpy.calledWith(config))
+      assert(startSpy.getCalls().length === 1)
+    })
   })
 })