-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
71 lines (70 loc) · 1.92 KB
/
index.test.js
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
// @flow
import Callable from "./";
describe("Callable", () => {
it("is a Callable", () => {
const fn = new Callable(x => x);
expect(fn instanceof Callable).toEqual(true);
});
it("is a Function", () => {
const fn = new Callable(x => x);
expect(fn instanceof Function).toEqual(true);
});
it("can be called", () => {
const fn = new Callable(x => x + 1);
expect(fn(0)).toEqual(1);
});
describe("when extending", () => {
class Foo<A, B> extends Callable<A, B> {
tag: string;
constructor(tag: string, fn: A => B) {
super(fn);
this.tag = tag;
}
getTag() {
return this.tag;
}
}
it("can be called", () => {
const fn = new Foo("incr", x => x + 1);
expect(fn(0)).toEqual(1);
});
it("has additional fields", () => {
const fn = new Foo("incr", x => x + 1);
expect(fn.tag).toEqual("incr");
});
it("has additional methods", () => {
const fn = new Foo("incr", x => x + 1);
expect(fn.getTag()).toEqual("incr");
});
it("has a valid prototype chain", () => {
const fn = new Foo("incr", x => x + 1);
(fn: number => number);
(fn: Callable<number, number>);
(fn: Foo<number, number>);
expect(fn instanceof Foo).toEqual(true);
expect(fn instanceof Callable).toEqual(true);
expect(fn instanceof Function).toEqual(true);
});
it("is able to hijack the incoming function", () => {
class Spy<A, B> extends Callable<A, B> {
spy: {
callCount: number
};
constructor(fn: A => B) {
const spy = {
callCount: 0
};
super((x: A): B => {
spy.callCount += 1;
return fn(x);
});
this.spy = spy;
}
}
const fn = new Spy(x => x + 1);
expect(fn.spy.callCount).toEqual(0);
fn(0);
expect(fn.spy.callCount).toEqual(1);
});
});
});