UNPKG

@skele/core

Version:

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

1 lines 7.32 kB
'use strict';var _immutable=require("immutable");var _=require("../");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;}describe('element',function(){it('tests if an object is an element',function(){var emptyKind=(0,_immutable.fromJS)({kind:[]});var stringKind=(0,_immutable.fromJS)({kind:'component'});var singleKind=(0,_immutable.fromJS)({kind:['component']});var doubleKind=(0,_immutable.fromJS)({kind:['component','test']});var noKind=(0,_immutable.fromJS)({title:'bla'});var aList=_immutable.List.of(stringKind,singleKind,doubleKind,emptyKind);expect((0,_.isElement)(emptyKind)).toBeTruthy();expect((0,_.isElement)(stringKind)).toBeTruthy();expect((0,_.isElement)(singleKind)).toBeTruthy();expect((0,_.isElement)(doubleKind)).toBeTruthy();expect((0,_.isElement)(noKind)).toEqual(false);expect((0,_.isElement)(aList)).toEqual(false);});it('tests if an object-kind is a proper ref for an element',function(){expect((0,_.isElementRef)(undefined)).toEqual(false);expect((0,_.isElementRef)(null)).toEqual(false);expect((0,_.isElementRef)([null])).toEqual(false);expect((0,_.isElementRef)(['test',null])).toEqual(false);expect((0,_.isElementRef)(['test',{}])).toEqual(false);expect((0,_.isElementRef)(['test',[]])).toEqual(false);expect((0,_.isElementRef)([])).toEqual(true);expect((0,_.isElementRef)('test')).toEqual(true);expect((0,_.isElementRef)(['test'])).toEqual(true);expect((0,_.isElementRef)(['test','test2'])).toEqual(true);});it('properly checks for element kinds',function(){var emptyKind=(0,_immutable.fromJS)({kind:[]});var stringKind=(0,_immutable.fromJS)({kind:'component'});var singleKind=(0,_immutable.fromJS)({kind:['component']});var doubleKind=(0,_immutable.fromJS)({kind:['component','test']});var tripleKind=(0,_immutable.fromJS)({kind:['component','test','detail']});expect((0,_.isOfKind)('component',stringKind)).toEqual(true);expect((0,_.isOfKind)('component')(stringKind)).toEqual(true);expect((0,_.isOfKind)(['component'],stringKind)).toEqual(true);expect((0,_.isOfKind)([],singleKind)).toEqual(true);expect((0,_.isOfKind)(['component'],singleKind)).toEqual(true);expect((0,_.isOfKind)(['component'],doubleKind)).toEqual(true);expect((0,_.isOfKind)(['component','test'],doubleKind)).toEqual(true);expect((0,_.isOfKind)(['component'],tripleKind)).toEqual(true);expect((0,_.isOfKind)(['component','test'],tripleKind)).toEqual(true);expect((0,_.isOfKind)(['component','test','detail'],tripleKind)).toEqual(true);expect((0,_.isOfKind)('component',null)).toEqual(false);expect((0,_.isOfKind)('component',emptyKind)).toEqual(false);expect((0,_.isOfKind)('',emptyKind)).toEqual(false);expect((0,_.isOfKind)(['unknown'],stringKind)).toEqual(false);expect((0,_.isOfKind)(['component','test'],singleKind)).toEqual(false);expect((0,_.isOfKind)(['test'],doubleKind)).toEqual(false);expect((0,_.isOfKind)(['component','test','detail'],doubleKind)).toEqual(false);expect((0,_.isOfKind)(['detail'],tripleKind)).toEqual(false);expect((0,_.isOfKind)(['component','test','detail','unknown'],tripleKind)).toEqual(false);});it('properly checks for exact element kinds',function(){var element=(0,_immutable.fromJS)({kind:['component','test']});expect((0,_.isExactlyOfKind)(null,null)).toEqual(false);expect((0,_.isExactlyOfKind)(['component'],element)).toEqual(false);expect((0,_.isExactlyOfKind)(['component','test'],element)).toEqual(true);expect((0,_.isExactlyOfKind)(['component','test'])(element)).toEqual(true);});it('returns the element kind',function(){var kind=['component','test'];var element1=(0,_immutable.fromJS)({kind:null});var element2=(0,_immutable.fromJS)({kind:kind});expect((0,_.kindOf)(element1)).toEqual(null);expect((0,_.kindOf)(element2)).toEqual((0,_immutable.List)(kind));});it('returns the ancestor kinds',function(){var kinds=['component','test','detail'];var ancestors=[['component','test','detail'],['component','test'],['component']];expect((0,_.ancestorKinds)([]).toJS()).toEqual([]);expect((0,_.ancestorKinds)('component').toJS()).toEqual([['component']]);expect((0,_.ancestorKinds)(kinds).toJS()).toEqual(ancestors);});it('properly normalizes element kinds',function(){expect((0,_.canonical)(null)==null).toBe(true);expect((0,_.canonical)(true)==null).toBe(true);expect((0,_.canonical)('component')).toEqual(_immutable.List.of('component'));expect((0,_.canonical)(['component','test'])).toEqual(_immutable.List.of('component','test'));expect((0,_.canonical)(_immutable.List.of('component','test'))).toEqual(_immutable.List.of('component','test'));expect((0,_.canonical)(_immutable.Seq.of('component'))).toEqual(_immutable.List.of('component'));});});describe('childPositions',function(){var aString=(0,_immutable.fromJS)(_defineProperty({kind:'component'},_.childrenProperty,'children'));var anArray=(0,_immutable.fromJS)(_defineProperty({kind:'component'},_.childrenProperty,['children']));var multiple=(0,_immutable.fromJS)(_defineProperty({kind:'component'},_.childrenProperty,['children','aside']));var missing=(0,_immutable.fromJS)({kind:'component'});var deprecatedUse=(0,_immutable.fromJS)({kind:'component','@@girders-elements/children':['children']});expect((0,_.childPositions)(aString)).toEqualI(_immutable.List.of('children'));expect((0,_.childPositions)(anArray)).toEqualI(_immutable.List.of('children'));expect((0,_.childPositions)(multiple)).toEqualI(_immutable.List.of('children','aside'));expect((0,_.childPositions)(missing)).toEqualI((0,_immutable.List)());expect((0,_.childPositions)(deprecatedUse)).toEqualI(_immutable.List.of('children'));});describe('pathsToChildElements',function(){var _fromJS4,_fromJS5,_fromJS6,_fromJS7,_fromJS8;var aStringWithSingleChild=(0,_immutable.fromJS)((_fromJS4={kind:'component'},_defineProperty(_fromJS4,_.childrenProperty,'children'),_defineProperty(_fromJS4,"children",{kind:'child',bla:1}),_fromJS4));expect((0,_.pathsToChildElements)(aStringWithSingleChild)).toEqualI((0,_immutable.fromJS)([['children']]));var anArrayWithSingleChild=(0,_immutable.fromJS)((_fromJS5={kind:'component'},_defineProperty(_fromJS5,_.childrenProperty,['children']),_defineProperty(_fromJS5,"children",{kind:'child'}),_fromJS5));expect((0,_.pathsToChildElements)(anArrayWithSingleChild)).toEqualI((0,_immutable.fromJS)([['children']]));var aStringWithChildrenArray=(0,_immutable.fromJS)((_fromJS6={kind:'component'},_defineProperty(_fromJS6,_.childrenProperty,'children'),_defineProperty(_fromJS6,"children",[{kind:'child'},{kind:'child'}]),_fromJS6));expect((0,_.pathsToChildElements)(aStringWithChildrenArray)).toEqualI((0,_immutable.fromJS)([['children',0],['children',1]]));var anArrayWithChildrenArray=(0,_immutable.fromJS)((_fromJS7={kind:'component'},_defineProperty(_fromJS7,_.childrenProperty,['children']),_defineProperty(_fromJS7,"children",[{kind:'child'}]),_fromJS7));expect((0,_.pathsToChildElements)(anArrayWithChildrenArray)).toEqualI((0,_immutable.fromJS)([['children',0]]));var nonExistingChildPOsition=(0,_immutable.fromJS)((_fromJS8={kind:'component'},_defineProperty(_fromJS8,_.childrenProperty,['children','foo']),_defineProperty(_fromJS8,"children",{kind:'child'}),_fromJS8));expect((0,_.pathsToChildElements)(nonExistingChildPOsition)).toEqualI((0,_immutable.fromJS)([['children']]));});