From 5e1a2b65e412f95efbcfb3039917c754c9949ba8 Mon Sep 17 00:00:00 2001
From: Eric Rosenbaum <eric.rosenbaum@gmail.com>
Date: Wed, 7 Nov 2018 11:52:20 -0500
Subject: [PATCH] Add test for Scratch Link unavailable error modal (#3670)

* Add test for Scratch Link unavailable error modal

* update vm
---
 package.json                              |  2 +-
 test/integration/connection-modal.test.js | 66 +++++++++++++++++++++++
 2 files changed, 67 insertions(+), 1 deletion(-)
 create mode 100644 test/integration/connection-modal.test.js

diff --git a/package.json b/package.json
index 520d091e8..fd00337f1 100644
--- a/package.json
+++ b/package.json
@@ -106,7 +106,7 @@
     "scratch-render": "0.1.0-prerelease.20181029125804",
     "scratch-storage": "1.1.0",
     "scratch-svg-renderer": "0.2.0-prerelease.20181024192149",
-    "scratch-vm": "0.2.0-prerelease.20181106161946",
+    "scratch-vm": "0.2.0-prerelease.20181107153250",
     "selenium-webdriver": "3.6.0",
     "startaudiocontext": "1.2.1",
     "style-loader": "^0.23.0",
diff --git a/test/integration/connection-modal.test.js b/test/integration/connection-modal.test.js
new file mode 100644
index 000000000..117be5ce5
--- /dev/null
+++ b/test/integration/connection-modal.test.js
@@ -0,0 +1,66 @@
+import path from 'path';
+import SeleniumHelper from '../helpers/selenium-helper';
+
+const {
+    clickText,
+    clickXpath,
+    findByText,
+    getDriver,
+    getLogs,
+    loadUri
+} = new SeleniumHelper();
+
+const uri = path.resolve(__dirname, '../../build/index.html');
+
+let driver;
+
+// The tests below require Scratch Link to be unavailable, so we can trigger
+// an error modal. To make sure this is always true, we came up with the idea of
+// injecting javascript that overwrites the global Websocket object with one that
+// attempts to connect to a fake socket address.
+const websocketFakeoutJs = `var RealWebSocket = WebSocket;
+    WebSocket = function () {
+        return new RealWebSocket("wss://fake.fake");
+    }`;
+
+describe('Hardware extension connection modal', () => {
+    beforeAll(() => {
+        driver = getDriver();
+    });
+
+    afterAll(async () => {
+        await driver.quit();
+    });
+
+    test('Message saying Scratch Link is unavailable (BLE)', async () => {
+        await loadUri(uri);
+        await clickXpath('//button[@title="Try It"]');
+
+        await driver.executeScript(websocketFakeoutJs);
+
+        await clickXpath('//button[@title="Add Extension"]');
+
+        await clickText('micro:bit');
+        await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for modal to open
+        findByText('Scratch Link'); // Scratch Link is mentioned in the error modal
+
+        const logs = await getLogs();
+        await expect(logs).toEqual([]);
+    });
+
+    test('Message saying Scratch Link is unavailable (BT)', async () => {
+        await loadUri(uri);
+        await clickXpath('//button[@title="Try It"]');
+
+        await driver.executeScript(websocketFakeoutJs);
+
+        await clickXpath('//button[@title="Add Extension"]');
+
+        await clickText('EV3');
+        await new Promise(resolve => setTimeout(resolve, 1000)); // Wait for modal to open
+        findByText('Scratch Link'); // Scratch Link is mentioned in the error modal
+
+        const logs = await getLogs();
+        await expect(logs).toEqual([]);
+    });
+});
-- 
GitLab