


module("can/view");

/*test("Ajax transport", function(){
	var order = 0;
	$.ajax({
		url: "//can/view/test/qunit/template.ejs",
		dataType : "view",
		async : false
	}).done(function(view){
		equals(++order,1, "called synchronously");
		equals(view({message: "hi"}).indexOf("<h3>hi</h3>"), 0, "renders stuff!")
	});
	
	equals(++order,2, "called synchronously");
})*/

if(window.Jaml){
	test("multiple template types work", function(){
		
		can.each(["micro","ejs","jaml"/*, "tmpl"*/], function(i, ext){
			var div = can.$(document.createElement('div'));
				
			can.append(div, can.view("//can/view/test/qunit/template."+ext,{"message" :"helloworld"}))
			
			
			ok( div[0].getElementsByTagName('h3').length, ext+": h3 written for ")
			ok( /helloworld\s*/.test( div[0].innerHTML ), ext+": hello world present for ")
		})
	});
}

test("plugin in ejs", function(){

	can.append( can.$("#qunit-test-area"), can.view("//can/view/test/qunit/plugin.ejs",{}) )
	ok(/something/.test( can.$("#something span")[0].firstChild.nodeValue ),"something has something");
	can.remove( can.$("#something") );
});



test("async templates, and caching work", function(){
	stop();
	var i = 0;
	
	can.view.render("//can/view/test/qunit/temp.ejs",{"message" :"helloworld"}, function(text){
		ok(/helloworld\s*/.test(text), "we got a rendered template");
		i++;
		equals(i, 2, "Ajax is not synchronous");
		start();
	});
	i++;
	equals(i, 1, "Ajax is not synchronous")
})
test("caching works", function(){
	// this basically does a large ajax request and makes sure 
	// that the second time is always faster
	stop();
	var startT = new Date(),
		first;
	can.view.render("//can/view/test/qunit/large.ejs",{"message" :"helloworld"}, function(text){
		first = new Date();
		ok(text, "we got a rendered template");
		
		
		can.view.render("//can/view/test/qunit/large.ejs",{"message" :"helloworld"}, function(text){
			var lap2 = (new Date()) - first,
				lap1 =  first-startT;
			// ok( lap1 > lap2, "faster this time "+(lap1 - lap2) )
			
			start();
		})
		
	})
})
test("hookup", function(){

	can.view("//can/view/test/qunit/hookup.ejs",{})

})

test("inline templates other than 'tmpl' like ejs", function(){

        can.append( can.$("#qunit-test-area") ,'<script type="test/ejs" id="test_ejs"><span id="new_name"><%= name %></span></script>');
	
		var div = document.createElement('div');
		div.appendChild(can.view('test_ejs', {name: 'Henry'}))

        equal( div.getElementsByTagName("span")[0].firstChild.nodeValue , 'Henry');

});

test("object of deferreds", function(){
	var foo = new can.Deferred(),
		bar = new can.Deferred();
	stop();
	can.view.render("//can/view/test/qunit/deferreds.ejs",{
		foo : typeof foo.promise == 'function' ? foo.promise() : foo,
		bar : bar
	}).then(function(result){
		equals(result, "FOO and BAR");
		start();
	});
	setTimeout(function(){
		foo.resolve("FOO");
	},100);
	bar.resolve("BAR");
	
});

test("deferred", function(){
	var foo = new can.Deferred();
	stop();
	can.view.render("//can/view/test/qunit/deferred.ejs",foo).then(function(result){
		equals(result, "FOO");
		start();
	});
	setTimeout(function(){
		foo.resolve({
			foo: "FOO"
		});
	},100);
	
});

/*test("bad url", function(){
	can.render("//asfdsaf/sadf.ejs")
});*/

test("hyphen in type", function(){
	
	can.append( can.$("#qunit-test-area") ,"<script type='text/x-ejs' id='hyphenEjs'>\nHyphen\n</script>");
	
	var div = document.createElement('div');
	div.appendChild(can.view('hyphenEjs', {}))

    ok( /Hyphen/.test(div.innerHTML) , 'has hyphen');

})



@@ -2189,149 +2189,260 @@
 ()%7B%0A
-%0A        can.append( can.$(%22#qunit-test-area%22) ,'%3Cscript type=%22test/ejs%22 id=%22test_ejs%22%3E%3Cspan id=%22new_name%22%3E%3C%25= name %25%3E%3C/span%3E%3C/
+%09var script = document.createElement('script');%0A%09script.setAttribute('type', 'test/ejs')%0A%09script.setAttribute('id', 'test_ejs')%0A%09script.text = '%3Cspan id=%22new_name%22%3E%3C%25= name %25%3E%3C/span%3E';%0A%09document.getElementById(%22qunit-test-area%22).appendChild(
 script
-%3E'
 );%0A%09%0A
-%09
 %09var
@@ -2479,17 +2479,16 @@
 'div');%0A
-%09
 %09div.app
@@ -2540,16 +2540,9 @@
 ))%0A%0A
-        
+%09
 equa
@@ -2613,17 +2613,16 @@
 enry');%0A
-%0A
 %7D);%0A%0Ates

Fix for 'Dojo/can/view: inline templates other than 'tmpl' like ejs' in IE8