Skip to content

Commit 08fb9e0

Browse files
committed
Attempt 2 at fixing tests
1 parent 15c1d2e commit 08fb9e0

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

spec/test-utils.js

+53-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import './olm-loader';
33

44
import {logger} from '../src/logger';
55
import {MatrixEvent} from "../src/models/event";
6+
import {polyfillSuper} from "../src/utils";
67

78
/**
89
* Return a promise that is resolved when the client next emits a
@@ -43,30 +44,61 @@ export function syncPromise(client, count) {
4344
* @return {Object} An instantiated object with spied methods/properties.
4445
*/
4546
export function mock(constr, name) {
46-
// Based on
47-
// http://eclipsesource.com/blogs/2014/03/27/mocks-in-jasmine-tests/
48-
const result = new (class Anon extends constr {constructor() {
49-
super();
50-
}})();
51-
//result.prototype = constr.prototype;
52-
const origToString = result.toString.bind(result);
53-
result.toString = function() {
54-
return `Anon<${name || 'Unnamed'}> = ${origToString()}`;
55-
};
56-
console.log(Object.keys(result.constructor.__proto__));
57-
console.log(result.toString());
58-
for (const key in constr.prototype) { // eslint-disable-line guard-for-in
59-
try {
60-
if (constr.prototype[key] instanceof Function) {
61-
result[key] = jest.fn();
47+
console.log("Mocking " + name);
48+
if (Object.keys(constr.prototype).length > 0) {
49+
// Probably an actual prototype - use the old way
50+
51+
// Based on
52+
// http://eclipsesource.com/blogs/2014/03/27/mocks-in-jasmine-tests/
53+
const HelperContr = new Function(); // jshint ignore:line
54+
HelperContr.prototype = constr.prototype;
55+
const result = new HelperContr();
56+
result.prototype = constr.prototype;
57+
const origToString = result.toString.bind(result);
58+
result.toString = function() {
59+
return `Anon<${name || 'Unnamed'}> = ${origToString()}`;
60+
};
61+
for (const key in constr.prototype) { // eslint-disable-line guard-for-in
62+
try {
63+
if (constr.prototype[key] instanceof Function) {
64+
result[key] = jest.fn();
65+
}
66+
} catch (ex) {
67+
// Direct access to some non-function fields of DOM prototypes may
68+
// cause exceptions.
69+
// Overwriting will not work either in that case.
6270
}
63-
} catch (ex) {
64-
// Direct access to some non-function fields of DOM prototypes may
65-
// cause exceptions.
66-
// Overwriting will not work either in that case.
6771
}
72+
return result;
73+
} else {
74+
// Probably a class - use the new way to extend/override everything
75+
const HelperClass = class HelperClass extends constr {
76+
constructor() {
77+
try {
78+
super();
79+
polyfillSuper(this, constr);
80+
} catch (e) {
81+
// Ignore - we're about to stub the entire class anyways.
82+
}
83+
}
84+
};
85+
const result = new HelperClass();
86+
let obj = result;
87+
let depth = 0;
88+
do {
89+
if (depth++ >= 3) break; // This is where we get into node internals
90+
const props = Object.getOwnPropertyNames(obj);
91+
try {
92+
for (const prop of props) {
93+
if (typeof(obj[prop]) !== 'function' || prop === 'constructor') continue;
94+
obj[prop] = jest.fn();
95+
}
96+
} catch (e) {
97+
// Ignore - we probably just can't write to it
98+
}
99+
} while(!!(obj = Object.getPrototypeOf(obj)));
100+
return result;
68101
}
69-
return result;
70102
}
71103

72104
/**

src/models/event-timeline.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ export class EventTimeline {
209209
* @return {RoomState} state at the start/end of the timeline
210210
*/
211211
getState(direction) {
212-
if (direction == EventTimeline.BACKWARDS) {
212+
if (direction === EventTimeline.BACKWARDS) {
213213
return this._startState;
214-
} else if (direction == EventTimeline.FORWARDS) {
214+
} else if (direction === EventTimeline.FORWARDS) {
215215
return this._endState;
216216
} else {
217217
throw new Error("Invalid direction '" + direction + "'");
@@ -345,7 +345,7 @@ export class EventTimeline {
345345
*
346346
* @param {MatrixEvent} event the event whose metadata is to be set
347347
* @param {RoomState} stateContext the room state to be queried
348-
* @param {bool} toStartOfTimeline if true the event's forwardLooking flag is set false
348+
* @param {boolean} toStartOfTimeline if true the event's forwardLooking flag is set false
349349
*/
350350
static setEventMetadata(event, stateContext, toStartOfTimeline) {
351351
// set sender and target properties

0 commit comments

Comments
 (0)