-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubscription.test.ts
150 lines (139 loc) · 3.71 KB
/
Subscription.test.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import {
assertEquals,
assertThrows,
assert,
assertThrowsAsync
} from "https://deno.land/std/testing/asserts.ts";
import {
CleanUp,
SubscriberFunction,
Subscription,
SubscriptionLike,
normalizeCleanUp
} from "../Subscription.ts";
import { assertIsCleanUp } from "../utils.ts";
Deno.test(`
Subscription:
it should throw if not given a observer
`, () => {
assertThrows(() => new Subscription(undefined!));
});
Deno.test(`
Subscription:
it should throw if not given an optional function on options
`, () => {
assertThrows(() => new Subscription({}, { subscriber: 1 as any }));
assertThrows(() => new Subscription({}, { subscriber: {} as any }));
assertThrows(() => new Subscription({}, { subscriber: null as any }));
assertThrows(() => new Subscription({}, { subscriber: true as any }));
assertThrows(() => new Subscription({}, { subscriber: [] as any }));
assertThrows(() => new Subscription({}, { errorReporter: 1 as any }));
assertThrows(() => new Subscription({}, { errorReporter: {} as any }));
assertThrows(() => new Subscription({}, { errorReporter: null as any }));
assertThrows(() => new Subscription({}, { errorReporter: true as any }));
assertThrows(() => new Subscription({}, { errorReporter: [] as any }));
assert(
new Subscription(
{},
{ errorReporter: undefined, subscriber: undefined },
) instanceof Subscription,
);
});
Deno.test(`
Subscription:
it should invoke observer start method in constructor
`, () => {
let started = false;
new Subscription({
start: () => started = true,
}, undefined);
assertEquals(started, true);
});
Deno.test(`
Subscription:
it should re-throw errors from start method into the host
`, () => {
let error: unknown;
new Subscription({
start: () => {
throw new Error("this should go to host");
},
}, { errorReporter: (e) => error = e });
assert(error instanceof Error);
});
Deno.test(`
Subscription:
it should run subscriber on constructor
`, () => {
let called = false;
new Subscription({}, { subscriber: () => {
called = true;
} });
assertEquals(called, true);
});
Deno.test(`
Subscription:
it should run observer error on subscriber errors
`, () => {
let error: unknown;
new Subscription({
error: (e) => error = e,
}, { subscriber: () => {
throw new Error("this should go to observer error");
} });
assert(error instanceof Error);
});
Deno.test(`
Subscription:
it should be closed if subscriber completes
`, () => {
assertEquals(new Subscription({}, { subscriber: (o) => {
o.complete();
} }).closed, true);
});
Deno.test(`
Subscription:
it should call cleanUp function on completion
`, () => {
let called = false;
assertEquals(new Subscription({}, { subscriber: (o) => {
o.complete();
return () => called = true;
} }).closed, true);
assertEquals(called, true);
});
Deno.test(`
Subscription.unsubscribe:
it should call cleanup ONCE and close the subscription
`, () => {
let calls = 0;
const subscription = new Subscription(
{},
{ subscriber: (o) => () => ++calls },
);
assertEquals(subscription.closed, false);
subscription.unsubscribe();
subscription.unsubscribe();
assertEquals(calls, 1);
assertEquals(subscription.closed, true);
});
Deno.test(`
static Subscription.getObserver:
it should return observer provided to the subscription
`, () => {
const observer = {};
assertEquals(observer, Subscription.getObserver(new Subscription(observer)));
});
Deno.test(`
static Subscription.getErrorReporter:
it should return errorReporter provided to the subscription
`, () => {
const observer = {};
const errorReporter = () => {};
assertEquals(
errorReporter,
Subscription.getErrorReporter(
new Subscription(observer, { errorReporter }),
),
);
});