react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / utils / __tests__ / PooledClass-test.js
81155 views/**1* Copyright 2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @emails react-core9*/1011"use strict";1213var PooledClass;14var PoolableClass;1516describe('Pooled class', function() {17beforeEach(function() {18PooledClass = require('PooledClass');19PoolableClass = function() {};20PooledClass.addPoolingTo(PoolableClass);21});2223it('should initialize a pool correctly', function() {24expect(PoolableClass.instancePool).toBeDefined();25});2627it('should return a new instance when the pool is empty', function() {28var instance = PoolableClass.getPooled();29expect(instance instanceof PoolableClass).toBe(true);30});3132it('should return the instance back into the pool when it gets released',33function() {34var instance = PoolableClass.getPooled();35PoolableClass.release(instance);36expect(PoolableClass.instancePool.length).toBe(1);37expect(PoolableClass.instancePool[0]).toBe(instance);38}39);4041it('should return an old instance if available in the pool', function() {42var instance = PoolableClass.getPooled();43PoolableClass.release(instance);44var instance2 = PoolableClass.getPooled();45expect(instance).toBe(instance2);46});4748it('should call the destructor when instance gets released', function() {49var log = [];50var PoolableClassWithDestructor = function() {};51PoolableClassWithDestructor.prototype.destructor = function() {52log.push('released');53};54PooledClass.addPoolingTo(PoolableClassWithDestructor);55var instance = PoolableClassWithDestructor.getPooled();56PoolableClassWithDestructor.release(instance);57expect(log).toEqual(['released']);58});5960it('should accept poolers with different arguments', function() {61var log = [];62var PoolableClassWithMultiArguments = function(a, b) {63log.push(a, b);64};65PooledClass.addPoolingTo(66PoolableClassWithMultiArguments,67PooledClass.twoArgumentPooler68);69PoolableClassWithMultiArguments.getPooled('a', 'b', 'c');70expect(log).toEqual(['a', 'b']);71});7273it('should throw when the class releases an instance of a different type',74function() {75var RandomClass = function() {};76PooledClass.addPoolingTo(RandomClass);77var randomInstance = RandomClass.getPooled();78PoolableClass.getPooled();79expect(function() {80PoolableClass.release(randomInstance);81}).toThrow(82'Invariant Violation: Trying to release an instance into a pool ' +83'of a different type.'84);85}86);87});888990