microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/test/common/extensionMessaging.test.ts
130lines · modeblame
347157a1Joshua Skelton10 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. | |
| 3 | | |
df1e4c6fdigeff10 years ago | 4 | import {ExtensionMessage, MessagingChannel} from "../../common/extensionMessaging"; |
347157a1Joshua Skelton10 years ago | 5 | |
df1e4c6fdigeff10 years ago | 6 | import {RemoteExtension} from "../../common/remoteExtension"; |
725cf712digeff10 years ago | 7 | |
df1e4c6fdigeff10 years ago | 8 | import {InterProcessMessageSender} from "../../common/interProcessMessageSender"; |
76c41138digeff10 years ago | 9 | |
347157a1Joshua Skelton10 years ago | 10 | import * as assert from "assert"; |
| 11 | import * as net from "net"; | |
| 12 | import * as Q from "q"; | |
| 13 | | |
8f09b17cJoshua Skelton10 years ago | 14 | let mockServer: net.Server; |
| 15 | | |
347157a1Joshua Skelton10 years ago | 16 | suite("extensionMessaging", function() { |
| 17 | suite("commonContext", function() { | |
d3950d7edigeff10 years ago | 18 | const projectRootPath = "/myPath"; |
| 19 | const port: string = new MessagingChannel(projectRootPath).getPath(); | |
| 20 | | |
347157a1Joshua Skelton10 years ago | 21 | teardown(function() { |
8f09b17cJoshua Skelton10 years ago | 22 | if (mockServer) { |
| 23 | mockServer.close(); | |
347157a1Joshua Skelton10 years ago | 24 | } |
| 25 | }); | |
| 26 | | |
| 27 | test("should successfully send a message", function(done: MochaDone) { | |
ad7eb7feJoshua Skelton10 years ago | 28 | let receivedMessage: ExtensionMessage; |
| 29 | | |
8f09b17cJoshua Skelton10 years ago | 30 | mockServer = net.createServer(function(client: net.Socket): void { |
347157a1Joshua Skelton10 years ago | 31 | mockServer.close(); |
| 32 | client.on("data", function(data: Buffer) { | |
8f09b17cJoshua Skelton10 years ago | 33 | const messageData: any = JSON.parse(data.toString("utf8")); |
347157a1Joshua Skelton10 years ago | 34 | client.end(); |
| 35 | | |
ad7eb7feJoshua Skelton10 years ago | 36 | receivedMessage = messageData.message; |
347157a1Joshua Skelton10 years ago | 37 | }); |
| 38 | }); | |
| 39 | | |
| 40 | mockServer.on("error", done); | |
c1c16452Joshua Skelton10 years ago | 41 | mockServer.listen(port); |
347157a1Joshua Skelton10 years ago | 42 | |
c7f1165cdigeff10 years ago | 43 | const sender = RemoteExtension.atProjectRootPath(projectRootPath); |
347157a1Joshua Skelton10 years ago | 44 | |
| 45 | Q({}) | |
| 46 | .then(function() { | |
725cf712digeff10 years ago | 47 | return sender.startPackager(); |
ad7eb7feJoshua Skelton10 years ago | 48 | }) |
| 49 | .then(function() { | |
| 50 | assert.equal(receivedMessage, ExtensionMessage.START_PACKAGER); | |
347157a1Joshua Skelton10 years ago | 51 | }).done(() => done(), done); |
| 52 | }); | |
| 53 | | |
| 54 | test("should successfully send a message with args", function(done: MochaDone) { | |
8f09b17cJoshua Skelton10 years ago | 55 | const args = ["android"]; |
ad7eb7feJoshua Skelton10 years ago | 56 | let receivedMessage: ExtensionMessage; |
| 57 | let receivedArgs: any; | |
| 58 | | |
8f09b17cJoshua Skelton10 years ago | 59 | mockServer = net.createServer(function(client: net.Socket): void { |
347157a1Joshua Skelton10 years ago | 60 | mockServer.close(); |
| 61 | client.on("data", function(data: Buffer) { | |
8f09b17cJoshua Skelton10 years ago | 62 | const messageData: any = JSON.parse(data.toString("utf8")); |
347157a1Joshua Skelton10 years ago | 63 | client.end(); |
| 64 | | |
ad7eb7feJoshua Skelton10 years ago | 65 | receivedMessage = messageData.message; |
| 66 | receivedArgs = messageData.args; | |
347157a1Joshua Skelton10 years ago | 67 | }); |
| 68 | }); | |
| 69 | | |
| 70 | mockServer.on("error", done); | |
c1c16452Joshua Skelton10 years ago | 71 | mockServer.listen(port); |
347157a1Joshua Skelton10 years ago | 72 | |
c7f1165cdigeff10 years ago | 73 | const sender = RemoteExtension.atProjectRootPath(projectRootPath); |
347157a1Joshua Skelton10 years ago | 74 | |
| 75 | Q({}) | |
| 76 | .then(function() { | |
725cf712digeff10 years ago | 77 | return sender.prewarmBundleCache(args[0]); |
ad7eb7feJoshua Skelton10 years ago | 78 | }) |
| 79 | .then(function() { | |
| 80 | assert.equal(receivedMessage, ExtensionMessage.PREWARM_BUNDLE_CACHE); | |
| 81 | assert.deepEqual(receivedArgs, args); | |
347157a1Joshua Skelton10 years ago | 82 | }).done(() => done(), done); |
| 83 | }); | |
| 84 | | |
4b124a08Jimmy Thomson9 years ago | 85 | test("should reject on failed communication", function(done: MochaDone) { |
| 86 | | |
| 87 | mockServer = net.createServer(function(client: net.Socket): void { | |
| 88 | mockServer.close(); | |
| 89 | client.on("data", function(data: Buffer) { | |
| 90 | client.end("vscodereactnative-error-marker"); | |
| 91 | }); | |
| 92 | }); | |
| 93 | | |
| 94 | mockServer.on("error", done); | |
| 95 | mockServer.listen(port); | |
| 96 | | |
| 97 | const sender = RemoteExtension.atProjectRootPath(projectRootPath); | |
| 98 | | |
| 99 | Q({}) | |
| 100 | .then(function() { | |
| 101 | return sender.prewarmBundleCache("android"); | |
| 102 | }) | |
| 103 | .then(function() { | |
| 104 | assert(false, "sendMessage should reject on failed communication"); | |
| 105 | }, | |
| 106 | function(reason: any) { | |
| 107 | let expectedErrorMessage = "An error ocurred while handling message: PREWARM_BUNDLE_CACHE"; | |
| 108 | assert.equal(reason.message, expectedErrorMessage); | |
| 109 | }) | |
| 110 | .done(() => done(), done); | |
| 111 | }); | |
| 112 | | |
347157a1Joshua Skelton10 years ago | 113 | test("should reject on socket error", function(done: MochaDone) { |
725cf712digeff10 years ago | 114 | const sender = new InterProcessMessageSender(projectRootPath); |
347157a1Joshua Skelton10 years ago | 115 | |
| 116 | Q({}) | |
| 117 | .then(function() { | |
| 118 | return sender.sendMessage(ExtensionMessage.PREWARM_BUNDLE_CACHE); | |
| 119 | }) | |
| 120 | .then(function() { | |
15c3e378Joshua Skelton10 years ago | 121 | assert(false, "sendMessage should reject on socket error"); |
e9868794Joshua Skelton10 years ago | 122 | }, |
| 123 | function(reason: any) { | |
4b124a08Jimmy Thomson9 years ago | 124 | let expectedErrorMessage = "Unable to set up communication with VSCode react-native extension. Is this a react-native project, and have you made sure that the react-native npm package is installed at the root?"; |
347157a1Joshua Skelton10 years ago | 125 | assert.equal(reason.message, expectedErrorMessage); |
| 126 | }) | |
| 127 | .done(() => done(), done); | |
| 128 | }); | |
| 129 | }); | |
df1e4c6fdigeff10 years ago | 130 | }); |