Skip to content

Commit c2ef446

Browse files
committed
angular-mocks: Update types and docs for 1.7
Related to angular/angular.js#16603 and angular/angular.js#16640.
1 parent f3760c6 commit c2ef446

File tree

2 files changed

+125
-7
lines changed

2 files changed

+125
-7
lines changed

types/angular-mocks/angular-mocks-tests.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ timeoutService.verifyNoPendingTasks();
8787
let intervalService: ng.IIntervalService;
8888
let intervalServiceTimeActuallyAdvanced: number;
8989

90-
intervalServiceTimeActuallyAdvanced = intervalService.flush();
9190
intervalServiceTimeActuallyAdvanced = intervalService.flush(1234);
9291

9392
///////////////////////////////////////
@@ -1516,6 +1515,23 @@ requestHandler.respond(404, { key: 'value' });
15161515
requestHandler.respond(404, { key: 'value' }, { header: 'value' });
15171516
requestHandler.respond(404, { key: 'value' }, { header: 'value' }, 'responseText');
15181517

1518+
///////////////////////////////////////
1519+
// IFlushPendingTasksService
1520+
///////////////////////////////////////
1521+
let $flushPendingTasks: ng.IFlushPendingTasksService;
1522+
$flushPendingTasks();
1523+
$flushPendingTasks(42);
1524+
1525+
///////////////////////////////////////
1526+
// IVerifyNoPendingTasksService
1527+
///////////////////////////////////////
1528+
let $verifyNoPendingTasks: ng.IVerifyNoPendingTasksService;
1529+
$verifyNoPendingTasks();
1530+
$verifyNoPendingTasks('task type');
1531+
1532+
///////////////////////////////////////
1533+
// browserTrigger
1534+
///////////////////////////////////////
15191535
browserTrigger(document.body, 'click');
15201536
browserTrigger(angular.element(document.body), 'click');
15211537
browserTrigger(angular.element(document.body), 'click', { which: 1, keys: ['ctrl'] });

types/angular-mocks/index.d.ts

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Type definitions for Angular JS (ngMock, ngMockE2E module) 1.6
1+
// Type definitions for Angular JS (ngMock, ngMockE2E module) 1.7
22
// Project: http://angularjs.org
33
// Definitions by: Diego Vilar <https://github.com/diegovilar>, Tony Curtis <https://github.com/daltin>
44
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
@@ -59,7 +59,38 @@ declare module 'angular' {
5959
// Augments the original service
6060
///////////////////////////////////////////////////////////////////////////
6161
interface ITimeoutService {
62+
/**
63+
* **Deprecated** since version 1.7.3. (Use `$flushPendingTasks` instead.)
64+
*
65+
* ---
66+
* Flushes the queue of pending tasks.
67+
*
68+
* _This method is essentially an alias of `$flushPendingTasks`._
69+
*
70+
* > For historical reasons, this method will also flush non-`$timeout` pending tasks, such as
71+
* > `$q` promises and tasks scheduled via `$applyAsync` and `$evalAsync`.
72+
*
73+
* @param delay - The maximum timeout amount to flush up until.
74+
*/
6275
flush(delay?: number): void;
76+
77+
/**
78+
* **Deprecated** since version 1.7.3. (Use `$verifyNoPendingTasks` instead.)
79+
*
80+
* ---
81+
* Verifies that there are no pending tasks that need to be flushed. It throws an error if there
82+
* are still pending tasks.
83+
*
84+
* _This method is essentially an alias of `$verifyNoPendingTasks` (called with no arguments)._
85+
*
86+
* > For historical reasons, this method will also verify non-`$timeout` pending tasks, such as
87+
* > pending `$http` requests, in-progress `$route` transitions, unresolved `$q` promises and
88+
* > tasks scheduled via `$applyAsync` and `$evalAsync`.
89+
* >
90+
* > It is recommended to use `$verifyNoPendingTasks` instead, which additionally supports
91+
* > verifying a specific type of tasks. For example, you can verify there are no pending
92+
* > timeouts with `$verifyNoPendingTasks('$timeout')`.
93+
*/
6394
verifyNoPendingTasks(): void;
6495
}
6596

@@ -69,7 +100,13 @@ declare module 'angular' {
69100
// Augments the original service
70101
///////////////////////////////////////////////////////////////////////////
71102
interface IIntervalService {
72-
flush(millis?: number): number;
103+
/**
104+
* Runs interval tasks scheduled to be run in the next `millis` milliseconds.
105+
*
106+
* @param millis - The maximum timeout amount to flush up until.
107+
* @return The amount of time moved forward.
108+
*/
109+
flush(millis: number): number;
73110
}
74111

75112
///////////////////////////////////////////////////////////////////////////
@@ -413,6 +450,65 @@ declare module 'angular' {
413450
whenRoute(method: string, url: string): mock.IRequestHandler;
414451
}
415452

453+
///////////////////////////////////////////////////////////////////////////
454+
// FlushPendingTasksService
455+
// see https://docs.angularjs.org/api/ngMock/service/$flushPendingTasks
456+
///////////////////////////////////////////////////////////////////////////
457+
interface IFlushPendingTasksService {
458+
/**
459+
* Flushes all currently pending tasks and executes the corresponding callbacks.
460+
*
461+
* Optionally, you can also pass a `delay` argument to only flush tasks that are scheduled to be
462+
* executed within `delay` milliseconds. Currently, `delay` only applies to timeouts, since all
463+
* other tasks have a delay of 0 (i.e. they are scheduled to be executed as soon as possible, but
464+
* still asynchronously).
465+
*
466+
* If no delay is specified, it uses a delay such that all currently pending tasks are flushed.
467+
*
468+
* The types of tasks that are flushed include:
469+
*
470+
* - Pending timeouts (via `$timeout`).
471+
* - Pending tasks scheduled via `$applyAsync`.
472+
* - Pending tasks scheduled via `$evalAsync`.
473+
* These include tasks scheduled via `$evalAsync()` indirectly (such as `$q` promises).
474+
*
475+
* > Periodic tasks scheduled via `$interval` use a different queue and are not flushed by
476+
* > `$flushPendingTasks()`. Use `$interval.flush(millis)` instead.
477+
*
478+
* @param millis - The number of milliseconds to flush.
479+
*/
480+
(delay?: number): void;
481+
}
482+
483+
///////////////////////////////////////////////////////////////////////////
484+
// VerifyNoPendingTasksService
485+
// see https://docs.angularjs.org/api/ngMock/service/$verifyNoPendingTasks
486+
///////////////////////////////////////////////////////////////////////////
487+
interface IVerifyNoPendingTasksService {
488+
/**
489+
* Verifies that there are no pending tasks that need to be flushed. It throws an error if there
490+
* are still pending tasks.
491+
*
492+
* You can check for a specific type of tasks only, by specifying a `taskType`.
493+
*
494+
* Available task types:
495+
*
496+
* - `$timeout`: Pending timeouts (via `$timeout`).
497+
* - `$http`: Pending HTTP requests (via `$http`).
498+
* - `$route`: In-progress route transitions (via `$route`).
499+
* - `$applyAsync`: Pending tasks scheduled via `$applyAsync`.
500+
* - `$evalAsync`: Pending tasks scheduled via `$evalAsync`.
501+
* These include tasks scheduled via `$evalAsync()` indirectly (such as `$q` promises).
502+
*
503+
* > Periodic tasks scheduled via `$interval` use a different queue and are not taken into
504+
* > account by `$verifyNoPendingTasks()`. There is currently no way to verify that there are no
505+
* > pending `$interval` tasks.
506+
*
507+
* @param taskType - The type of tasks to check for.
508+
*/
509+
(taskType?: string): void;
510+
}
511+
416512
///////////////////////////////////////////////////////////////////////////
417513
// AnimateService
418514
// see https://docs.angularjs.org/api/ngMock/service/$animate
@@ -513,6 +609,11 @@ declare module 'angular' {
513609
* for keyboard events (keydown, keypress, and keyup).
514610
*/
515611
charcode?: number;
612+
/**
613+
* [data](https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent/data) for
614+
* [CompositionEvents](https://developer.mozilla.org/en-US/docs/Web/API/CompositionEvent).
615+
*/
616+
data?: string;
516617
/**
517618
* The elapsedTime for
518619
* [TransitionEvent](https://developer.mozilla.org/docs/Web/API/TransitionEvent)
@@ -566,10 +667,11 @@ declare global {
566667
* This is a global (window) function that is only available when the `ngMock` module is included.
567668
* It can be used to trigger a native browser event on an element, which is useful for unit testing.
568669
*
569-
* @param element Either a wrapped jQuery/jqLite node or a DOM element
570-
* @param eventType Optional event type. If none is specified, the function tries to determine
571-
* the right event type for the element, e.g. `change` for `input[text]`.
572-
* @param eventData An optional object which contains additional event data used when creating the event.
670+
* @param element Either a wrapped jQuery/jqLite node or a DOM element.
671+
* @param eventType Optional event type. If none is specified, the function tries to determine the
672+
* right event type for the element, e.g. `change` for `input[text]`.
673+
* @param eventData An optional object which contains additional event data used when creating the
674+
* event.
573675
*/
574676
function browserTrigger(
575677
element: JQuery | Element,

0 commit comments

Comments
 (0)