2016-04-13 17:15:04 +08:00
|
|
|
/*
|
|
|
|
Copyright 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* joining.js: tests for the various paths when joining a room */
|
|
|
|
|
2019-12-20 09:47:40 +08:00
|
|
|
import PlatformPeg from 'matrix-react-sdk/src/PlatformPeg';
|
2018-12-19 02:13:51 +08:00
|
|
|
import WebPlatform from '../../src/vector/platform/WebPlatform';
|
2020-01-14 08:38:34 +08:00
|
|
|
import * as sdk from "matrix-react-sdk";
|
|
|
|
import * as jssdk from "matrix-js-sdk";
|
|
|
|
import "../skin-sdk";
|
2020-01-16 04:00:50 +08:00
|
|
|
import "../jest-mocks";
|
2020-01-14 08:38:34 +08:00
|
|
|
import React from "react";
|
|
|
|
import ReactDOM from "react-dom";
|
|
|
|
import ReactTestUtils from "react-dom/test-utils";
|
|
|
|
import {makeType} from "matrix-react-sdk/src/utils/TypeUtils";
|
|
|
|
import {ValidatedServerConfig} from "matrix-react-sdk/src/utils/AutoDiscoveryUtils";
|
|
|
|
import {sleep} from "../test-utils";
|
|
|
|
import * as test_utils from "../test-utils";
|
|
|
|
import MockHttpBackend from "matrix-mock-request";
|
2020-01-16 04:00:50 +08:00
|
|
|
import "fake-indexeddb/auto";
|
2021-10-01 22:48:57 +08:00
|
|
|
import { RoomView as RoomViewClass } from 'matrix-react-sdk/src/components/structures/RoomView';
|
2017-11-20 04:46:40 +08:00
|
|
|
|
2018-11-23 02:25:55 +08:00
|
|
|
|
|
|
|
const MatrixChat = sdk.getComponent('structures.MatrixChat');
|
|
|
|
const RoomDirectory = sdk.getComponent('structures.RoomDirectory');
|
|
|
|
const RoomPreviewBar = sdk.getComponent('rooms.RoomPreviewBar');
|
|
|
|
|
|
|
|
const HS_URL='http://localhost';
|
|
|
|
const IS_URL='http://localhost';
|
|
|
|
const USER_ID='@me:localhost';
|
|
|
|
const ACCESS_TOKEN='access_token';
|
2016-04-13 17:15:04 +08:00
|
|
|
|
2018-11-23 02:25:55 +08:00
|
|
|
describe('joining a room', function() {
|
|
|
|
describe('over federation', function() {
|
|
|
|
let parentDiv;
|
|
|
|
let httpBackend;
|
|
|
|
let matrixChat;
|
2016-08-11 05:39:53 +08:00
|
|
|
|
2016-04-13 17:15:04 +08:00
|
|
|
beforeEach(function() {
|
|
|
|
httpBackend = new MockHttpBackend();
|
|
|
|
jssdk.request(httpBackend.requestFn);
|
|
|
|
parentDiv = document.createElement('div');
|
2016-04-13 18:16:38 +08:00
|
|
|
|
|
|
|
// uncomment this to actually add the div to the UI, to help with
|
|
|
|
// debugging (but slow things down)
|
|
|
|
// document.body.appendChild(parentDiv);
|
2016-04-13 17:15:04 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
if (parentDiv) {
|
|
|
|
ReactDOM.unmountComponentAtNode(parentDiv);
|
2016-04-13 18:16:38 +08:00
|
|
|
parentDiv.remove();
|
2016-04-13 17:15:04 +08:00
|
|
|
parentDiv = null;
|
|
|
|
}
|
|
|
|
});
|
2016-08-11 05:39:53 +08:00
|
|
|
|
2019-01-30 14:26:14 +08:00
|
|
|
// TODO: Re-enable test
|
|
|
|
// The test is currently disabled because the room directory now resides in a dialog,
|
|
|
|
// which is not accessible from the MatrixChat component anymore. Convincing react that
|
|
|
|
// the dialog does exist and is under a different tree is incredibly difficult though,
|
|
|
|
// so for now the test has been disabled. We should revisit this test when someone has
|
|
|
|
// the time to kill to figure this out. Problem area is highlighted within the test.
|
|
|
|
xit('should not get stuck at a spinner', function() {
|
2018-11-23 02:25:55 +08:00
|
|
|
const ROOM_ALIAS = '#alias:localhost';
|
|
|
|
const ROOM_ID = '!id:localhost';
|
2016-08-11 05:39:53 +08:00
|
|
|
|
2016-04-13 17:15:04 +08:00
|
|
|
httpBackend.when('GET', '/pushrules').respond(200, {});
|
|
|
|
httpBackend.when('POST', '/filter').respond(200, { filter_id: 'fid' });
|
2017-04-18 20:45:31 +08:00
|
|
|
|
|
|
|
// note that we deliberately do *not* set an expectation for a
|
|
|
|
// presence update - setting one makes the first httpBackend.flush
|
|
|
|
// return before the first /sync arrives.
|
2016-04-13 17:15:04 +08:00
|
|
|
|
|
|
|
// start with a logged-in client
|
2016-08-11 05:39:53 +08:00
|
|
|
localStorage.setItem("mx_hs_url", HS_URL );
|
|
|
|
localStorage.setItem("mx_is_url", IS_URL );
|
|
|
|
localStorage.setItem("mx_access_token", ACCESS_TOKEN );
|
|
|
|
localStorage.setItem("mx_user_id", USER_ID);
|
|
|
|
|
2018-12-19 02:13:51 +08:00
|
|
|
PlatformPeg.set(new WebPlatform());
|
2017-11-20 04:46:40 +08:00
|
|
|
|
2019-05-03 14:11:51 +08:00
|
|
|
const config = {
|
2019-05-04 06:27:41 +08:00
|
|
|
validated_server_config: makeType(ValidatedServerConfig, {
|
2019-05-03 14:11:51 +08:00
|
|
|
hsUrl: HS_URL,
|
|
|
|
hsName: "TEST_ENVIRONMENT",
|
|
|
|
hsNameIsDifferent: false, // yes, we lie
|
|
|
|
isUrl: IS_URL,
|
2019-05-04 06:27:41 +08:00
|
|
|
}),
|
2019-05-03 14:11:51 +08:00
|
|
|
};
|
|
|
|
|
2018-11-23 02:25:55 +08:00
|
|
|
const mc = (
|
2019-05-04 06:27:41 +08:00
|
|
|
<MatrixChat
|
|
|
|
config={config}
|
|
|
|
serverConfig={config.validated_server_config}
|
2017-05-18 20:39:33 +08:00
|
|
|
makeRegistrationUrl={()=>{throw new Error("unimplemented");}}
|
2017-06-06 03:16:44 +08:00
|
|
|
initialScreenAfterLogin={{
|
|
|
|
screen: 'directory',
|
|
|
|
}}
|
2017-05-18 20:39:33 +08:00
|
|
|
/>
|
|
|
|
);
|
2016-04-13 17:15:04 +08:00
|
|
|
matrixChat = ReactDOM.render(mc, parentDiv);
|
|
|
|
|
2018-11-23 02:25:55 +08:00
|
|
|
let roomView;
|
2017-04-18 20:45:31 +08:00
|
|
|
|
2017-05-18 20:39:33 +08:00
|
|
|
// wait for /sync to happen. This may take some time, as the client
|
|
|
|
// has to initialise indexeddb.
|
|
|
|
console.log("waiting for /sync");
|
|
|
|
httpBackend.when('GET', '/sync')
|
|
|
|
.respond(200, {});
|
|
|
|
|
2017-07-14 23:14:42 +08:00
|
|
|
return httpBackend.flushAllExpected({
|
|
|
|
timeout: 1000,
|
|
|
|
}).then(() => {
|
2017-04-18 20:45:31 +08:00
|
|
|
// wait for the directory requests
|
|
|
|
httpBackend.when('POST', '/publicRooms').respond(200, {chunk: []});
|
|
|
|
httpBackend.when('GET', '/thirdparty/protocols').respond(200, {});
|
2017-07-13 20:23:10 +08:00
|
|
|
return httpBackend.flushAllExpected();
|
2017-02-17 22:32:08 +08:00
|
|
|
}).then(() => {
|
2017-07-13 20:23:10 +08:00
|
|
|
console.log(`${Date.now()} App made requests for directory view; switching to a room.`);
|
|
|
|
|
2019-01-30 14:26:14 +08:00
|
|
|
// TODO: Make this look in the right spot for the directory dialog.
|
|
|
|
// See the comment block at the top of the test for a bit more information. The short
|
|
|
|
// story here is that the RoomDirectory does not exist under matrixChat anymore, or even
|
|
|
|
// the parentDiv we have access to. Asking React to find the RoomDirectory as a child of
|
|
|
|
// the document results in it complaining that you didn't give it a component tree to
|
|
|
|
// search in. When you do get a reference to the component tree based off the document
|
|
|
|
// root and ask it to search, it races and can't find the component in time. To top it
|
|
|
|
// all off, MatrixReactTestUtils can't find the element in time either even with a very
|
|
|
|
// high number of attempts. Assuming we can get a reference to the RoomDirectory in a
|
|
|
|
// dialog, the rest of the test should be fine.
|
2018-11-23 02:25:55 +08:00
|
|
|
const roomDir = ReactTestUtils.findRenderedComponentWithType(
|
2019-01-30 14:26:14 +08:00
|
|
|
matrixChat, RoomDirectory,
|
|
|
|
);
|
2016-04-13 17:15:04 +08:00
|
|
|
|
|
|
|
// enter an alias in the input, and simulate enter
|
2018-11-23 02:25:55 +08:00
|
|
|
const input = ReactTestUtils.findRenderedDOMComponentWithTag(
|
2021-10-01 22:48:57 +08:00
|
|
|
roomDir, 'input') as HTMLInputElement;
|
2016-04-13 17:15:04 +08:00
|
|
|
input.value = ROOM_ALIAS;
|
2016-09-24 00:18:52 +08:00
|
|
|
ReactTestUtils.Simulate.change(input);
|
2016-04-13 17:15:04 +08:00
|
|
|
ReactTestUtils.Simulate.keyUp(input, {key: 'Enter'});
|
|
|
|
|
|
|
|
// that should create a roomview which will start a peek; wait
|
|
|
|
// for the peek.
|
2017-04-18 20:45:31 +08:00
|
|
|
httpBackend.when('GET', '/directory/room/'+encodeURIComponent(ROOM_ALIAS)).respond(200, { room_id: ROOM_ID });
|
2016-06-18 00:49:11 +08:00
|
|
|
httpBackend.when('GET', '/rooms/'+encodeURIComponent(ROOM_ID)+"/initialSync")
|
2016-04-13 17:15:04 +08:00
|
|
|
.respond(401, {errcode: 'M_GUEST_ACCESS_FORBIDDEN'});
|
2017-06-06 03:16:44 +08:00
|
|
|
|
2017-07-13 20:23:10 +08:00
|
|
|
return httpBackend.flushAllExpected();
|
2016-04-13 17:15:04 +08:00
|
|
|
}).then(() => {
|
2017-07-13 20:23:10 +08:00
|
|
|
console.log(`${Date.now()} App made room preview request`);
|
2016-04-13 17:15:04 +08:00
|
|
|
|
2017-07-13 20:23:10 +08:00
|
|
|
// we should now have a roomview
|
2016-04-13 17:15:04 +08:00
|
|
|
roomView = ReactTestUtils.findRenderedComponentWithType(
|
2021-10-01 22:48:57 +08:00
|
|
|
matrixChat, RoomViewClass);
|
2016-04-13 17:15:04 +08:00
|
|
|
|
2017-07-13 20:23:10 +08:00
|
|
|
// the preview bar may take a tick to be displayed
|
2019-11-04 20:46:28 +08:00
|
|
|
return sleep(1);
|
2017-07-13 20:23:10 +08:00
|
|
|
}).then(() => {
|
2017-06-06 03:16:44 +08:00
|
|
|
const previewBar = ReactTestUtils.findRenderedComponentWithType(
|
2016-04-13 17:15:04 +08:00
|
|
|
roomView, RoomPreviewBar);
|
|
|
|
|
2017-06-06 03:16:44 +08:00
|
|
|
const joinLink = ReactTestUtils.findRenderedDOMComponentWithTag(
|
2016-04-13 17:15:04 +08:00
|
|
|
previewBar, 'a');
|
|
|
|
|
|
|
|
ReactTestUtils.Simulate.click(joinLink);
|
|
|
|
|
2017-06-09 01:01:55 +08:00
|
|
|
httpBackend.when('POST', '/join/'+encodeURIComponent(ROOM_ALIAS))
|
2016-04-13 17:15:04 +08:00
|
|
|
.respond(200, {room_id: ROOM_ID});
|
2016-12-16 23:20:52 +08:00
|
|
|
}).then(() => {
|
|
|
|
// wait for the join request to be made
|
2019-11-04 20:46:28 +08:00
|
|
|
return sleep(1);
|
2017-06-06 22:59:24 +08:00
|
|
|
}).then(() => {
|
|
|
|
// and again, because the state update has to go to the store and
|
|
|
|
// then one dispatch within the store, then to the view
|
|
|
|
// XXX: This is *super flaky*: a better way would be to declare
|
|
|
|
// that we expect a certain state transition to happen, then wait
|
|
|
|
// for that transition to occur.
|
2019-11-04 20:46:28 +08:00
|
|
|
return sleep(1);
|
2016-12-16 23:20:52 +08:00
|
|
|
}).then(() => {
|
2016-04-13 17:15:04 +08:00
|
|
|
// the roomview should now be loading
|
|
|
|
expect(roomView.state.room).toBe(null);
|
|
|
|
expect(roomView.state.joining).toBe(true);
|
|
|
|
|
|
|
|
// there should be a spinner
|
|
|
|
ReactTestUtils.findRenderedDOMComponentWithClass(
|
|
|
|
roomView, "mx_Spinner");
|
|
|
|
|
2017-06-06 03:16:44 +08:00
|
|
|
// flush it through
|
2017-06-09 01:01:55 +08:00
|
|
|
return httpBackend.flush('/join/'+encodeURIComponent(ROOM_ALIAS));
|
2017-06-06 03:16:44 +08:00
|
|
|
}).then(() => {
|
|
|
|
httpBackend.verifyNoOutstandingExpectation();
|
|
|
|
|
2019-11-04 20:46:28 +08:00
|
|
|
return sleep(1);
|
2017-06-06 22:59:24 +08:00
|
|
|
}).then(() => {
|
2017-09-19 17:23:12 +08:00
|
|
|
// NB. we don't expect the 'joining' flag to reset at any point:
|
|
|
|
// it will stay set and we observe whether we have Room object for
|
|
|
|
// the room and whether our member event shows we're joined.
|
|
|
|
|
2016-04-13 17:15:04 +08:00
|
|
|
// now send the room down the /sync pipe
|
|
|
|
httpBackend.when('GET', '/sync').
|
|
|
|
respond(200, {
|
|
|
|
rooms: {
|
|
|
|
join: {
|
|
|
|
[ROOM_ID]: {
|
|
|
|
state: {},
|
|
|
|
timeline: {
|
|
|
|
events: [],
|
|
|
|
limited: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return httpBackend.flush();
|
|
|
|
}).then(() => {
|
|
|
|
// now the room should have loaded
|
2021-10-01 22:48:57 +08:00
|
|
|
expect(roomView.state.room).toBeTruthy();
|
2017-06-14 23:07:40 +08:00
|
|
|
});
|
2016-04-13 17:15:04 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|