UNPKG

@skele/core

Version:

Core package of the Skele framework. Element definitions, registrations and traversal.

1 lines 9.93 kB
'use strict';var _immutable=_interopRequireDefault(require("immutable"));var _ramda=_interopRequireDefault(require("ramda"));var zip=_interopRequireWildcard(require("../"));var _data2=require("../../data");var _predicate=require("../predicate");var _selector=require("../selector");var _selector2=require("../skele/selector");var _select=require("../select");var _ref,_data;function _interopRequireWildcard(obj){if(obj&&obj.__esModule){return obj;}else{var newObj={};if(obj!=null){for(var key in obj){if(Object.prototype.hasOwnProperty.call(obj,key)){var desc=Object.defineProperty&&Object.getOwnPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):{};if(desc.get||desc.set){Object.defineProperty(newObj,key,desc);}else{newObj[key]=obj[key];}}}}newObj.default=obj;return newObj;}}function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj};}function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_nonIterableSpread();}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance");}function _iterableToArray(iter){if((typeof Symbol==="function"?Symbol.iterator:"@@iterator")in Object(iter)||Object.prototype.toString.call(iter)==="[object Arguments]")return Array.from(iter);}function _arrayWithoutHoles(arr){if(Array.isArray(arr)){for(var i=0,arr2=new Array(arr.length);i<arr.length;i++){arr2[i]=arr[i];}return arr2;}}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}var iprop=_ramda.default.invoker(1,'get');var zipper=function zipper(data){return zip.elementZipper({})(data);};var data=(_data={kind:['navigation','tabs']},_defineProperty(_data,_data2.childrenProperty,['tabs','settings']),_defineProperty(_data,"title",'App'),_defineProperty(_data,"tabs",[{kind:['tab'],title:'Murder Bot'},(_ref={kind:['tab'],title:'Dr Mensah'},_defineProperty(_ref,_data2.childrenProperty,['elements','deeplink']),_defineProperty(_ref,"elements",[{kind:['element'],title:'robot'},{kind:['element'],title:'human'}]),_defineProperty(_ref,"deeplink",{kind:['link'],title:'goodreads'}),_ref)]),_defineProperty(_data,"settings",{kind:['settings'],title:'Martha Wells'}),_data);describe('zip.select',function(){describe('helpers',function(){test('isStringArray',function(){expect((0,_select.isStringArray)(['string','test'])).toEqual(true);expect((0,_select.isStringArray)(['string',null])).toEqual(false);expect((0,_select.isStringArray)(['string',undefined])).toEqual(false);expect((0,_select.isStringArray)([{},'test'])).toEqual(false);expect((0,_select.isStringArray)(['string',[]])).toEqual(false);expect((0,_select.isStringArray)([[]])).toEqual(false);expect((0,_select.isStringArray)([])).toEqual(true);expect((0,_select.isStringArray)(null)).toEqual(false);expect((0,_select.isStringArray)(undefined)).toEqual(false);});test('isLocationSeq',function(){expect((0,_select.isLocationSeq)(null)).toEqual(false);expect((0,_select.isLocationSeq)(undefined)).toEqual(false);expect((0,_select.isLocationSeq)(_immutable.default.Seq())).toEqual(false);expect((0,_select.isLocationSeq)(_immutable.default.Seq.of('test'))).toEqual(false);expect((0,_select.isLocationSeq)(_immutable.default.Seq.of(zipper(_immutable.default.fromJS(data))))).toEqual(true);expect((0,_select.isLocationSeq)(_immutable.default.Seq.of(zipper(_immutable.default.fromJS(data)),zipper(_immutable.default.fromJS(data))))).toEqual(true);expect((0,_select.isLocationSeq)([zipper(_immutable.default.fromJS(data)),'Not a Zipper',zipper(_immutable.default.fromJS(data))])).toEqual(false);});});describe('predicates',function(){test('children',function(){var root=zipper(_immutable.default.fromJS(data));var kids=_toConsumableArray((0,_selector2.children)(root));expect(kids.length).toEqual(3);expect((0,_data2.flow)(kids,_ramda.default.path([0]),zip.node,iprop('title'))).toEqual('Murder Bot');expect((0,_data2.flow)(kids,_ramda.default.path([1]),zip.node,iprop('title'))).toEqual('Dr Mensah');expect((0,_data2.flow)(kids,_ramda.default.path([2]),zip.node,iprop('title'))).toEqual('Martha Wells');var extendedData=_immutable.default.fromJS(data).updateIn([_data2.childrenProperty],function(children){return children.push('world');}).set('world',_immutable.default.List.of(_immutable.default.fromJS({kind:['world'],title:'Alien'})));var newKidsOnTheBlock=_toConsumableArray((0,_selector2.children)(zipper(extendedData)));expect(newKidsOnTheBlock.length).toEqual(4);expect((0,_data2.flow)(newKidsOnTheBlock,_ramda.default.path([3]),zip.node,iprop('title'))).toEqual('Alien');});test('ancestors',function(){var root=zipper(_immutable.default.fromJS(data));var settings=(0,_data2.flow)(root,(0,_selector2.childrenAt)('settings'),function(children){return _toConsumableArray(children)[0];});expect((0,_data2.flow)(settings,zip.node,iprop('title'))).toEqual('Martha Wells');var tabs=_toConsumableArray((0,_data2.flow)(root,(0,_selector2.childrenAt)('tabs')));expect(tabs.length).toEqual(2);var mensahChildren=(0,_data2.flow)(tabs,_ramda.default.path([1]),_selector2.children,function(children){return _toConsumableArray(children);});expect(mensahChildren.length).toEqual(3);var deeplinkLoc=_ramda.default.find(function(loc){return(0,_data2.isOfKind)('link',zip.node(loc));},mensahChildren);expect((0,_data2.flow)(deeplinkLoc,zip.node,iprop('title'))).toEqual('goodreads');var robotLoc=_ramda.default.find(function(loc){return(0,_data2.isOfKind)('element',zip.node(loc))&&zip.node(loc).get('title')==='robot';},mensahChildren);expect((0,_data2.flow)(robotLoc,zip.node,iprop('title'))).toEqual('robot');var robotAncestors=_toConsumableArray((0,_selector.ancestors)(robotLoc));expect(robotAncestors.length).toEqual(4);expect((0,_data2.flow)(robotAncestors,_ramda.default.path([1]),zip.node,iprop('title'))).toEqual('Dr Mensah');expect((0,_data2.flow)(robotAncestors,_ramda.default.path([3]),zip.node,iprop('title'))).toEqual('App');var deeplinkAncestors=_toConsumableArray((0,_selector.ancestors)(deeplinkLoc));expect(deeplinkAncestors.length).toEqual(4);expect((0,_data2.flow)(deeplinkAncestors,_ramda.default.path([1]),zip.node,iprop('title'))).toEqual('Dr Mensah');expect((0,_data2.flow)(deeplinkAncestors,_ramda.default.path([3]),zip.node,iprop('title'))).toEqual('App');var settingsAncestors=_toConsumableArray((0,_selector.ancestors)(settings));expect(settingsAncestors.length).toEqual(2);expect((0,_data2.flow)(settingsAncestors,_ramda.default.path([1]),zip.node,iprop('title'))).toEqual('App');});test('desendants',function(){var root=zipper(_immutable.default.fromJS(data));var settings=(0,_data2.flow)(root,(0,_selector2.childrenAt)('settings'),function(children){return _toConsumableArray(children)[0];});expect(_toConsumableArray((0,_selector.descendants)(settings)).length).toEqual(0);var tabs=_toConsumableArray((0,_selector2.childrenAt)('tabs')(root));var mensahDescendants=_toConsumableArray((0,_selector.descendants)(tabs[1]));expect(mensahDescendants.length).toEqual(5);expect((0,_data2.flow)(mensahDescendants,_ramda.default.path([0]),zip.node,iprop('title'))).toEqual('human');expect((0,_data2.flow)(mensahDescendants,_ramda.default.path([1]),zip.node,iprop('title'))).toEqual('robot');expect((0,_data2.flow)(mensahDescendants,_ramda.default.path([2]),zip.node,iprop('title'))).toEqual('goodreads');var all=_toConsumableArray((0,_selector.descendants)(root));expect(all.length).toEqual(10);expect((0,_data2.flow)(all,_ramda.default.path([0]),zip.node,iprop('title'))).toEqual('human');expect((0,_data2.flow)(all,_ramda.default.path([1]),zip.node,iprop('title'))).toEqual('robot');expect((0,_data2.flow)(all,_ramda.default.path([2]),zip.node,iprop('title'))).toEqual('goodreads');expect((0,_data2.flow)(all,_ramda.default.path([5]),zip.node,iprop('title'))).toEqual('Dr Mensah');expect((0,_data2.flow)(all,_ramda.default.path([6]),zip.node,iprop('title'))).toEqual('Murder Bot');expect((0,_data2.flow)(all,_ramda.default.path([7]),zip.node,iprop('title'))).toEqual('Martha Wells');});});describe('select',function(){var root=zipper(_immutable.default.fromJS(data));it('should return array with given location for no predicates',function(){expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([],root)).length)).toEqual(1);expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([],root)),_ramda.default.path([0]),zip.node)).toEqualI(zip.value(root));});it('should give proper result for combinations of predicates',function(){expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([_selector.descendants,(0,_predicate.propEq)('title','goodreads')],root)),_ramda.default.prop('length'))).toEqual(1);expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([_selector.descendants,(0,_predicate.ofKind)('element')],root)),_ramda.default.prop('length'))).toEqual(2);expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([_selector.descendants,(0,_predicate.ofKind)('tab'),(0,_predicate.propEq)('title','Murder Bot')],root)),_ramda.default.prop('length'))).toEqual(1);expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([_selector.descendants,['link']],root)),_ramda.default.prop('length'))).toEqual(1);expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([_selector.descendants,['tab']],root)),_ramda.default.prop('length'))).toEqual(2);expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([_selector.descendants,['tab'],'deeplink'],root)),_ramda.default.prop('length'))).toEqual(1);});it('should hanlde gracefully in case unknown predicate creaps in',function(){expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([_selector.descendants,function(loc){return{loc:loc};}],root)),_ramda.default.prop('length'))).toEqual(0);expect((0,_data2.flow)(_toConsumableArray((0,_select.select)([_selector.descendants,{prop:'test'}],root)),_ramda.default.prop('length'))).toEqual(0);});});});