Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81155 views
1
/**
2
* Copyright 2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @emails react-core
10
*/
11
12
"use strict";
13
14
var PooledClass;
15
var PoolableClass;
16
17
describe('Pooled class', function() {
18
beforeEach(function() {
19
PooledClass = require('PooledClass');
20
PoolableClass = function() {};
21
PooledClass.addPoolingTo(PoolableClass);
22
});
23
24
it('should initialize a pool correctly', function() {
25
expect(PoolableClass.instancePool).toBeDefined();
26
});
27
28
it('should return a new instance when the pool is empty', function() {
29
var instance = PoolableClass.getPooled();
30
expect(instance instanceof PoolableClass).toBe(true);
31
});
32
33
it('should return the instance back into the pool when it gets released',
34
function() {
35
var instance = PoolableClass.getPooled();
36
PoolableClass.release(instance);
37
expect(PoolableClass.instancePool.length).toBe(1);
38
expect(PoolableClass.instancePool[0]).toBe(instance);
39
}
40
);
41
42
it('should return an old instance if available in the pool', function() {
43
var instance = PoolableClass.getPooled();
44
PoolableClass.release(instance);
45
var instance2 = PoolableClass.getPooled();
46
expect(instance).toBe(instance2);
47
});
48
49
it('should call the destructor when instance gets released', function() {
50
var log = [];
51
var PoolableClassWithDestructor = function() {};
52
PoolableClassWithDestructor.prototype.destructor = function() {
53
log.push('released');
54
};
55
PooledClass.addPoolingTo(PoolableClassWithDestructor);
56
var instance = PoolableClassWithDestructor.getPooled();
57
PoolableClassWithDestructor.release(instance);
58
expect(log).toEqual(['released']);
59
});
60
61
it('should accept poolers with different arguments', function() {
62
var log = [];
63
var PoolableClassWithMultiArguments = function(a, b) {
64
log.push(a, b);
65
};
66
PooledClass.addPoolingTo(
67
PoolableClassWithMultiArguments,
68
PooledClass.twoArgumentPooler
69
);
70
PoolableClassWithMultiArguments.getPooled('a', 'b', 'c');
71
expect(log).toEqual(['a', 'b']);
72
});
73
74
it('should throw when the class releases an instance of a different type',
75
function() {
76
var RandomClass = function() {};
77
PooledClass.addPoolingTo(RandomClass);
78
var randomInstance = RandomClass.getPooled();
79
PoolableClass.getPooled();
80
expect(function() {
81
PoolableClass.release(randomInstance);
82
}).toThrow(
83
'Invariant Violation: Trying to release an instance into a pool ' +
84
'of a different type.'
85
);
86
}
87
);
88
});
89
90