From 367e76d235f5f37fa2d6aa305791d7387502c831 Mon Sep 17 00:00:00 2001
From: Will Hunt <will@half-shot.uk>
Date: Fri, 18 May 2018 10:49:44 +0100
Subject: [PATCH] Complete tests for MRH

---
 test/mocks/channel.ts          |  3 +-
 test/test_matrixroomhandler.ts | 54 ++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 2 deletions(-)

diff --git a/test/mocks/channel.ts b/test/mocks/channel.ts
index c9669f9..ba68309 100644
--- a/test/mocks/channel.ts
+++ b/test/mocks/channel.ts
@@ -1,9 +1,8 @@
-import {MockUser} from "./user";
-import * as Discord from "discord.js";
 import {MockMember} from "./member";
 import {MockCollection} from "./collection";
 
 // Mocking TextChannel
 export class MockChannel {
+    constructor (public id: string = "", public guild: any = null) { }
     public members = new MockCollection<string, MockMember>();
 }
diff --git a/test/test_matrixroomhandler.ts b/test/test_matrixroomhandler.ts
index b846ed0..343bfb2 100644
--- a/test/test_matrixroomhandler.ts
+++ b/test/test_matrixroomhandler.ts
@@ -11,6 +11,7 @@ import {MockChannel} from "./mocks/channel";
 import {MockMember} from "./mocks/member";
 import * as Bluebird from "bluebird";
 import {MockGuild} from "./mocks/guild";
+import {Guild} from "discord.js";
 
 Chai.use(ChaiAsPromised);
 const expect = Chai.expect;
@@ -479,4 +480,57 @@ describe("MatrixRoomHandler", () => {
             return expect(handler.tpParseUser("alias")).to.eventually.be.rejected;
         });
     });
+    describe("joinRoom", () => {
+        it("will join immediately", () => {
+            const handler: any = createRH({});
+            const intent = {
+                getClient: () => {
+                    return {
+                      joinRoom: () => {
+                          return Promise.resolve();
+                      }
+                    };
+                }
+            };
+            const startTime = Date.now();
+            const MAXTIME = 1000;
+            return expect(handler.joinRoom(intent, "#test:localhost")).to.eventually.be.fulfilled.and.satisfy(() => {
+                return (Date.now() - startTime) < MAXTIME;
+            });
+        });
+        it("will fail first, join after", () => {
+            log.level = "error";
+            const handler: any = createRH({});
+            let shouldFail = true;
+            const intent = {
+                getClient: () => {
+                    return {
+                        joinRoom: () => {
+                            if (shouldFail) {
+                                shouldFail = false;
+                                return Promise.reject("Test failed first time");
+                            }
+                            return Promise.resolve();
+                        },
+                        getUserId: () => "@test:localhost",
+                    };
+                }
+            };
+            const startTime = Date.now();
+            const MINTIME = 1000;
+            return expect(handler.joinRoom(intent, "#test:localhost")).to.eventually.be.fulfilled.and.satisfy(() => {
+                expect(shouldFail).to.be.false;
+                return (Date.now() - startTime) > MINTIME;
+            });
+        });
+    });
+    describe("createMatrixRoom", () => {
+        it("will return an object", () => {
+            const handler: any = createRH({});
+            const channel = new MockChannel("123", new MockGuild("456"));
+            const roomOpts = handler.createMatrixRoom(channel, "#test:localhost");
+            expect(roomOpts.creationOpts).to.exist;
+            expect(roomOpts.remote).to.exist;
+        });
+    });
 });
-- 
GitLab