|
| 1 | +/* |
| 2 | +Copyright 2022 Dominik Henneke |
| 3 | +Copyright 2022 Nordeck IT + Consulting GmbH. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +import HttpBackend from "matrix-mock-request"; |
| 19 | + |
| 20 | +import { Direction, MatrixClient, MatrixScheduler } from "../../src/matrix"; |
| 21 | +import { TestClient } from "../TestClient"; |
| 22 | + |
| 23 | +describe("MatrixClient relations", () => { |
| 24 | + const userId = "@alice:localhost"; |
| 25 | + const accessToken = "aseukfgwef"; |
| 26 | + const roomId = "!room:here"; |
| 27 | + let client: MatrixClient | undefined; |
| 28 | + let httpBackend: HttpBackend | undefined; |
| 29 | + |
| 30 | + const setupTests = (): [MatrixClient, HttpBackend] => { |
| 31 | + const scheduler = new MatrixScheduler(); |
| 32 | + const testClient = new TestClient( |
| 33 | + userId, |
| 34 | + "DEVICE", |
| 35 | + accessToken, |
| 36 | + undefined, |
| 37 | + { scheduler }, |
| 38 | + ); |
| 39 | + const httpBackend = testClient.httpBackend; |
| 40 | + const client = testClient.client; |
| 41 | + |
| 42 | + return [client, httpBackend]; |
| 43 | + }; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + [client, httpBackend] = setupTests(); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + httpBackend!.verifyNoOutstandingExpectation(); |
| 51 | + return httpBackend!.stop(); |
| 52 | + }); |
| 53 | + |
| 54 | + it("should read related events with the default options", async () => { |
| 55 | + const response = client!.relations(roomId, '$event-0', null, null); |
| 56 | + |
| 57 | + httpBackend! |
| 58 | + .when("GET", "/rooms/!room%3Ahere/relations/%24event-0?dir=b") |
| 59 | + .respond(200, { chunk: [], next_batch: 'NEXT' }); |
| 60 | + |
| 61 | + await httpBackend!.flushAllExpected(); |
| 62 | + |
| 63 | + expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should read related events with relation type", async () => { |
| 67 | + const response = client!.relations(roomId, '$event-0', 'm.reference', null); |
| 68 | + |
| 69 | + httpBackend! |
| 70 | + .when("GET", "/rooms/!room%3Ahere/relations/%24event-0/m.reference?dir=b") |
| 71 | + .respond(200, { chunk: [], next_batch: 'NEXT' }); |
| 72 | + |
| 73 | + await httpBackend!.flushAllExpected(); |
| 74 | + |
| 75 | + expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should read related events with relation type and event type", async () => { |
| 79 | + const response = client!.relations(roomId, '$event-0', 'm.reference', 'm.room.message'); |
| 80 | + |
| 81 | + httpBackend! |
| 82 | + .when( |
| 83 | + "GET", |
| 84 | + "/rooms/!room%3Ahere/relations/%24event-0/m.reference/m.room.message?dir=b", |
| 85 | + ) |
| 86 | + .respond(200, { chunk: [], next_batch: 'NEXT' }); |
| 87 | + |
| 88 | + await httpBackend!.flushAllExpected(); |
| 89 | + |
| 90 | + expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); |
| 91 | + }); |
| 92 | + |
| 93 | + it("should read related events with custom options", async () => { |
| 94 | + const response = client!.relations(roomId, '$event-0', null, null, { |
| 95 | + dir: Direction.Forward, |
| 96 | + from: 'FROM', |
| 97 | + limit: 10, |
| 98 | + to: 'TO', |
| 99 | + }); |
| 100 | + |
| 101 | + httpBackend! |
| 102 | + .when( |
| 103 | + "GET", |
| 104 | + "/rooms/!room%3Ahere/relations/%24event-0?dir=f&from=FROM&limit=10&to=TO", |
| 105 | + ) |
| 106 | + .respond(200, { chunk: [], next_batch: 'NEXT' }); |
| 107 | + |
| 108 | + await httpBackend!.flushAllExpected(); |
| 109 | + |
| 110 | + expect(await response).toEqual({ "events": [], "nextBatch": "NEXT" }); |
| 111 | + }); |
| 112 | +}); |
0 commit comments