react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / utils / __tests__ / OrderedMap-test.js
81155 views/**1* Copyright 2013-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 OrderedMap;1415/**16* Shared, reusable objects.17*/18var hasEmptyStringKey = {19'thisKeyIsFine': {data: []},20'': {thisShouldCauseAFailure: []},21'thisKeyIsAlsoFine': {data: []}22};2324/**25* Used as map/forEach callback.26*/27var duplicate = function(itm, key, count) {28return {29uniqueID: itm.uniqueID,30val: itm.val + key + count + this.justToTestScope31};32};3334// Should not be allowed - because then null/'null' become impossible to35// distinguish. Every key MUST be a string period!36var hasNullAndUndefStringKey = [37{uniqueID: 'undefined', val: 'thisIsUndefined'},38{uniqueID: 'null', val: 'thisIsNull'}39];40var hasNullKey = [41{uniqueID: 'thisKeyIsFine', data: []},42{uniqueID: 'thisKeyIsAlsoFine', data: []},43{uniqueID: null, data: []}44];4546var hasObjectKey = [47{uniqueID: 'thisKeyIsFine', data: []},48{uniqueID: 'thisKeyIsAlsoFine', data: []},49{uniqueID: {}, data: []}50];5152var hasArrayKey = [53{uniqueID: 'thisKeyIsFine', data: []},54{uniqueID: 'thisKeyIsAlsoFine', data: []},55{uniqueID: [], data: []}56];5758// This should be allowed59var hasNullStringKey = [60{uniqueID: 'thisKeyIsFine', data: []},61{uniqueID: 'thisKeyIsAlsoFine', data: []},62{uniqueID: 'null', data: []}63];6465var hasUndefinedKey = [66{uniqueID: 'thisKeyIsFine', data: []},67{uniqueID: 'thisKeyIsAlsoFine', data: []},68{uniqueID: undefined, data: []}69];7071var hasUndefinedStringKey = [72{uniqueID: 'thisKeyIsFine', data: []},73{uniqueID: 'thisKeyIsAlsoFine', data: []},74{uniqueID: 'undefined', data: []}75];7677var hasPositiveNumericKey = [78{uniqueID: 'notANumber', data: []},79{uniqueID: '5', data: []},80{uniqueID: 'notAnotherNumber',data: []}81];8283var hasZeroStringKey = [84{uniqueID: 'greg', data: 'grego'},85{uniqueID: '0', data: '0o'},86{uniqueID: 'tom', data: 'tomo'}87];8889var hasZeroNumberKey = [90{uniqueID: 'greg', data: 'grego'},91{uniqueID: 0, data: '0o'},92{uniqueID: 'tom', data: 'tomo'}93];9495var hasAllNumericStringKeys = [96{uniqueID: '0', name: 'Gregory'},97{uniqueID: '2', name: 'James'},98{uniqueID: '1', name: 'Tom'}99];100101var hasAllNumericKeys = [102{uniqueID: 0, name: 'Gregory'},103{uniqueID: 2, name: 'James'},104{uniqueID: 1, name: 'Tom'}105];106107var hasAllValidKeys = [108{uniqueID: 'keyOne', value: 'valueOne'},109{uniqueID: 'keyTwo', value: 'valueTwo'}110];111112var hasDuplicateKeys = [113{uniqueID: 'keyOne', value: 'valueOne'},114{uniqueID: 'keyTwo', value: 'valueTwo'},115{uniqueID: 'keyOne', value: 'valueThree'}116];117118var idEntities = [119{uniqueID: 'greg', name: 'Gregory'},120{uniqueID: 'james', name: 'James'},121{uniqueID: 'tom', name: 'Tom'}122];123124var hasEmptyKey = [125{uniqueID: 'greg', name: 'Gregory'},126{uniqueID: '', name: 'James'},127{uniqueID: 'tom', name: 'Tom'}128];129130var extractUniqueID = function(entity) {131return entity.uniqueID;132};133134describe('OrderedMap', function() {135beforeEach(function() {136require('mock-modules').dumpCache();137OrderedMap = require('OrderedMap');138});139140it('should create according to simple object with keys', function() {141OrderedMap.fromArray(hasAllValidKeys, extractUniqueID);142// Iterate over and ensure key order.143});144145it('should create from array when providing an identity CB', function() {146expect(function() {147OrderedMap.fromArray(idEntities, extractUniqueID);148}).not.toThrow();149});150151it('should throw if constructing from Array without identity CB', function() {152OrderedMap.fromArray(idEntities, extractUniqueID);153// Iterate and ensure key order154});155156it('should not throw when fromArray extracts a numeric key', function() {157expect(function() {158OrderedMap.fromArray(hasPositiveNumericKey, extractUniqueID);159}).not.toThrow();160161});162163it('should throw when any key is the empty string', function() {164expect(function() {165OrderedMap.fromArray(hasEmptyKey, extractUniqueID);166}).toThrow();167});168169it('should not throw when a key is the string "undefined" or "null"',170function() {171var om = OrderedMap.fromArray(hasNullAndUndefStringKey, extractUniqueID);172expect(om.length).toBe(2);173expect(om.indexOfKey('undefined')).toBe(0);174expect(om.indexOfKey('null')).toBe(1);175expect(om.keyAfter('undefined')).toBe('null');176expect(om.keyAfter('null')).toBe(undefined);177expect(om.keyBefore('undefined')).toBe(undefined);178expect(om.has('undefined')).toBe(true);179expect(om.has('null')).toBe(true);180expect(om.get('undefined').val).toBe('thisIsUndefined');181expect(om.get('null').val).toBe('thisIsNull');182});183184185/**186* Numeric keys are cast to strings.187*/188it('should not throw when a key is the number zero', function() {189var om = OrderedMap.fromArray(hasZeroNumberKey, extractUniqueID);190expect(om.length).toBe(3);191expect(om.indexOfKey('0')).toBe(1);192expect(om.indexOfKey(0)).toBe(1);193});194195it('should throw when any key is falsey', function() {196expect(function() {197OrderedMap.fromArray(hasEmptyStringKey, extractUniqueID);198}).toThrow();199200expect(function() {201OrderedMap.fromArray(hasNullKey, extractUniqueID);202}).toThrow();203204expect(function() {205OrderedMap.fromArray(hasUndefinedKey, extractUniqueID);206}).toThrow();207});208209it('should not throw on string keys "undefined/null"', function() {210expect(function() {211OrderedMap.fromArray(hasNullStringKey, extractUniqueID);212}).not.toThrow();213214expect(function() {215OrderedMap.fromArray(hasUndefinedStringKey, extractUniqueID);216}).not.toThrow();217});218219it('should throw on extracting keys that are not strings/nums', function() {220expect(function() {221OrderedMap.fromArray(hasObjectKey, extractUniqueID);222}).toThrow();223224expect(function() {225OrderedMap.fromArray(hasArrayKey, extractUniqueID);226}).toThrow();227});228229it('should throw if instantiating with duplicate key', function() {230expect(function() {231OrderedMap.fromArray(hasDuplicateKeys, extractUniqueID);232}).toThrow();233});234235it('should not throw when a key is the string "0"', function() {236var verifyOM = function(om) {237expect(om.length).toBe(3);238expect(om.indexOfKey('greg')).toBe(0);239expect(om.indexOfKey('0')).toBe(1);240expect(om.indexOfKey(0)).toBe(1); // Casts on writes and reads.241expect(om.indexOfKey('tom')).toBe(2);242expect(om.keyAfter('greg')).toBe('0');243expect(om.keyAfter('0')).toBe('tom');244expect(om.keyAfter(0)).toBe('tom');245expect(om.keyAfter('tom')).toBe(undefined);246expect(om.keyBefore('greg')).toBe(undefined);247expect(om.keyBefore(0)).toBe('greg');248expect(om.keyBefore('0')).toBe('greg');249expect(om.keyBefore('tom')).toBe('0');250expect(om.has('undefined')).toBe(false);251expect(om.has(0)).toBe(true);252expect(om.has('0')).toBe(true);253};254var om = OrderedMap.fromArray(hasZeroStringKey, extractUniqueID);255verifyOM(om);256om = OrderedMap.fromArray(hasZeroNumberKey, extractUniqueID);257verifyOM(om);258});259260it('should throw when getting invalid public key', function() {261var om = OrderedMap.fromArray(hasAllValidKeys, extractUniqueID);262expect(function() {263om.has(undefined);264}).toThrow();265expect(function() {266om.get(undefined);267}).toThrow();268expect(function() {269om.has(null);270}).toThrow();271expect(function() {272om.get(null);273}).toThrow();274expect(function() {275om.has('');276}).toThrow();277expect(function() {278om.get('');279}).toThrow();280});281282it('should throw when any key is falsey', function() {283expect(function() {284OrderedMap.fromArray(hasEmptyStringKey, extractUniqueID);285}).toThrow();286287expect(function() {288OrderedMap.fromArray(hasNullKey, extractUniqueID);289}).toThrow();290291expect(function() {292OrderedMap.fromArray(hasUndefinedKey, extractUniqueID);293}).toThrow();294});295296297it('should throw when fromArray is passed crazy args', function() {298// Test passing another OrderedMap (when it expects a plain object.)299// This is probably not what you meant to do! We should error.300var validOM = OrderedMap.fromArray(hasAllValidKeys, extractUniqueID);301expect(function() {302OrderedMap.fromArray({uniqueID: 'asdf'}, extractUniqueID);303}).toThrow();304expect(function() {305OrderedMap.fromArray(validOM, extractUniqueID);306}).toThrow();307});308309it('should throw when fromArray is passed crazy things', function() {310expect(function() {311OrderedMap.fromArray(null, extractUniqueID);312}).toThrow();313expect(function() {314OrderedMap.fromArray('stringgg', extractUniqueID);315}).toThrow();316expect(function() {317OrderedMap.fromArray(undefined, extractUniqueID);318}).toThrow();319expect(function() {320OrderedMap.fromArray(new Date(), extractUniqueID);321}).toThrow();322expect(function() {323OrderedMap.fromArray({}, extractUniqueID);324}).toThrow();325326// Test failure without extractor327expect(function() {328OrderedMap.fromArray(idEntities);329}).toThrow();330expect(function() {331OrderedMap.fromArray(idEntities, extractUniqueID);332}).not.toThrow();333});334335// Testing methods that accept other `OrderedMap`s.336it('should throw when from/merge is passed an non-OrderedMap.', function() {337// Test passing an array to construction.338expect(function() {339OrderedMap.from(idEntities, extractUniqueID);340}).toThrow();341342// Test passing an array to merge.343expect(function() {344OrderedMap.fromArray(idEntities, extractUniqueID)345.merge(idEntities, extractUniqueID);346}).toThrow();347348349// Test passing a plain object to merge.350expect(function() {351OrderedMap.fromArray(352idEntities,353extractUniqueID354).merge({blah: 'willFail'});355}).toThrow();356});357358it('should throw when accessing key before/after of non-key', function() {359var om = OrderedMap.fromArray([360{uniqueID: 'first'},361{uniqueID: 'two'}], extractUniqueID362);363expect(function() {364om.keyBefore('dog');365}).toThrow();366expect(function() {367om.keyAfter('cat');368}).toThrow();369expect(function() {370om.keyAfter(null);371}).toThrow();372expect(function() {373om.keyAfter(undefined);374}).toThrow();375});376377it('should throw passing invalid/not-present-keys to before/after',378function() {379var om = OrderedMap.fromArray([380{uniqueID: 'one', val: 'first'},381{uniqueID: 'two', val: 'second'},382{uniqueID: 'three', val: 'third'},383{uniqueID: 'four', val: 'fourth'}384], extractUniqueID);385386expect(function() {387om.keyBefore('');388}).toThrow();389expect(function() {390om.keyBefore(null);391}).toThrow();392expect(function() {393om.keyBefore(undefined);394}).toThrow();395expect(function() {396om.keyBefore('notInTheOrderedMap!');397}).toThrow();398399expect(function() {400om.keyAfter('');401}).toThrow();402expect(function() {403om.keyAfter(null);404}).toThrow();405expect(function() {406om.keyAfter(undefined);407}).toThrow();408expect(function() {409om.keyAfter('notInTheOrderedMap!');410}).toThrow();411412expect(function() {413om.nthKeyAfter('', 1);414}).toThrow();415expect(function() {416om.nthKeyAfter(null, 1);417}).toThrow();418expect(function() {419om.nthKeyAfter(undefined, 1);420}).toThrow();421expect(function() {422om.nthKeyAfter('notInTheOrderedMap!', 1);423}).toThrow();424425expect(function() {426om.nthKeyBefore('', 1);427}).toThrow();428expect(function() {429om.nthKeyBefore(null, 1);430}).toThrow();431expect(function() {432om.nthKeyBefore(undefined, 1);433}).toThrow();434expect(function() {435om.nthKeyBefore('notInTheOrderedMap!', 1);436}).toThrow();437});438439it('should correctly determine the nth key after before', function() {440var om = OrderedMap.fromArray([441{uniqueID: 'one', val: 'first'},442{uniqueID: 'two', val: 'second'},443{uniqueID: 'three', val: 'third'},444{uniqueID: 'four', val: 'fourth'}445], extractUniqueID);446expect(om.keyBefore('one')).toBe(undefined); // first key447expect(om.keyBefore('two')).toBe('one');448expect(om.keyBefore('three')).toBe('two');449expect(om.keyBefore('four')).toBe('three');450451expect(om.keyAfter('one')).toBe('two'); // first key452expect(om.keyAfter('two')).toBe('three');453expect(om.keyAfter('three')).toBe('four');454expect(om.keyAfter('four')).toBe(undefined);455456expect(om.nthKeyBefore('one', 0)).toBe('one'); // first key457expect(om.nthKeyBefore('one', 1)).toBe(undefined);458expect(om.nthKeyBefore('one', 2)).toBe(undefined);459expect(om.nthKeyBefore('two', 0)).toBe('two');460expect(om.nthKeyBefore('two', 1)).toBe('one');461expect(om.nthKeyBefore('four', 0)).toBe('four');462expect(om.nthKeyBefore('four', 1)).toBe('three');463464expect(om.nthKeyAfter('one', 0)).toBe('one');465expect(om.nthKeyAfter('one', 1)).toBe('two');466expect(om.nthKeyAfter('one', 2)).toBe('three');467expect(om.nthKeyAfter('two', 0)).toBe('two');468expect(om.nthKeyAfter('two', 1)).toBe('three');469expect(om.nthKeyAfter('four', 0)).toBe('four');470expect(om.nthKeyAfter('four', 1)).toBe(undefined);471});472473it('should compute key indices correctly', function() {474var om = OrderedMap.fromArray([475{uniqueID: 'one', val: 'first'},476{uniqueID: 'two', val: 'second'}477], extractUniqueID);478expect(om.keyAtIndex(0)).toBe('one');479expect(om.keyAtIndex(1)).toBe('two');480expect(om.keyAtIndex(2)).toBe(undefined);481expect(om.indexOfKey('one')).toBe(0);482expect(om.indexOfKey('two')).toBe(1);483expect(om.indexOfKey('nope')).toBe(undefined);484expect(function() {485om.indexOfKey(null);486}).toThrow();487expect(function() {488om.indexOfKey(undefined);489}).toThrow();490expect(function() {491om.indexOfKey(''); // Empty key is not allowed492}).toThrow();493});494495it('should compute indices on array that extracted numeric ids', function() {496var om = OrderedMap.fromArray(hasZeroStringKey, extractUniqueID);497expect(om.keyAtIndex(0)).toBe('greg');498expect(om.keyAtIndex(1)).toBe('0');499expect(om.keyAtIndex(2)).toBe('tom');500expect(om.indexOfKey('greg')).toBe(0);501expect(om.indexOfKey('0')).toBe(1);502expect(om.indexOfKey('tom')).toBe(2);503504505var verifyNumericKeys = function(om) {506expect(om.keyAtIndex(0)).toBe('0');507expect(om.keyAtIndex(1)).toBe('2');508expect(om.keyAtIndex(2)).toBe('1');509expect(om.indexOfKey('0')).toBe(0);510expect(om.indexOfKey('2')).toBe(1); // Proove these are not ordered by511expect(om.indexOfKey('1')).toBe(2); // their keys512};513var omStringNumberKeys =514OrderedMap.fromArray(hasAllNumericStringKeys, extractUniqueID);515verifyNumericKeys(omStringNumberKeys);516var omNumericKeys =517OrderedMap.fromArray(hasAllNumericKeys, extractUniqueID);518verifyNumericKeys(omNumericKeys);519});520521it('should compute indices on mutually exclusive merge', function() {522var om = OrderedMap.fromArray([523{uniqueID: 'one', val: 'first'},524{uniqueID: 'two', val: 'second'}525], extractUniqueID);526var om2 = OrderedMap.fromArray([527{uniqueID: 'three', val: 'third'}528], extractUniqueID);529var res = om.merge(om2);530531expect(res.length).toBe(3);532533expect(res.keyAtIndex(0)).toBe('one');534expect(res.keyAtIndex(1)).toBe('two');535expect(res.keyAtIndex(2)).toBe('three');536expect(res.keyAtIndex(3)).toBe(undefined);537538expect(res.indexOfKey('one')).toBe(0);539expect(res.indexOfKey('two')).toBe(1);540expect(res.indexOfKey('three')).toBe(2);541expect(res.indexOfKey('dog')).toBe(undefined);542543expect(res.has('one')).toBe(true);544expect(res.has('two')).toBe(true);545expect(res.has('three')).toBe(true);546expect(res.has('dog')).toBe(false);547548expect(res.get('one').val).toBe('first');549expect(res.get('two').val).toBe('second');550expect(res.get('three').val).toBe('third');551expect(res.get('dog')).toBe(undefined);552});553554it('should compute indices on intersected merge', function() {555var oneTwo = OrderedMap.fromArray([556{uniqueID: 'one', val: 'first'},557{uniqueID: 'two', val: 'secondOM1'}558], extractUniqueID);559560var testOneTwoMergedWithTwoThree = function(res) {561expect(res.length).toBe(3);562expect(res.keyAtIndex(0)).toBe('one');563expect(res.keyAtIndex(1)).toBe('two');564expect(res.keyAtIndex(2)).toBe('three');565expect(res.keyAtIndex(3)).toBe(undefined);566expect(res.indexOfKey('one')).toBe(0);567expect(res.indexOfKey('two')).toBe(1);568expect(res.indexOfKey('three')).toBe(2);569expect(res.indexOfKey('dog')).toBe(undefined);570expect(res.has('one')).toBe(true);571expect(res.has('two')).toBe(true);572expect(res.has('three')).toBe(true);573expect(res.has('dog')).toBe(false);574expect(res.get('one').val).toBe('first');575expect(res.get('two').val).toBe('secondOM2');576expect(res.get('three').val).toBe('third');577expect(res.get('dog')).toBe(undefined);578};579580var result =581oneTwo.merge(OrderedMap.fromArray([582{uniqueID: 'two', val: 'secondOM2'},583{uniqueID: 'three', val: 'third'}584], extractUniqueID));585testOneTwoMergedWithTwoThree(result);586587// Everything should be exactly as before, since the ordering of `two` was588// already determined by `om`.589result = oneTwo.merge(590OrderedMap.fromArray([591{uniqueID: 'three', val: 'third'},592{uniqueID: 'two', val:'secondOM2'}593], extractUniqueID)594);595testOneTwoMergedWithTwoThree(result);596597598var testTwoThreeMergedWithOneTwo = function(res) {599expect(res.length).toBe(3);600expect(res.keyAtIndex(0)).toBe('two');601expect(res.keyAtIndex(1)).toBe('three');602expect(res.keyAtIndex(2)).toBe('one');603expect(res.keyAtIndex(3)).toBe(undefined);604expect(res.indexOfKey('two')).toBe(0);605expect(res.indexOfKey('three')).toBe(1);606expect(res.indexOfKey('one')).toBe(2);607expect(res.indexOfKey('cat')).toBe(undefined);608expect(res.has('two')).toBe(true);609expect(res.has('three')).toBe(true);610expect(res.has('one')).toBe(true);611expect(res.has('dog')).toBe(false);612expect(res.get('one').val).toBe('first');613expect(res.get('two').val).toBe('secondOM1');614expect(res.get('three').val).toBe('third');615expect(res.get('dog')).toBe(undefined);616};617result = OrderedMap.fromArray([618{uniqueID: 'two', val: 'secondOM2'},619{uniqueID: 'three', val: 'third'}620], extractUniqueID).merge(oneTwo);621testTwoThreeMergedWithOneTwo(result);622623});624625it('should merge mutually exclusive keys to the end.', function() {626var om = OrderedMap.fromArray([627{uniqueID: 'one', val: 'first'},628{uniqueID: 'two', val: 'second'}629], extractUniqueID);630var om2 = OrderedMap.fromArray([631{uniqueID: 'three', val: 'first'},632{uniqueID: 'four', val: 'second'}633], extractUniqueID);634var res = om.merge(om2);635expect(res.length).toBe(4);636637});638639it('should map correctly', function() {640var om = OrderedMap.fromArray([641{uniqueID: 'x', val: 'xx'},642{uniqueID: 'y', val: 'yy'},643{uniqueID: 'z', val: 'zz'}644], extractUniqueID);645var scope = {justToTestScope: 'justTestingScope'};646var verifyResult = function(omResult) {647expect(omResult.length).toBe(3);648expect(omResult.keyAtIndex(0)).toBe('x');649expect(omResult.keyAtIndex(1)).toBe('y');650expect(omResult.keyAtIndex(2)).toBe('z');651expect(omResult.get('x').val).toBe('xxx0justTestingScope');652expect(omResult.get('y').val).toBe('yyy1justTestingScope');653expect(omResult.get('z').val).toBe('zzz2justTestingScope');654};655var resultOM = om.map(function(itm, key, count) {656return {657uniqueID: itm.uniqueID,658val: itm.val + key + count + this.justToTestScope659};660}, scope);661verifyResult(resultOM);662663var resArray = [];664om.forEach(function(itm, key, count) {665resArray.push({666uniqueID: itm.uniqueID,667val: itm.val + key + count + this.justToTestScope668});669}, scope);670resultOM = OrderedMap.fromArray(resArray, extractUniqueID);671verifyResult(resultOM);672});673674it('should filter correctly', function() {675var om = OrderedMap.fromArray([676{uniqueID: 'x', val: 'xx'},677{uniqueID: 'y', val: 'yy'},678{uniqueID: 'z', val: 'zz'}679], extractUniqueID);680var scope = {justToTestScope: 'justTestingScope'};681682var filteringCallback = function(item, key, indexInOriginal) {683expect(this).toBe(scope);684expect(key === 'x' || key === 'y' || key === 'z').toBe(true);685if (key === 'x') {686expect(item.val).toBe('xx');687expect(indexInOriginal).toBe(0);688return false;689} else if (key === 'y') {690expect(item.val).toBe('yy');691expect(indexInOriginal).toBe(1);692return true;693} else {694expect(item.val).toBe('zz');695expect(indexInOriginal).toBe(2);696return true;697}698};699700var verifyResult = function(omResult) {701expect(omResult.length).toBe(2);702expect(omResult.keyAtIndex(0)).toBe('y');703expect(omResult.keyAtIndex(1)).toBe('z');704expect(omResult.has('x')).toBe(false);705expect(omResult.has('z')).toBe(true);706expect(omResult.get('z').val).toBe('zz');707expect(omResult.has('y')).toBe(true);708expect(omResult.get('y').val).toBe('yy');709};710711var resultOM = om.filter(filteringCallback, scope);712verifyResult(resultOM);713});714715it('should throw when providing invalid ranges to ranging', function() {716var om = OrderedMap.fromArray([717{uniqueID: 'x', val: 'xx'},718{uniqueID: 'y', val: 'yy'},719{uniqueID: 'z', val: 'zz'}720], extractUniqueID);721var scope = {justToTestScope: 'justTestingScope'};722723expect(function() {724om.mapRange(duplicate, 0, 3, scope);725}).not.toThrow();726expect(function() {727om.filterRange(duplicate, 0, 3, scope);728}).not.toThrow();729expect(function() {730om.forEachRange(duplicate, 0, 3, scope);731}).not.toThrow();732expect(function() {733om.mapKeyRange(duplicate, 'x' , 3, scope);734}).toThrow(735'Invariant Violation: mapKeyRange must be given keys ' +736'that are present.'737);738expect(function() {739om.forEachKeyRange(duplicate, 'x', 3, scope);740}).toThrow(741'Invariant Violation: forEachKeyRange must be given keys ' +742'that are present.'743);744745expect(function() {746om.mapRange(duplicate, 0, 4, scope);747}).toThrow();748expect(function() {749om.filterRange(duplicate, 0, 4, scope);750}).toThrow();751expect(function() {752om.forEachRange(duplicate, 0, 4, scope);753}).toThrow();754expect(function() {755om.mapKeyRange(duplicate, 'x', null, scope);756}).toThrow();757expect(function() {758om.forEachKeyRange(duplicate, 'x', null, scope);759}).toThrow();760761expect(function() {762om.mapRange(duplicate, -1, 1, scope);763}).toThrow();764expect(function() {765om.filterRange(duplicate, -1, 1, scope);766}).toThrow();767expect(function() {768om.forEachRange(duplicate, -1, 1, scope);769}).toThrow();770expect(function() {771om.mapKeyRange(duplicate, null, 'y', scope);772}).toThrow();773expect(function() {774om.forEachKeyRange(duplicate, null, 'y', scope);775}).toThrow();776777expect(function() {778om.mapRange(duplicate, 0, 0, scope);779}).not.toThrow();780expect(function() {781om.filterRange(duplicate, 0, 0, scope);782}).not.toThrow();783expect(function() {784om.forEachRange(duplicate, 0, 0, scope);785}).not.toThrow();786expect(function() {787om.mapKeyRange(duplicate, 'x', 'x', scope);788}).not.toThrow();789expect(function() {790om.forEachKeyRange(duplicate, 'x', 'x', scope);791}).not.toThrow();792793expect(function() {794om.mapRange(duplicate, 0, -1, scope);795}).toThrow();796expect(function() {797om.filterRange(duplicate, 0, -1, scope);798}).toThrow();799expect(function() {800om.forEachRange(duplicate, 0, -1, scope);801}).toThrow();802expect(function() {803om.mapKeyRange(duplicate, 'x', null, scope);804}).toThrow();805expect(function() {806om.forEachKeyRange(duplicate, 'x', null, scope);807}).toThrow();808809expect(function() {810om.mapRange(duplicate, 2, 1, scope);811}).not.toThrow();812expect(function() {813om.filterRange(duplicate, 2, 1, scope);814}).not.toThrow();815expect(function() {816om.forEachRange(duplicate, 2, 1, scope);817}).not.toThrow();818expect(function() {819om.mapKeyRange(duplicate, 'z', 'z', scope);820}).not.toThrow();821expect(function() {822om.forEachKeyRange(duplicate, 'z', 'z', scope);823}).not.toThrow();824825expect(function() {826om.mapRange(duplicate, 2, 2, scope);827}).toThrow();828expect(function() {829om.filterRange(duplicate, 2, 2, scope);830}).toThrow();831expect(function() {832om.forEachRange(duplicate, 2, 2, scope);833}).toThrow();834expect(function() {835om.mapKeyRange(duplicate, 'z', null, scope);836}).toThrow();837expect(function() {838om.forEachKeyRange(duplicate, 'z', null, scope);839}).toThrow();840841// Provide keys in reverse order - should throw.842expect(function() {843om.mapKeyRange(duplicate, 'y', 'x', scope);844}).toThrow();845expect(function() {846om.forEachKeyRange(duplicate, 'y', 'x', scope);847}).toThrow();848});849850// TEST length zero map, or keyrange start===end851852it('should map range correctly', function() {853var om = OrderedMap.fromArray([854{uniqueID: 'x', val: 'xx'},855{uniqueID: 'y', val: 'yy'},856{uniqueID: 'z', val: 'zz'}857], extractUniqueID);858var scope = {justToTestScope: 'justTestingScope'};859var verifyThreeItems = function(omResult) {860expect(omResult.length).toBe(3);861expect(omResult.keyAtIndex(0)).toBe('x');862expect(omResult.keyAtIndex(1)).toBe('y');863expect(omResult.keyAtIndex(2)).toBe('z');864expect(omResult.get('x').val).toBe('xxx0justTestingScope');865expect(omResult.get('y').val).toBe('yyy1justTestingScope');866expect(omResult.get('z').val).toBe('zzz2justTestingScope');867};868var verifyFirstTwoItems = function(omResult) {869expect(omResult.length).toBe(2);870expect(omResult.keyAtIndex(0)).toBe('x');871expect(omResult.keyAtIndex(1)).toBe('y');872expect(omResult.get('x').val).toBe('xxx0justTestingScope');873expect(omResult.get('y').val).toBe('yyy1justTestingScope');874};875876var verifyLastTwoItems = function(omResult) {877expect(omResult.length).toBe(2);878expect(omResult.keyAtIndex(0)).toBe('y');879expect(omResult.keyAtIndex(1)).toBe('z');880expect(omResult.get('y').val).toBe('yyy1justTestingScope');881expect(omResult.get('z').val).toBe('zzz2justTestingScope');882};883884var verifyMiddleItem = function(omResult) {885expect(omResult.length).toBe(1);886expect(omResult.keyAtIndex(0)).toBe('y');887expect(omResult.get('y').val).toBe('yyy1justTestingScope');888};889890var verifyEmpty = function(omResult) {891expect(omResult.length).toBe(0);892};893894var omResultThree = om.mapRange(duplicate, 0, 3, scope);895verifyThreeItems(omResultThree);896var resArray = [];897var pushToResArray = function(itm, key, count) {898resArray.push({899uniqueID: itm.uniqueID,900val: itm.val + key + count + this.justToTestScope901});902};903904om.forEachRange(pushToResArray, 0, 3, scope);905omResultThree = OrderedMap.fromArray(resArray, extractUniqueID);906verifyThreeItems(omResultThree);907908var omResultFirstTwo = om.mapRange(duplicate, 0, 2, scope);909verifyFirstTwoItems(omResultFirstTwo);910resArray = [];911om.forEachRange(pushToResArray, 0, 2, scope);912omResultFirstTwo = OrderedMap.fromArray(resArray, extractUniqueID);913verifyFirstTwoItems(omResultFirstTwo);914915var omResultLastTwo = om.mapRange(duplicate, 1, 2, scope);916verifyLastTwoItems(omResultLastTwo);917resArray = [];918om.forEachRange(pushToResArray, 1, 2, scope);919omResultLastTwo = OrderedMap.fromArray(resArray, extractUniqueID);920verifyLastTwoItems(omResultLastTwo);921922var omResultMiddle = om.mapRange(duplicate, 1, 1, scope);923verifyMiddleItem(omResultMiddle);924resArray = [];925om.forEachRange(pushToResArray, 1, 1, scope);926omResultMiddle = OrderedMap.fromArray(resArray, extractUniqueID);927verifyMiddleItem(omResultMiddle);928929var omResultNone = om.mapRange(duplicate, 1, 0, scope);930verifyEmpty(omResultNone);931});932933it('should extract the original array correctly', function() {934var sourceArray = [935{uniqueID: 'x', val: 'xx'},936{uniqueID: 'y', val: 'yy'},937{uniqueID: 'z', val: 'zz'}938];939var om = OrderedMap.fromArray(sourceArray, extractUniqueID);940expect(om.toArray()).toEqual(sourceArray);941});942});943944945