microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
test/common/extensionMessaging.test.ts
52lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import {MessagingHelper} from "../../src/common/extensionMessaging"; |
| 5 | |
| 6 | import {RemoteExtension} from "../../src/common/remoteExtension"; |
| 7 | |
| 8 | import * as assert from "assert"; |
| 9 | import * as Q from "q"; |
| 10 | import * as rpc from "noice-json-rpc"; |
| 11 | import * as WebSocket from "ws"; |
| 12 | import WebSocketServer = WebSocket.Server; |
| 13 | |
| 14 | let mockServer: WebSocketServer; |
| 15 | |
| 16 | suite("extensionMessaging", function() { |
| 17 | suite("commonContext", function() { |
| 18 | const projectRootPath = "/myPath"; |
| 19 | const port: string = MessagingHelper.getPath(projectRootPath); |
| 20 | setup(function () { |
| 21 | mockServer = new WebSocketServer({port: <any>port}); |
| 22 | let api = new rpc.Server(mockServer).api(); |
| 23 | api.Extension.expose({ |
| 24 | stopMonitoringLogcat: function () { |
| 25 | return {data: "STOP_MONITORING_LOGCAT"}; |
| 26 | }, |
| 27 | }); |
| 28 | }); |
| 29 | |
| 30 | teardown(function() { |
| 31 | if (mockServer) { |
| 32 | mockServer.close(); |
| 33 | } |
| 34 | }); |
| 35 | |
| 36 | test("should successfully send a message", function(done: MochaDone) { |
| 37 | const sender = RemoteExtension.atProjectRootPath(projectRootPath); |
| 38 | mockServer.on("error", done); |
| 39 | |
| 40 | Q({}) |
| 41 | .then(function() { |
| 42 | return sender.stopMonitoringLogcat() |
| 43 | .then((message: any) => { |
| 44 | return message.data; |
| 45 | }); |
| 46 | }) |
| 47 | .then(function(message) { |
| 48 | assert.equal(message, "STOP_MONITORING_LOGCAT"); |
| 49 | }).done(() => done(), done); |
| 50 | }); |
| 51 | }); |
| 52 | }); |
| 53 | |