tsmockit 1.1.2 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Mock/Mock.d.ts +1 -1
- package/Mock/Mock.js +15 -28
- package/Mock/SignatureService.d.ts +1 -1
- package/Mock/SignatureService.js +3 -2
- package/Mock/TypeLiterals.d.ts +1 -0
- package/Mock/module.d.ts +2 -0
- package/Mock/module.js +6 -0
- package/README.md +14 -6
- package/Utility/EmitEventAtElement.d.ts +6 -0
- package/Utility/EmitEventAtElement.js +15 -0
- package/Utility/EmitKeyEventAtElement.d.ts +7 -0
- package/Utility/EmitKeyEventAtElement.js +18 -0
- package/Utility/Expect.d.ts +7 -0
- package/Utility/Expect.js +29 -0
- package/Utility/TestHelpers.d.ts +28 -0
- package/Utility/TestHelpers.js +40 -8
- package/Utility/module.d.ts +4 -0
- package/Utility/module.js +8 -0
- package/package.json +2 -2
- package/public_api.d.ts +2 -3
- package/public_api.js +2 -6
package/Mock/Mock.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export declare class Mock<T> {
|
|
|
3
3
|
private memberSignatureMaps;
|
|
4
4
|
private object;
|
|
5
5
|
get Object(): T;
|
|
6
|
-
Setup(member: (func: T) => any, returns?: any): void;
|
|
6
|
+
Setup(member: (func: T) => any, returns?: any, exactSignatureMatch?: boolean): void;
|
|
7
7
|
TimesMemberCalled(member: (func: T) => any): number;
|
|
8
8
|
Verify(member: (func: T) => any, times: Times | number): void;
|
|
9
9
|
private getReturnsValueForProperty;
|
package/Mock/Mock.js
CHANGED
|
@@ -14,10 +14,11 @@ var Mock = /** @class */ (function () {
|
|
|
14
14
|
enumerable: false,
|
|
15
15
|
configurable: true
|
|
16
16
|
});
|
|
17
|
-
Mock.prototype.Setup = function (member, returns) {
|
|
17
|
+
Mock.prototype.Setup = function (member, returns, exactSignatureMatch) {
|
|
18
18
|
var _this = this;
|
|
19
19
|
if (returns === void 0) { returns = null; }
|
|
20
|
-
|
|
20
|
+
if (exactSignatureMatch === void 0) { exactSignatureMatch = false; }
|
|
21
|
+
var memberSignatureMap = SignatureService_1.SignatureService.GetMemberSignatureMap(member, returns, exactSignatureMatch);
|
|
21
22
|
this.updateMemberSignatureMaps(memberSignatureMap);
|
|
22
23
|
var memberName = SignatureService_1.SignatureService.GetMemberNameFromSignature(memberSignatureMap.signature);
|
|
23
24
|
if (SignatureService_1.SignatureService.MemberSignatureIsProperty(memberSignatureMap.signature)) {
|
|
@@ -25,13 +26,12 @@ var Mock = /** @class */ (function () {
|
|
|
25
26
|
}
|
|
26
27
|
else {
|
|
27
28
|
this.object[memberName] = (function () {
|
|
29
|
+
var _a;
|
|
28
30
|
var args = [];
|
|
29
31
|
for (var _i = 0; _i < arguments.length; _i++) {
|
|
30
32
|
args[_i] = arguments[_i];
|
|
31
33
|
}
|
|
32
|
-
|
|
33
|
-
return returnFunction ? returnFunction() :
|
|
34
|
-
(function () { return console.error('Unable to resolve setup function'); })();
|
|
34
|
+
return (_a = _this.getReturnsForFunction(memberSignatureMap, args, exactSignatureMatch)) === null || _a === void 0 ? void 0 : _a();
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
37
|
};
|
|
@@ -53,19 +53,15 @@ var Mock = /** @class */ (function () {
|
|
|
53
53
|
}
|
|
54
54
|
return value;
|
|
55
55
|
};
|
|
56
|
-
Mock.prototype.getReturnsForFunction = function (memberSignatureMap, args) {
|
|
57
|
-
var returnFunction;
|
|
56
|
+
Mock.prototype.getReturnsForFunction = function (memberSignatureMap, args, exactSignatureMatch) {
|
|
58
57
|
var existingMemberSignatureMap = this.memberSignatureMaps.find(function (s) { return s.signature === memberSignatureMap.signature; });
|
|
59
|
-
var
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
return returnFunction;
|
|
58
|
+
var exactFunctionMap = existingMemberSignatureMap === null || existingMemberSignatureMap === void 0 ? void 0 : existingMemberSignatureMap.functionMaps.find(function (m) { return JSON.stringify(m.state) === JSON.stringify(args); });
|
|
59
|
+
var defaultFunctionMap = exactSignatureMatch ? undefined : existingMemberSignatureMap === null || existingMemberSignatureMap === void 0 ? void 0 : existingMemberSignatureMap.functionMaps.find(function (m) { return m.default; });
|
|
60
|
+
var functionMap = exactFunctionMap || defaultFunctionMap;
|
|
61
|
+
return functionMap ? (function () {
|
|
62
|
+
functionMap.timesCalled++;
|
|
63
|
+
return functionMap.returns;
|
|
64
|
+
}) : undefined;
|
|
69
65
|
};
|
|
70
66
|
Mock.prototype.updateMemberSignatureMaps = function (memberSignatureMap) {
|
|
71
67
|
var existingSignatureMap = this.memberSignatureMaps.find(function (m) { return m.signature === memberSignatureMap.signature; });
|
|
@@ -88,19 +84,10 @@ var Mock = /** @class */ (function () {
|
|
|
88
84
|
}
|
|
89
85
|
};
|
|
90
86
|
Mock.prototype.getFunctionMapFromSignatureMap = function (memberSignature) {
|
|
91
|
-
var functionMap;
|
|
92
87
|
var existingMember = this.memberSignatureMaps.find(function (m) { return m.signature === memberSignature.signature; });
|
|
93
88
|
var functionMapToFind = memberSignature.functionMaps[0];
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
if (functionMapCount === 1) {
|
|
97
|
-
functionMap = existingMember.functionMaps[0];
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
functionMap = existingMember.functionMaps.find(function (m) { return JSON.stringify(m.state) === JSON.stringify(functionMapToFind.state); });
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return functionMap;
|
|
89
|
+
return (existingMember === null || existingMember === void 0 ? void 0 : existingMember.functionMaps.find(function (m) { return JSON.stringify(m.state) === JSON.stringify(functionMapToFind.state); })) ||
|
|
90
|
+
(existingMember === null || existingMember === void 0 ? void 0 : existingMember.functionMaps.find(function (m) { return m.default; }));
|
|
104
91
|
};
|
|
105
92
|
return Mock;
|
|
106
93
|
}());
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { SignatureMap } from './TypeLiterals';
|
|
2
2
|
export declare class SignatureService {
|
|
3
|
-
static GetMemberSignatureMap(value: (obj: any) => any, returns?: any): SignatureMap;
|
|
3
|
+
static GetMemberSignatureMap(value: (obj: any) => any, returns?: any, exactSignatureMatch?: boolean): SignatureMap;
|
|
4
4
|
static MemberSignatureIsProperty(memberSignatureString: string): boolean;
|
|
5
5
|
static GetMemberNameFromSignature(memberSignatureString: string): string;
|
|
6
6
|
private static getPropertyMemberSignature;
|
package/Mock/SignatureService.js
CHANGED
|
@@ -5,7 +5,8 @@ var Constants_1 = require("./Constants");
|
|
|
5
5
|
var SignatureService = /** @class */ (function () {
|
|
6
6
|
function SignatureService() {
|
|
7
7
|
}
|
|
8
|
-
SignatureService.GetMemberSignatureMap = function (value, returns) {
|
|
8
|
+
SignatureService.GetMemberSignatureMap = function (value, returns, exactSignatureMatch) {
|
|
9
|
+
if (exactSignatureMatch === void 0) { exactSignatureMatch = false; }
|
|
9
10
|
var memberSignature = '';
|
|
10
11
|
memberSignature =
|
|
11
12
|
this.getOperationMemberSignature(value, memberSignature) ||
|
|
@@ -14,7 +15,7 @@ var SignatureService = /** @class */ (function () {
|
|
|
14
15
|
SignatureService.getStateForMemberSignature(memberSignature, value);
|
|
15
16
|
return {
|
|
16
17
|
signature: memberSignature,
|
|
17
|
-
functionMaps: [{ state: state, returns: returns ? returns : null, timesCalled: 0 }]
|
|
18
|
+
functionMaps: [{ default: !exactSignatureMatch, state: state, returns: returns ? returns : null, timesCalled: 0 }]
|
|
18
19
|
};
|
|
19
20
|
};
|
|
20
21
|
SignatureService.MemberSignatureIsProperty = function (memberSignatureString) {
|
package/Mock/TypeLiterals.d.ts
CHANGED
package/Mock/module.d.ts
ADDED
package/Mock/module.js
ADDED
package/README.md
CHANGED
|
@@ -2,7 +2,6 @@
|
|
|
2
2
|
Generic mocking library for TypeScript
|
|
3
3
|
|
|
4
4
|
[](https://github.com/bayes343/tsmockit/actions/workflows/ci.yml)
|
|
5
|
-

|
|
6
5
|

|
|
7
6
|
|
|
8
7
|
|
|
@@ -20,7 +19,8 @@ class Mock<T>
|
|
|
20
19
|
```typescript
|
|
21
20
|
Setup(
|
|
22
21
|
member: (func: T) => any,
|
|
23
|
-
returns: any = null
|
|
22
|
+
returns: any = null,
|
|
23
|
+
exactSignatureMatch = false // will default to true and replace the signature matching convention as of v2.0.0.
|
|
24
24
|
): void
|
|
25
25
|
```
|
|
26
26
|
```typescript
|
|
@@ -49,7 +49,12 @@ static EmitKeyEventAtElement(
|
|
|
49
49
|
): void
|
|
50
50
|
```
|
|
51
51
|
```typescript
|
|
52
|
-
static async
|
|
52
|
+
static async Expect<T>(
|
|
53
|
+
selector: () => T,
|
|
54
|
+
assertion: (m: jasmine.Matchers<T>) => void,
|
|
55
|
+
interval = 0,
|
|
56
|
+
getTimeFunc = () => Date.now()
|
|
57
|
+
): Promise<void>
|
|
53
58
|
```
|
|
54
59
|
|
|
55
60
|
## Usage
|
|
@@ -140,6 +145,9 @@ mockIStereo.Setup(s => s.SetStation(2), 'Station set to 2');
|
|
|
140
145
|
```
|
|
141
146
|
|
|
142
147
|
## Conventions
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
148
|
+
|
|
149
|
+
***This convention will be replaced by passing `false` to the `exactSignatureMatch` param when calling `Setup` in version 2.0.0.***
|
|
150
|
+
|
|
151
|
+
You'll often want a mock to return the same response regardless of input parameters. The following convention accommodates this:
|
|
152
|
+
|
|
153
|
+
- If a method is called and no exact setup is available, the first `Setup` called without overriding `exactSignatureMatch` to `true` will be used.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EmitEventAtElement = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Emit an event at a given element
|
|
6
|
+
* @param element
|
|
7
|
+
* @param eventType
|
|
8
|
+
*/
|
|
9
|
+
function EmitEventAtElement(element, eventType) {
|
|
10
|
+
var event = document.createEvent('Event');
|
|
11
|
+
event.initEvent(eventType);
|
|
12
|
+
element.dispatchEvent(event);
|
|
13
|
+
}
|
|
14
|
+
exports.EmitEventAtElement = EmitEventAtElement;
|
|
15
|
+
//# sourceMappingURL=EmitEventAtElement.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EmitKeyEventAtElement = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Emit a key event at a given element
|
|
6
|
+
* @param element
|
|
7
|
+
* @param key
|
|
8
|
+
* @param keyEvent
|
|
9
|
+
*/
|
|
10
|
+
function EmitKeyEventAtElement(element, key, keyEvent) {
|
|
11
|
+
var event = document.createEvent('Event');
|
|
12
|
+
event['keyCode'] = key;
|
|
13
|
+
event['key'] = key;
|
|
14
|
+
event.initEvent(keyEvent);
|
|
15
|
+
element.dispatchEvent(event);
|
|
16
|
+
}
|
|
17
|
+
exports.EmitKeyEventAtElement = EmitKeyEventAtElement;
|
|
18
|
+
//# sourceMappingURL=EmitKeyEventAtElement.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/// <reference types="jasmine" />
|
|
2
|
+
/**
|
|
3
|
+
* Performs an asynchronous assertion, allowing up to one second to pass before failing
|
|
4
|
+
* @param selector A function that returns the subject of the assertion once it is expected to pass.
|
|
5
|
+
* @param assertion A function that is given the result of the selector function for normal jasmine assertions.
|
|
6
|
+
*/
|
|
7
|
+
export declare function Expect<T>(selector: () => T, assertion: (m: jasmine.Matchers<T>) => void, interval?: number, getTimeFunc?: () => number): Promise<void>;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Expect = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Performs an asynchronous assertion, allowing up to one second to pass before failing
|
|
6
|
+
* @param selector A function that returns the subject of the assertion once it is expected to pass.
|
|
7
|
+
* @param assertion A function that is given the result of the selector function for normal jasmine assertions.
|
|
8
|
+
*/
|
|
9
|
+
function Expect(selector, assertion, interval, getTimeFunc) {
|
|
10
|
+
if (interval === void 0) { interval = 0; }
|
|
11
|
+
if (getTimeFunc === void 0) { getTimeFunc = function () { return Date.now(); }; }
|
|
12
|
+
return new Promise(function (resolve) {
|
|
13
|
+
var startTime = getTimeFunc();
|
|
14
|
+
var timedOut = function () { return getTimeFunc() - startTime > 1000; };
|
|
15
|
+
var execute = function () {
|
|
16
|
+
var selection = selector();
|
|
17
|
+
if (selection || timedOut()) {
|
|
18
|
+
assertion(expect(selection));
|
|
19
|
+
resolve();
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
setTimeout(function () { return execute(); }, interval);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
execute();
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
exports.Expect = Expect;
|
|
29
|
+
//# sourceMappingURL=Expect.js.map
|
package/Utility/TestHelpers.d.ts
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
|
+
/// <reference types="jasmine" />
|
|
2
|
+
/**
|
|
3
|
+
* @deprecated This class will be removed in v2 - Import its functions directly as of 1.3.0
|
|
4
|
+
*/
|
|
1
5
|
export declare class TestHelpers {
|
|
6
|
+
/**
|
|
7
|
+
* Emit an event at a given element
|
|
8
|
+
* @param element
|
|
9
|
+
* @param eventType
|
|
10
|
+
*/
|
|
2
11
|
static EmitEventAtElement(element: HTMLElement, eventType: string): void;
|
|
12
|
+
/**
|
|
13
|
+
* Emit a key event at a given element
|
|
14
|
+
* @param element
|
|
15
|
+
* @param key
|
|
16
|
+
* @param keyEvent
|
|
17
|
+
*/
|
|
3
18
|
static EmitKeyEventAtElement(element: HTMLInputElement, key: string, keyEvent: 'keydown' | 'keypress' | 'keyup' | 'input'): void;
|
|
19
|
+
/**
|
|
20
|
+
* Wait up to 1 second for a given condition to be true
|
|
21
|
+
* @deprecated Will be removed in version 2 - Use "Expect" instead
|
|
22
|
+
* @param condition
|
|
23
|
+
* @param interval
|
|
24
|
+
* @returns true if condition is met before 1 second limit, false otherwise
|
|
25
|
+
*/
|
|
4
26
|
static TimeLapsedCondition(condition: () => boolean, interval?: number): Promise<boolean>;
|
|
27
|
+
/**
|
|
28
|
+
* Performs an asynchronous assertion, allowing up to one second to pass before failing
|
|
29
|
+
* @param selector A function that returns the subject of the assertion once it is expected to pass.
|
|
30
|
+
* @param assertion A function that is given the result of the selector function for normal jasmine assertions.
|
|
31
|
+
*/
|
|
32
|
+
static Expect<T>(selector: () => T, assertion: (m: jasmine.Matchers<T>) => void, interval?: number, getTimeFunc?: () => number): Promise<void>;
|
|
5
33
|
}
|
package/Utility/TestHelpers.js
CHANGED
|
@@ -2,21 +2,39 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.TestHelpers = void 0;
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
|
+
var EmitEventAtElement_1 = require("./EmitEventAtElement");
|
|
6
|
+
var EmitKeyEventAtElement_1 = require("./EmitKeyEventAtElement");
|
|
7
|
+
var Expect_1 = require("./Expect");
|
|
8
|
+
/**
|
|
9
|
+
* @deprecated This class will be removed in v2 - Import its functions directly as of 1.3.0
|
|
10
|
+
*/
|
|
5
11
|
var TestHelpers = /** @class */ (function () {
|
|
6
12
|
function TestHelpers() {
|
|
7
13
|
}
|
|
14
|
+
/**
|
|
15
|
+
* Emit an event at a given element
|
|
16
|
+
* @param element
|
|
17
|
+
* @param eventType
|
|
18
|
+
*/
|
|
8
19
|
TestHelpers.EmitEventAtElement = function (element, eventType) {
|
|
9
|
-
|
|
10
|
-
event.initEvent(eventType);
|
|
11
|
-
element.dispatchEvent(event);
|
|
20
|
+
(0, EmitEventAtElement_1.EmitEventAtElement)(element, eventType);
|
|
12
21
|
};
|
|
22
|
+
/**
|
|
23
|
+
* Emit a key event at a given element
|
|
24
|
+
* @param element
|
|
25
|
+
* @param key
|
|
26
|
+
* @param keyEvent
|
|
27
|
+
*/
|
|
13
28
|
TestHelpers.EmitKeyEventAtElement = function (element, key, keyEvent) {
|
|
14
|
-
|
|
15
|
-
event['keyCode'] = key;
|
|
16
|
-
event['key'] = key;
|
|
17
|
-
event.initEvent(keyEvent);
|
|
18
|
-
element.dispatchEvent(event);
|
|
29
|
+
(0, EmitKeyEventAtElement_1.EmitKeyEventAtElement)(element, key, keyEvent);
|
|
19
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* Wait up to 1 second for a given condition to be true
|
|
33
|
+
* @deprecated Will be removed in version 2 - Use "Expect" instead
|
|
34
|
+
* @param condition
|
|
35
|
+
* @param interval
|
|
36
|
+
* @returns true if condition is met before 1 second limit, false otherwise
|
|
37
|
+
*/
|
|
20
38
|
TestHelpers.TimeLapsedCondition = function (condition, interval) {
|
|
21
39
|
if (interval === void 0) { interval = 10; }
|
|
22
40
|
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
@@ -45,6 +63,20 @@ var TestHelpers = /** @class */ (function () {
|
|
|
45
63
|
});
|
|
46
64
|
});
|
|
47
65
|
};
|
|
66
|
+
/**
|
|
67
|
+
* Performs an asynchronous assertion, allowing up to one second to pass before failing
|
|
68
|
+
* @param selector A function that returns the subject of the assertion once it is expected to pass.
|
|
69
|
+
* @param assertion A function that is given the result of the selector function for normal jasmine assertions.
|
|
70
|
+
*/
|
|
71
|
+
TestHelpers.Expect = function (selector, assertion, interval, getTimeFunc) {
|
|
72
|
+
if (interval === void 0) { interval = 0; }
|
|
73
|
+
if (getTimeFunc === void 0) { getTimeFunc = function () { return Date.now(); }; }
|
|
74
|
+
return (0, tslib_1.__awaiter)(this, void 0, void 0, function () {
|
|
75
|
+
return (0, tslib_1.__generator)(this, function (_a) {
|
|
76
|
+
return [2 /*return*/, (0, Expect_1.Expect)(selector, assertion, interval, getTimeFunc)];
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
};
|
|
48
80
|
return TestHelpers;
|
|
49
81
|
}());
|
|
50
82
|
exports.TestHelpers = TestHelpers;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var tslib_1 = require("tslib");
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./EmitEventAtElement"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./EmitKeyEventAtElement"), exports);
|
|
6
|
+
(0, tslib_1.__exportStar)(require("./Expect"), exports);
|
|
7
|
+
(0, tslib_1.__exportStar)(require("./TestHelpers"), exports);
|
|
8
|
+
//# sourceMappingURL=module.js.map
|
package/package.json
CHANGED
package/public_api.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export * from './Mock/
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './Utility/TestHelpers';
|
|
1
|
+
export * from './Mock/module';
|
|
2
|
+
export * from './Utility/module';
|
package/public_api.js
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Public API Surface of tsmockit
|
|
4
|
-
*/
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
var tslib_1 = require("tslib");
|
|
7
|
-
(0, tslib_1.__exportStar)(require("./Mock/
|
|
8
|
-
(0, tslib_1.__exportStar)(require("./
|
|
9
|
-
(0, tslib_1.__exportStar)(require("./Utility/TestHelpers"), exports);
|
|
4
|
+
(0, tslib_1.__exportStar)(require("./Mock/module"), exports);
|
|
5
|
+
(0, tslib_1.__exportStar)(require("./Utility/module"), exports);
|
|
10
6
|
//# sourceMappingURL=public_api.js.map
|