import { expect } from "chai" ; import "./setup" ; import ServerSling from "../store/ServerSling" ; import Cache from "../store/Cache" ; import { JavaSling } from "../references" ; import { EditDialogData } from "../store/Sling" ; import ClientSling from "../store/ClientSling" ; import { ResourceComponent } from "../component/ResourceComponent" ; describe ( "ClientSling" , ( ) => { it ( "should include resource" , ( ) => { let html = "<div></div>" ; let cache = new Cache ( ) ; let sling = new ClientSling ( cache , null ) ; cache . putIncluded ( "/test" , html ) ; let actualHtml = sling . includeResource ( "/test" , "/component/test" ) ; expect ( actualHtml ) . to . equal ( html ) ; } ) ; it ( "should include dialog" , ( ) => { let dialog = { element : "el" } ; let cache = new Cache ( ) ; let sling = new ClientSling ( cache , null ) ; cache . putScript ( "/test" , dialog ) ; let actualDialog = sling . renderDialogScript ( "/test" , "/component/test" ) ; expect ( actualDialog ) . to . deep . equal ( dialog ) ; } ) ; it ( "should subscribe to cached resource" , ( ) => { let resource = { } ; let cache = new Cache ( ) ; let sling = new ClientSling ( cache , null ) ; let path = "/test" ; cache . put ( path , resource ) ; let actualResource ; let actualPath ; let listener = new ( class MockResourceComponent extends ResourceComponent < any , any , any > { public changedResource ( _path , _resource ) { actualResource = _resource ; actualPath = _path ; } public renderBody ( ) { } } ) ( ) ; sling . subscribe ( listener , path , { depth : 1 } ) ; expect ( actualPath ) . to . equal ( path ) ; expect ( actualResource ) . to . equal ( resource ) ; } ) ; it ( "should subscribe to resource" , ( ) => { let actualUrl ; let resource = { data : { text : "hi" } , depth : 1 } ; let resources = { resources : { "/test" : resource } } ; let cache = new Cache ( ) ; let sling = new ClientSling ( cache , "/url" , { fetch : function ( url , options ) : any { actualUrl = url ; return { then : function ( cb ) : any { return cb ( { json : function ( ) : any { return { then : function ( cb2 ) : void { cb2 ( resources ) ; } } ; } } ) ; } } ; } } ) ; let path = "/test" ; let actualResource ; let actualPath ; let listener = { changedResource : function ( _path , _resource ) : void { actualResource = _resource ; actualPath = _path ; } } ; sling . subscribe ( ( listener as ResourceComponent < any , any , any > ) , path , { depth : 1 } ) ; expect ( actualPath ) . to . equal ( path ) ; expect ( actualUrl ) . to . equal ( "/url/test.json.html" ) ; expect ( actualResource ) . to . deep . equal ( resource . data ) ; } ) ; } ) ;