-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (101 loc) · 3.31 KB
/
index.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
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
import test from "ava";
import React from "react";
import ComponentMap from "../src";
import { tenmegabyte, allocatedMemory } from "./_helpers";
test("is a function", t => {
t.is(typeof ComponentMap, "function");
});
const Component = React.createClass({
displayName: "Component",
render: function() {
return null;
}
});
const InvalidBucketName = { displayName: undefined };
function testImplementation(test) {
test("#get returns undefined by default", t => {
const map = new ComponentMap({ WeakMap });
t.is(map.get(Component), undefined);
});
test("#has returns false by default", t => {
const map = new ComponentMap({ WeakMap });
t.is(map.has(Component), false);
});
test("#get returns undefined if bucket name is invalid", t => {
const map = new ComponentMap({ WeakMap });
t.is(map.get(InvalidBucketName), undefined);
});
test("#jas returns undefined if bucket name is invalid", t => {
const map = new ComponentMap({ WeakMap });
t.is(map.has(InvalidBucketName), false);
});
test("#set and #get allow for saving and retrieving values", t => {
const map = new ComponentMap({ WeakMap });
map.set(Component, "bar");
t.is(map.get(Component), "bar");
});
test("#set and #has allow for saving and knowing whether value exists", t => {
const map = new ComponentMap({ WeakMap });
map.set(Component, "bar");
t.is(map.has(Component), true);
});
test("#delete allows for deleting values", t => {
const map = new ComponentMap({ WeakMap });
map.set(Component, "bar");
map.delete(Component);
t.is(map.get(Component), undefined);
});
if (!global.gc) {
test.todo("It is recommended to run test suite with --expose-gc flag!");
} else {
test("releases memory when scope that created ComponentMap exits", t => {
return allocatedMemory(function() {
const map = new ComponentMap({ WeakMap });
map.set(Component, tenmegabyte());
}).then(heapUsed => {
t.is(heapUsed <= 0, true);
});
});
}
test("keeps memory for keys outside scope", t => {
const map = new ComponentMap({ WeakMap });
return allocatedMemory(function() {
map.set(Component, tenmegabyte());
}).then(heapUsed => {
t.is(heapUsed <= 10, true);
});
});
}
[
{ WeakMap: undefined, note: "(shim)" },
{ WeakMap: WeakMap, note: "(native)" }
].forEach(({ WeakMap, note }) => {
testImplementation((title, fn) => test(`${title} ${note}`, fn));
});
if (WeakMap && global.gc) {
test("releases memory for expired keys (only native)", t => {
const map = new ComponentMap({ WeakMap });
return allocatedMemory(function() {
const Bar = React.createClass({ render: function() {} });
map.set(Bar, tenmegabyte());
}).then(heapUsed => {
t.is(heapUsed, 0);
});
});
test("releases memory for deleted keys (only native)", t => {
const map = new ComponentMap({ WeakMap });
let Bar = React.createClass({ render: function() {} });
return allocatedMemory(function() {
map.set(Bar, tenmegabyte());
Bar = null;
}).then(heapUsed => {
t.is(heapUsed, 0);
});
});
}
test("allows for custom getBucketname", t => {
const object = { lol: "foo" };
const map = new ComponentMap({ getBucketName: key => key.lol });
map.set(object, "bar");
t.is(map.get(object), "bar");
});