import * as ReactTestUtils from "react-dom/test-utils" ; import { expect } from "chai" ; import * as enzyme from "enzyme" ; import "./setup" ; import { ClientAemContext } from "../AemContext" ; import { ResourceComponent } from "../component/ResourceComponent" ; import RootComponent from "../component/RootComponent" ; import * as React from "react" ; import RootComponentRegistry from "../RootComponentRegistry" ; import ComponentRegistry from "../ComponentRegistry" ; import { Container } from "../di/Container" ; import ComponentManager from "../ComponentManager" ; import MockSling from "./MockSling" ; import { Cq } from "../references" ; import Cache from "../store/Cache" ; describe ( "ResourceComponent" , ( ) => { class Test extends ResourceComponent < any , any , any > { public renderBody ( ) { return ( < span className = "test" > { this . props . resource ? this . props . resource . text : "unknown" } < /span>); } } class Embedded extends ResourceComponent < any , any , any > { public renderBody ( ) { return ( < Test path = "test" > < /Test>); } } class AemContainer extends ResourceComponent < any , any , any > { public renderBody ( ) { let children = this . renderChildren ( this . props . childPath , this . props . childClassName , this . props . childElementName ) ; return ( < div data - container > { children } < /div>); } } function createContainer ( className ? , elementName ? , childPath ? ) { return class AnonComponent extends ResourceComponent < any , any , any > { public renderBody ( ) { let children = this . renderChildren ( childPath , className , elementName ) ; return ( < div data - container > { children } < /div>); } } ; } let testRegistry = new ComponentRegistry ( ) ; testRegistry . register ( Test ) ; let registry = new RootComponentRegistry ( ) ; registry . add ( testRegistry ) ; registry . init ( ) ; let container = new Container ( ( { } as Cq ) ) ; let componentManager = new ComponentManager ( registry , container ) ; let aemContext = { registry : registry , componentManager : componentManager , container : container } ; it ( "should render loading message" , ( ) => { container . register ( "sling" , new MockSling ( null ) ) ; let item = enzyme . mount ( < RootComponent aemContext = { aemContext } comp = { Test } path = "/content/notfound" / > ) ; expect ( item . find ( "span" ) . html ( ) ) . to . equal ( "<span>Loading</span>" ) ; } ) ; it ( "should get resource directly" , ( ) => { let cache = new Cache ( ) ; cache . put ( "/content/embed" , { test : { text : "Hallo" } } ) ; container . register ( "sling" , new MockSling ( cache ) ) ; const item = ReactTestUtils . renderIntoDocument ( < RootComponent aemContext = { aemContext } comp = { Embedded } path = "/content/embed" / > ) ; let test = ReactTestUtils . findRenderedComponentWithType ( item , Test ) ; expect ( test . getPath ( ) ) . to . equal ( "/content/embed/test" ) ; expect ( test . props . path ) . to . equal ( "test" ) ; expect ( test . getResource ( ) . text ) . to . equal ( "Hallo" ) ; } ) ; it ( "should get resource from absolute Path" , ( ) => { let cache = new Cache ( ) ; cache . put ( "/content/test" , { text : "Hallo" } ) ; container . register ( "sling" , new MockSling ( cache ) ) ; const item = ReactTestUtils . renderIntoDocument ( < RootComponent aemContext = { aemContext } comp = { Test } path = "/content/test" / > ) ; let test = ReactTestUtils . findRenderedComponentWithType ( item , Test ) ; expect ( test . getPath ( ) ) . to . equal ( "/content/test" ) ; expect ( test . props . path ) . to . equal ( "/content/test" ) ; expect ( test . getResource ( ) . text ) . to . equal ( "Hallo" ) ; } ) ; it ( "should render htl children wcmmode disabled" , ( ) => { let cache = new Cache ( ) ; cache . put ( "/content" , { "child1" : { "sling:resourceType" : "htl/test" , "text" : "Hallo" , "jcr:primaryType" : "nt:unstructured" } } ) ; container . register ( "sling" , new MockSling ( cache ) ) ; const item = enzyme . render ( < RootComponent wcmmode = "disabled" aemContext = { aemContext } comp = { AemContainer } path = "/content" / > ) ; let include = item . find ( "include" ) ; expect ( include [ 0 ] . attribs . path ) . to . equal ( "/content/child1" ) ; expect ( include [ 0 ] . attribs . resourcetype ) . to . equal ( "htl/test" ) ; } ) ; describe ( "should render htl children wcmmode enabled" , ( ) => { before ( ( ) => { let cache = new Cache ( ) ; cache . put ( "/content" , { "child1" : { "sling:resourceType" : "htl/test" , "text" : "Hallo" , "jcr:primaryType" : "nt:unstructured" } } ) ; container . register ( "sling" , new MockSling ( cache ) ) ; } ) ; it ( "default " , ( ) => { const item = enzyme . render ( < RootComponent wcmmode = "edit" aemContext = { aemContext } comp = { AemContainer } path = "/content" / > ) ; let include = item . find ( "include" ) ; expect ( include [ 1 ] . attribs . path ) . to . equal ( "/content/*" ) ; expect ( include [ 1 ] . attribs . resourcetype ) . to . equal ( "foundation/components/parsys/new" ) ; } ) ; it ( "with child wrapper " , ( ) => { const item = enzyme . mount ( < RootComponent wcmmode = "disabled" aemContext = { aemContext } comp = { createContainer ( "childClass" , "el" ) } path = "/content" / > ) ; let dialog = item . find ( "el" ) ; expect ( dialog . props ( ) . className ) . to . equal ( "childClass" ) ; expect ( dialog . html ( ) ) . to . equal ( '<el class="childClass"><div><include resourcetype="htl/test" path="/content/child1"></include></div></el>' ) ; } ) ; } ) ; describe ( "should render react children wcmmode disabled" , ( ) => { before ( ( ) => { let cache = new Cache ( ) ; cache . put ( "/content" , { "child1" : { "sling:resourceType" : "test" , "text" : "OOPS" , "jcr:primaryType" : "nt:unstructured" } } ) ; container . register ( "sling" , new MockSling ( cache ) ) ; } ) ; it ( "default " , ( ) => { const item = enzyme . render ( < RootComponent wcmmode = "disabled" aemContext = { aemContext } comp = { AemContainer } path = "/content" / > ) ; let test = item . find ( ".test" ) ; expect ( test [ 0 ] . children [ 0 ] . data ) . to . equal ( "OOPS" ) ; } ) ; it ( "with child wrapper" , ( ) => { const item = enzyme . render ( < RootComponent wcmmode = "disabled" aemContext = { aemContext } comp = { createContainer ( "childClass" , "el" ) } path = "/content" / > ) ; let test = item . find ( "el" ) ; expect ( test . length ) . to . equal ( 1 ) ; expect ( test [ 0 ] . attribs . class ) . to . equal ( "childClass" ) ; } ) ; it ( "with child class name" , ( ) => { const item = enzyme . render ( < RootComponent wcmmode = "disabled" aemContext = { aemContext } comp = { createContainer ( "childClass" ) } path = "/content" / > ) ; let dialog = item . find ( ".dialog" ) ; expect ( dialog [ 0 ] . attribs . class . split ( " " ) ) . to . contain ( "childClass" ) ; } ) ; } ) ; describe ( "should render react children with child path" , ( ) => { before ( ( ) => { let cache = new Cache ( ) ; cache . put ( "/content" , { "children" : { child1 : { "sling:resourceType" : "test" , "text" : "OOPS" , "jcr:primaryType" : "nt:unstructured" } } } ) ; container . register ( "sling" , new MockSling ( cache ) ) ; } ) ; it ( "with child path" , ( ) => { const item = enzyme . render ( < RootComponent wcmmode = "disabled" aemContext = { aemContext } comp = { createContainer ( "childClass" , null , "children" ) } path = "/content" / > ) ; let child = item . find ( ".test" ) ; expect ( child [ 0 ] . children [ 0 ] . data ) . to . equal ( "OOPS" ) ; } ) ; } ) ; } ) ;