@@ -3,6 +3,7 @@ import './olm-loader';
3
3
4
4
import { logger } from '../src/logger' ;
5
5
import { MatrixEvent } from "../src/models/event" ;
6
+ import { polyfillSuper } from "../src/utils" ;
6
7
7
8
/**
8
9
* Return a promise that is resolved when the client next emits a
@@ -43,30 +44,61 @@ export function syncPromise(client, count) {
43
44
* @return {Object } An instantiated object with spied methods/properties.
44
45
*/
45
46
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.
62
70
}
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.
67
71
}
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 ;
68
101
}
69
- return result ;
70
102
}
71
103
72
104
/**
0 commit comments