2016-03-30 23:48:46 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var React = require('react');
|
|
|
|
var ReactDOM = require("react-dom");
|
|
|
|
var TestUtils = require('react-addons-test-utils');
|
|
|
|
var expect = require('expect');
|
2017-07-07 14:26:55 +00:00
|
|
|
import sinon from 'sinon';
|
2016-03-30 23:48:46 +00:00
|
|
|
|
|
|
|
var sdk = require('matrix-react-sdk');
|
|
|
|
|
|
|
|
var MessagePanel = sdk.getComponent('structures.MessagePanel');
|
2017-07-07 14:26:55 +00:00
|
|
|
import UserSettingsStore from '../../../src/UserSettingsStore';
|
2017-09-15 02:47:24 +00:00
|
|
|
import MatrixClientPeg from '../../../src/MatrixClientPeg';
|
2016-03-30 23:48:46 +00:00
|
|
|
|
|
|
|
var test_utils = require('test-utils');
|
|
|
|
var mockclock = require('mock-clock');
|
|
|
|
|
2016-11-14 18:20:15 +00:00
|
|
|
var client;
|
|
|
|
|
|
|
|
// wrap MessagePanel with a component which provides the MatrixClient in the context.
|
|
|
|
const WrappedMessagePanel = React.createClass({
|
|
|
|
childContextTypes: {
|
|
|
|
matrixClient: React.PropTypes.object,
|
|
|
|
},
|
|
|
|
|
|
|
|
getChildContext: function() {
|
|
|
|
return {
|
|
|
|
matrixClient: client,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
|
|
|
return <MessagePanel {...this.props} />;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2016-03-30 23:48:46 +00:00
|
|
|
describe('MessagePanel', function () {
|
|
|
|
var clock = mockclock.clock();
|
|
|
|
var realSetTimeout = window.setTimeout;
|
|
|
|
var events = mkEvents();
|
2017-09-15 02:47:24 +00:00
|
|
|
var sandbox = null;
|
2016-03-30 23:48:46 +00:00
|
|
|
|
2016-04-07 15:47:17 +00:00
|
|
|
beforeEach(function() {
|
|
|
|
test_utils.beforeEach(this);
|
2017-09-15 02:47:24 +00:00
|
|
|
sandbox = test_utils.stubClient();
|
|
|
|
client = MatrixClientPeg.get();
|
2016-04-19 20:10:23 +00:00
|
|
|
client.credentials = {userId: '@me:here'};
|
2017-07-07 14:26:55 +00:00
|
|
|
UserSettingsStore.getSyncedSettings = sinon.stub().returns({});
|
2016-04-07 15:47:17 +00:00
|
|
|
});
|
|
|
|
|
2017-07-07 14:26:55 +00:00
|
|
|
afterEach(function() {
|
2016-03-30 23:48:46 +00:00
|
|
|
clock.uninstall();
|
2017-09-15 02:47:24 +00:00
|
|
|
sandbox.restore();
|
2016-03-30 23:48:46 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
function mkEvents() {
|
|
|
|
var events = [];
|
|
|
|
var ts0 = Date.now();
|
|
|
|
for (var i = 0; i < 10; i++) {
|
|
|
|
events.push(test_utils.mkMessage(
|
|
|
|
{
|
|
|
|
event: true, room: "!room:id", user: "@user:id",
|
|
|
|
ts: ts0 + i*1000,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return events;
|
|
|
|
}
|
|
|
|
|
|
|
|
it('should show the events', function() {
|
|
|
|
var res = TestUtils.renderIntoDocument(
|
2016-11-14 18:20:15 +00:00
|
|
|
<WrappedMessagePanel className="cls" events={events} />
|
2016-03-30 23:48:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// just check we have the right number of tiles for now
|
|
|
|
var tiles = TestUtils.scryRenderedComponentsWithType(
|
|
|
|
res, sdk.getComponent('rooms.EventTile'));
|
|
|
|
expect(tiles.length).toEqual(10);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show the read-marker in the right place', function() {
|
|
|
|
var res = TestUtils.renderIntoDocument(
|
2016-11-14 18:20:15 +00:00
|
|
|
<WrappedMessagePanel className="cls" events={events} readMarkerEventId={events[4].getId()}
|
2016-03-30 23:48:46 +00:00
|
|
|
readMarkerVisible={true} />
|
|
|
|
);
|
|
|
|
|
|
|
|
var tiles = TestUtils.scryRenderedComponentsWithType(
|
|
|
|
res, sdk.getComponent('rooms.EventTile'));
|
|
|
|
|
|
|
|
// find the <li> which wraps the read marker
|
|
|
|
var rm = TestUtils.findRenderedDOMComponentWithClass(res, 'mx_RoomView_myReadMarker_container');
|
|
|
|
|
|
|
|
// it should follow the <li> which wraps the event tile for event 4
|
|
|
|
var eventContainer = ReactDOM.findDOMNode(tiles[4]).parentNode;
|
|
|
|
expect(rm.previousSibling).toEqual(eventContainer);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows a ghost read-marker when the read-marker moves', function(done) {
|
|
|
|
// fake the clock so that we can test the velocity animation.
|
|
|
|
clock.install();
|
|
|
|
clock.mockDate();
|
|
|
|
|
|
|
|
var parentDiv = document.createElement('div');
|
|
|
|
|
|
|
|
// first render with the RM in one place
|
|
|
|
var mp = ReactDOM.render(
|
2016-11-14 18:20:15 +00:00
|
|
|
<WrappedMessagePanel className="cls" events={events} readMarkerEventId={events[4].getId()}
|
2016-04-19 20:10:23 +00:00
|
|
|
readMarkerVisible={true}
|
2016-03-30 23:48:46 +00:00
|
|
|
/>, parentDiv);
|
|
|
|
|
|
|
|
var tiles = TestUtils.scryRenderedComponentsWithType(
|
|
|
|
mp, sdk.getComponent('rooms.EventTile'));
|
2016-03-31 16:01:11 +00:00
|
|
|
var tileContainers = tiles.map(function (t) {
|
|
|
|
return ReactDOM.findDOMNode(t).parentNode;
|
|
|
|
});
|
2016-03-30 23:48:46 +00:00
|
|
|
|
|
|
|
// find the <li> which wraps the read marker
|
|
|
|
var rm = TestUtils.findRenderedDOMComponentWithClass(mp, 'mx_RoomView_myReadMarker_container');
|
2016-03-31 16:01:11 +00:00
|
|
|
expect(rm.previousSibling).toEqual(tileContainers[4]);
|
2016-03-30 23:48:46 +00:00
|
|
|
|
|
|
|
// now move the RM
|
|
|
|
mp = ReactDOM.render(
|
2016-11-14 18:20:15 +00:00
|
|
|
<WrappedMessagePanel className="cls" events={events} readMarkerEventId={events[6].getId()}
|
2016-04-19 20:10:23 +00:00
|
|
|
readMarkerVisible={true}
|
2016-03-30 23:48:46 +00:00
|
|
|
/>, parentDiv);
|
|
|
|
|
|
|
|
// now there should be two RM containers
|
|
|
|
var found = TestUtils.scryRenderedDOMComponentsWithClass(mp, 'mx_RoomView_myReadMarker_container');
|
|
|
|
expect(found.length).toEqual(2);
|
2016-04-19 20:10:23 +00:00
|
|
|
|
2016-03-30 23:48:46 +00:00
|
|
|
// the first should be the ghost
|
2016-03-31 16:01:11 +00:00
|
|
|
expect(found[0].previousSibling).toEqual(tileContainers[4]);
|
|
|
|
var hr = found[0].children[0];
|
2016-03-30 23:48:46 +00:00
|
|
|
|
|
|
|
// the second should be the real thing
|
2016-03-31 16:01:11 +00:00
|
|
|
expect(found[1].previousSibling).toEqual(tileContainers[6]);
|
2016-03-30 23:48:46 +00:00
|
|
|
|
|
|
|
// advance the clock, and then let the browser run an animation frame,
|
|
|
|
// to let the animation start
|
|
|
|
clock.tick(1500);
|
2016-04-19 20:10:23 +00:00
|
|
|
|
2016-03-30 23:48:46 +00:00
|
|
|
realSetTimeout(() => {
|
|
|
|
// then advance it again to let it complete
|
|
|
|
clock.tick(1000);
|
|
|
|
realSetTimeout(() => {
|
|
|
|
// the ghost should now have finished
|
2016-03-31 00:01:49 +00:00
|
|
|
expect(hr.style.opacity).toEqual('0');
|
2016-03-30 23:48:46 +00:00
|
|
|
done();
|
|
|
|
}, 100);
|
|
|
|
}, 100);
|
|
|
|
});
|
2016-03-31 17:19:57 +00:00
|
|
|
|
|
|
|
it('shows only one ghost when the RM moves twice', function() {
|
|
|
|
var parentDiv = document.createElement('div');
|
|
|
|
|
|
|
|
// first render with the RM in one place
|
|
|
|
var mp = ReactDOM.render(
|
2016-11-14 18:20:15 +00:00
|
|
|
<WrappedMessagePanel className="cls" events={events} readMarkerEventId={events[4].getId()}
|
2016-03-31 17:19:57 +00:00
|
|
|
readMarkerVisible={true}
|
|
|
|
/>, parentDiv);
|
|
|
|
|
|
|
|
var tiles = TestUtils.scryRenderedComponentsWithType(
|
|
|
|
mp, sdk.getComponent('rooms.EventTile'));
|
|
|
|
var tileContainers = tiles.map(function (t) {
|
|
|
|
return ReactDOM.findDOMNode(t).parentNode;
|
|
|
|
});
|
|
|
|
|
|
|
|
// now move the RM
|
|
|
|
mp = ReactDOM.render(
|
2016-11-14 18:20:15 +00:00
|
|
|
<WrappedMessagePanel className="cls" events={events} readMarkerEventId={events[6].getId()}
|
2016-03-31 17:19:57 +00:00
|
|
|
readMarkerVisible={true}
|
|
|
|
/>, parentDiv);
|
|
|
|
|
|
|
|
// now there should be two RM containers
|
|
|
|
var found = TestUtils.scryRenderedDOMComponentsWithClass(mp, 'mx_RoomView_myReadMarker_container');
|
|
|
|
expect(found.length).toEqual(2);
|
|
|
|
|
|
|
|
// the first should be the ghost
|
|
|
|
expect(tileContainers.indexOf(found[0].previousSibling)).toEqual(4);
|
|
|
|
|
|
|
|
// the second should be the real RM
|
|
|
|
expect(tileContainers.indexOf(found[1].previousSibling)).toEqual(6);
|
|
|
|
|
|
|
|
// and move the RM again
|
|
|
|
mp = ReactDOM.render(
|
2016-11-14 18:20:15 +00:00
|
|
|
<WrappedMessagePanel className="cls" events={events} readMarkerEventId={events[8].getId()}
|
2016-03-31 17:19:57 +00:00
|
|
|
readMarkerVisible={true}
|
|
|
|
/>, parentDiv);
|
|
|
|
|
|
|
|
// still two RM containers
|
|
|
|
found = TestUtils.scryRenderedDOMComponentsWithClass(mp, 'mx_RoomView_myReadMarker_container');
|
|
|
|
expect(found.length).toEqual(2);
|
|
|
|
|
|
|
|
// they should have moved
|
|
|
|
expect(tileContainers.indexOf(found[0].previousSibling)).toEqual(6);
|
|
|
|
expect(tileContainers.indexOf(found[1].previousSibling)).toEqual(8);
|
|
|
|
});
|
2016-03-30 23:48:46 +00:00
|
|
|
});
|