tsmockit 1.1.3 → 1.2.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/README.md CHANGED
@@ -48,7 +48,12 @@ static EmitKeyEventAtElement(
48
48
  ): void
49
49
  ```
50
50
  ```typescript
51
- static async TimeLapsedCondition(condition: () => boolean, interval = 10): Promise<boolean>
51
+ static async Expect<T>(
52
+ selector: () => T,
53
+ assertion: (m: jasmine.Matchers<T>) => void,
54
+ interval = 0,
55
+ getTimeFunc = () => Date.now()
56
+ ): Promise<void>
52
57
  ```
53
58
 
54
59
  ## Usage
@@ -1,5 +1,30 @@
1
+ /// <reference types="jasmine" />
1
2
  export declare class TestHelpers {
3
+ /**
4
+ * Emit an event at a given element
5
+ * @param element
6
+ * @param eventType
7
+ */
2
8
  static EmitEventAtElement(element: HTMLElement, eventType: string): void;
9
+ /**
10
+ * Emit a key event at a given element
11
+ * @param element
12
+ * @param key
13
+ * @param keyEvent
14
+ */
3
15
  static EmitKeyEventAtElement(element: HTMLInputElement, key: string, keyEvent: 'keydown' | 'keypress' | 'keyup' | 'input'): void;
16
+ /**
17
+ * Wait up to 1 second for a given condition to be true
18
+ * @deprecated Will be removed in version 2 - Use "Expect" instead
19
+ * @param condition
20
+ * @param interval
21
+ * @returns true if condition is met before 1 second limit, false otherwise
22
+ */
4
23
  static TimeLapsedCondition(condition: () => boolean, interval?: number): Promise<boolean>;
24
+ /**
25
+ * Performs an asynchronous assertion, allowing up to one second to pass before failing
26
+ * @param selector A function that returns the subject of the assertion once it is expected to pass.
27
+ * @param assertion A function that is given the result of the selector function for normal jasmine assertions.
28
+ */
29
+ static Expect<T>(selector: () => T, assertion: (m: jasmine.Matchers<T>) => void, interval?: number, getTimeFunc?: () => number): Promise<void>;
5
30
  }
@@ -5,11 +5,22 @@ var tslib_1 = require("tslib");
5
5
  var TestHelpers = /** @class */ (function () {
6
6
  function TestHelpers() {
7
7
  }
8
+ /**
9
+ * Emit an event at a given element
10
+ * @param element
11
+ * @param eventType
12
+ */
8
13
  TestHelpers.EmitEventAtElement = function (element, eventType) {
9
14
  var event = document.createEvent('Event');
10
15
  event.initEvent(eventType);
11
16
  element.dispatchEvent(event);
12
17
  };
18
+ /**
19
+ * Emit a key event at a given element
20
+ * @param element
21
+ * @param key
22
+ * @param keyEvent
23
+ */
13
24
  TestHelpers.EmitKeyEventAtElement = function (element, key, keyEvent) {
14
25
  var event = document.createEvent('Event');
15
26
  event['keyCode'] = key;
@@ -17,6 +28,13 @@ var TestHelpers = /** @class */ (function () {
17
28
  event.initEvent(keyEvent);
18
29
  element.dispatchEvent(event);
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,34 @@ 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*/, new Promise(function (resolve) {
77
+ var startTime = getTimeFunc();
78
+ var timedOut = function () { return getTimeFunc() - startTime > 1000; };
79
+ var execute = function () {
80
+ var selection = selector();
81
+ if (selection || timedOut()) {
82
+ assertion(expect(selection));
83
+ resolve();
84
+ }
85
+ else {
86
+ setTimeout(function () { return execute(); }, interval);
87
+ }
88
+ };
89
+ execute();
90
+ })];
91
+ });
92
+ });
93
+ };
48
94
  return TestHelpers;
49
95
  }());
50
96
  exports.TestHelpers = TestHelpers;
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "dependency injection"
15
15
  ],
16
16
  "license": "MIT",
17
- "version": "1.1.3",
17
+ "version": "1.2.0",
18
18
  "description": "Generic mocking library for TypeScript",
19
19
  "private": false,
20
20
  "main": "./public_api.js",