forked from ReactiveX/rxjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fromEvent-spec.ts
110 lines (88 loc) · 4.72 KB
/
fromEvent-spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { fromEvent } from 'rxjs';
import {
HasEventTargetAddRemove,
NodeStyleEventEmitter,
NodeCompatibleEventEmitter,
JQueryStyleEventEmitter
} from '../../src/internal/observable/fromEvent';
import { B } from '../helpers';
declare const eventTargetSource: EventTarget;
it('should support an event target source', () => {
const source: HasEventTargetAddRemove<Event> = eventTargetSource;
const a = fromEvent(eventTargetSource, "click"); // $ExpectType Observable<Event>
});
it('should support an event target source result selector', () => {
const a = fromEvent(eventTargetSource, "click", () => "clunk"); // $ExpectType Observable<string>
});
it('should support an event target source with options', () => {
const a = fromEvent(eventTargetSource, "click", { once: true }); // $ExpectType Observable<Event>
});
it('should support an event target source with options and result selector', () => {
const a = fromEvent(eventTargetSource, "click", { once: true }, () => "clunk"); // $ExpectType Observable<string>
});
declare const documentSource: HTMLDocument;
it('should support a document source', () => {
const source: HasEventTargetAddRemove<Event> = documentSource;
const a = fromEvent(documentSource, "click"); // $ExpectType Observable<Event>
});
it('should support a document source result selector', () => {
const a = fromEvent(documentSource, "click", () => "clunk"); // $ExpectType Observable<string>
});
it('should support a document source with options', () => {
const a = fromEvent(documentSource, "click", { once: true }); // $ExpectType Observable<Event>
});
it('should support a document source with options and result selector', () => {
const a = fromEvent(documentSource, "click", { once: true }, () => "clunk"); // $ExpectType Observable<string>
});
// Pick the parts that will match NodeStyleEventEmitter. If this isn't done, it
// will match JQueryStyleEventEmitter - because of the `on` and `off` methods -
// despite the latter being declared last in the EventTargetLike union.
declare const nodeStyleSource: Pick<typeof process, 'addListener' | 'removeListener'>;
it('should support a node-style source', () => {
const source: NodeStyleEventEmitter = nodeStyleSource;
const a = fromEvent(nodeStyleSource, "exit"); // $ExpectType Observable<unknown>
const b = fromEvent<B>(nodeStyleSource, "exit"); // $ExpectType Observable<B>
});
it('should support a node-style source and symbol eventName', () => {
const SYMBOL_EVENT = Symbol();
const source: NodeStyleEventEmitter = nodeStyleSource;
const a = fromEvent(nodeStyleSource, SYMBOL_EVENT); // $ExpectType Observable<unknown>
const b = fromEvent<B>(nodeStyleSource, SYMBOL_EVENT); // $ExpectType Observable<B>
});
it('should deprecate explicit type parameters for a node-style source', () => {
const source: NodeStyleEventEmitter = nodeStyleSource;
const a = fromEvent(nodeStyleSource, "exit"); // $ExpectNoDeprecation
const b = fromEvent<B>(nodeStyleSource, "exit"); // $ExpectDeprecation
});
it('should support a node-style source result selector', () => {
const a = fromEvent(nodeStyleSource, "exit", () => "bye"); // $ExpectType Observable<string>
});
const nodeCompatibleSource = {
addListener(eventName: "something", handler: () => void) {},
removeListener(eventName: "something", handler: () => void) {}
};
it('should support a node-compatible source', () => {
const source: NodeCompatibleEventEmitter = nodeCompatibleSource;
const a = fromEvent(nodeCompatibleSource, "something"); // $ExpectType Observable<unknown>
const b = fromEvent<B>(nodeCompatibleSource, "something"); // $ExpectType Observable<B>
});
it('should deprecate explicit type parameters for a node-compatible source', () => {
const source: NodeCompatibleEventEmitter = nodeCompatibleSource;
const a = fromEvent(nodeCompatibleSource, "something"); // $ExpectNoDeprecation
const b = fromEvent<B>(nodeCompatibleSource, "something"); // $ExpectDeprecation
});
it('should support a node-compatible source result selector', () => {
const a = fromEvent(nodeCompatibleSource, "something", () => "something else"); // $ExpectType Observable<string>
});
const jQueryStyleSource = {
on(eventName: "something", handler: (this: any, b: B) => any) {},
off(eventName: "something", handler: (this: any, b: B) => any) {}
};
it('should support a jQuery-style source', () => {
const source: JQueryStyleEventEmitter<any, any> = jQueryStyleSource;
const a = fromEvent(jQueryStyleSource, "something"); // $ExpectType Observable<B>
const b = fromEvent<B>(jQueryStyleSource, "something"); // $ExpectType Observable<B>
});
it('should support a jQuery-style source result selector', () => {
const a = fromEvent(jQueryStyleSource, "something", () => "something else"); // $ExpectType Observable<string>
});