put mock code in separate file to reuse elsewhere
This commit is contained in:
parent
94957fcfd0
commit
08ff9e598a
2 changed files with 70 additions and 53 deletions
69
test/editor/mock.js
Normal file
69
test/editor/mock.js
Normal file
|
@ -0,0 +1,69 @@
|
||||||
|
/*
|
||||||
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {PartCreator} from "../../src/editor/parts";
|
||||||
|
|
||||||
|
class MockAutoComplete {
|
||||||
|
constructor(updateCallback, partCreator, completions) {
|
||||||
|
this._updateCallback = updateCallback;
|
||||||
|
this._partCreator = partCreator;
|
||||||
|
this._completions = completions;
|
||||||
|
this._part = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
close() {
|
||||||
|
this._updateCallback({close: true});
|
||||||
|
}
|
||||||
|
|
||||||
|
tryComplete(close = true) {
|
||||||
|
const matches = this._completions.filter(o => {
|
||||||
|
return o.resourceId.startsWith(this._part.text);
|
||||||
|
});
|
||||||
|
if (matches.length === 1 && this._part.text.length > 1) {
|
||||||
|
const match = matches[0];
|
||||||
|
let pill;
|
||||||
|
if (match.resourceId[0] === "@") {
|
||||||
|
pill = this._partCreator.userPill(match.label, match.resourceId);
|
||||||
|
} else {
|
||||||
|
pill = this._partCreator.roomPill(match.resourceId);
|
||||||
|
}
|
||||||
|
this._updateCallback({replacePart: pill, close});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// called by EditorModel when typing into pill-candidate part
|
||||||
|
onPartUpdate(part, offset) {
|
||||||
|
this._part = part;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MockClient & MockRoom are only used for avatars in room and user pills,
|
||||||
|
// which is not tested
|
||||||
|
class MockClient {
|
||||||
|
getRooms() { return []; }
|
||||||
|
getRoom() { return null; }
|
||||||
|
}
|
||||||
|
|
||||||
|
class MockRoom {
|
||||||
|
getMember() { return null; }
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createPartCreator(completions = []) {
|
||||||
|
const autoCompleteCreator = (partCreator) => {
|
||||||
|
return (updateCallback) => new MockAutoComplete(updateCallback, partCreator, completions);
|
||||||
|
};
|
||||||
|
return new PartCreator(autoCompleteCreator, new MockRoom(), new MockClient());
|
||||||
|
}
|
|
@ -16,59 +16,7 @@ limitations under the License.
|
||||||
|
|
||||||
import expect from 'expect';
|
import expect from 'expect';
|
||||||
import EditorModel from "../../src/editor/model";
|
import EditorModel from "../../src/editor/model";
|
||||||
import {PartCreator} from "../../src/editor/parts";
|
import {createPartCreator} from "./mock";
|
||||||
|
|
||||||
class MockAutoComplete {
|
|
||||||
constructor(updateCallback, partCreator, completions) {
|
|
||||||
this._updateCallback = updateCallback;
|
|
||||||
this._partCreator = partCreator;
|
|
||||||
this._completions = completions;
|
|
||||||
this._part = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
close() {
|
|
||||||
this._updateCallback({close: true});
|
|
||||||
}
|
|
||||||
|
|
||||||
tryComplete(close = true) {
|
|
||||||
const matches = this._completions.filter(o => {
|
|
||||||
return o.resourceId.startsWith(this._part.text);
|
|
||||||
});
|
|
||||||
if (matches.length === 1 && this._part.text.length > 1) {
|
|
||||||
const match = matches[0];
|
|
||||||
let pill;
|
|
||||||
if (match.resourceId[0] === "@") {
|
|
||||||
pill = this._partCreator.userPill(match.label, match.resourceId);
|
|
||||||
} else {
|
|
||||||
pill = this._partCreator.roomPill(match.resourceId);
|
|
||||||
}
|
|
||||||
this._updateCallback({replacePart: pill, close});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// called by EditorModel when typing into pill-candidate part
|
|
||||||
onPartUpdate(part, offset) {
|
|
||||||
this._part = part;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// MockClient & MockRoom are only used for avatars in room and user pills,
|
|
||||||
// which is not tested
|
|
||||||
class MockClient {
|
|
||||||
getRooms() { return []; }
|
|
||||||
getRoom() { return null; }
|
|
||||||
}
|
|
||||||
|
|
||||||
class MockRoom {
|
|
||||||
getMember() { return null; }
|
|
||||||
}
|
|
||||||
|
|
||||||
function createPartCreator(completions = []) {
|
|
||||||
const autoCompleteCreator = (partCreator) => {
|
|
||||||
return (updateCallback) => new MockAutoComplete(updateCallback, partCreator, completions);
|
|
||||||
};
|
|
||||||
return new PartCreator(autoCompleteCreator, new MockRoom(), new MockClient());
|
|
||||||
}
|
|
||||||
|
|
||||||
function createRenderer() {
|
function createRenderer() {
|
||||||
const render = (c) => {
|
const render = (c) => {
|
||||||
|
|
Loading…
Reference in a new issue