You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This util is built with and for [Jasmine](https://jasmine.github.io/) test framework. Basic understanding of Jasmine is assumed.
26
47
27
-
This util requires [ES6 Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) and only contains un-compiled `*.ts` files which must be compiled with a [TypeScript](https://www.typescriptlang.org/) compiler.
28
-
48
+
This util requires [ES6 Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) and only contains `*.ts` files that must be compiled with a [TypeScript](https://www.typescriptlang.org/) compiler.
29
49
30
50
31
51
## Usage
52
+
32
53
### Install
33
54
```Shell
34
55
npminstalljasmine-mock-factory--save-dev
@@ -62,33 +83,101 @@ const realInstance: RealInterface = new RealClass();
`MockFactory.create()` will return an object with the same interface as the original object. You can invoke methods and get/set properties on this object.
86
+
#### From window objects
87
+
```TypeScript
88
+
/* make sure you have included dom types from the TypeScript library */
89
+
constmockWindow=MockFactory.create(window);
90
+
constmockDocument=MockFactory.create(document);
91
+
constmockLocation=MockFactory.create(location);
92
+
```
67
93
68
-
* All the public and private methods will have a jasmine.Spy as the initial value. The Spy cannot be overwritten.
69
-
* All the public and private properties will have `undefined` as the initial value. The value can be overwritten with anything.
70
-
71
-
### Examples
94
+
### Using a mock
95
+
* `MockFactory.create()` will return an object with the same interface as the real object. You can invoke functions and access properties on this object.
96
+
* In addition, the mock object provides a `_spy` facade, where you can access and config spies on functions and properties.
72
97
```TypeScript
73
-
classRealClass {
74
-
public doSomething(...arg:any[]) { ... }
75
-
public someProperty ='whatever';
76
-
}
98
+
constmockInstance=MockFactory.create(location);
99
+
mockInstance.reload(); // use it as if it were the real window.location
100
+
lettemp=mockInstance.search; // use it as if it were the real window.search
101
+
mockInstance.hash='myHash'; // use it as if it were the real window.hash
102
+
mockInstance._spy.reload._func; // returns the spy behind location.reload
103
+
mockInstance._spy.search._get; // returns the spy behind the getter for location.search
104
+
mockInstance._spy.hash._set; // returns the spy behind the setter for location.hash
105
+
```
77
106
78
-
constmockInstance=MockFactory.create(RealClass);
107
+
#### Invoking functions
108
+
* All functions will have a `jasmine.Spy` as the initial value. The spy cannot be overwritten and returns `undefined` by default.
109
+
* To access protected and private functions, cast the mockInstance `asany` or use bracket notation.
110
+
```TypeScript
111
+
mockInstance.publicFunction(42); // the spy behind it is invoked with 42
* All properties have spies on the getter and setter, even if the getter and setter don't exist in the real object.
153
+
* Access a getter spy on `mockInstance._spy.propertyName._get`.
154
+
* Access a setter spy on `mockInstance._spy.propertyName._set`.
155
+
* NOTE: modification to the properties will not persist after getter or setter spies are customized
156
+
* NOTE: `expect(mockInstance.someProperty).toBe(...)` will trigger `mockInstance._spy.someProperty._get`. Design the sequence of your assertions carefully to avoid shooting yourself in the foot.
0 commit comments