index.html revision 21a3d9293597d65e84324fe57b7158e3d67a46c1
66900b43227bf441b6a6b1084af2c96ce2c747c8Allen Rabinovich<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
66900b43227bf441b6a6b1084af2c96ce2c747c8Allen Rabinovich<html>
66900b43227bf441b6a6b1084af2c96ce2c747c8Allen Rabinovich<head>
66900b43227bf441b6a6b1084af2c96ce2c747c8Allen Rabinovich<title>DataSource Tests</title>
66900b43227bf441b6a6b1084af2c96ce2c747c8Allen Rabinovich<script type="text/javascript" src="/build/yui/yui.js"></script>
66900b43227bf441b6a6b1084af2c96ce2c747c8Allen Rabinovich<script type="text/javascript" src="/build/datasource/datasource-debug.js"></script>
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich</head>
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich<body class="yui-skin-sam">
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich<h1>DataSource Tests</h1>
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich<script type="text/javascript">
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich(function() {
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich YUI({
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich base: "/build/",
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich //filter: "debug",
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich logInclude:{"TestRunner":true,"datasource":true},
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich useConsole: true
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich }).use("dump", "test", "console", "io-base", "cache", "base", "plugin", "json", "datatype", "dataschema", "datasource", function(Y) {
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich var ASSERT = Y.Assert,
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich ARRAYASSERT = Y.ArrayAssert,
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich OBJECTASSERT = Y.ObjectAssert,
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich BTNRUN = Y.get("#btnRun"),
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich WAITTIMEOUT = 5000; // On a slow connection set to 5000
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich // Set up the page
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich BTNRUN.set("disabled", false);
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich Y.on("click", function() {
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich Y.Test.Runner.run();
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich }, BTNRUN);
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich var myConsole = new Y.Console().render();
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich var testClass = new Y.Test.Case({
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich name: "Class Tests",
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich testConstructor: function() {
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich var ds = new Y.DataSource.Local();
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.areSame((ds instanceof Y.Base), true, "Expected Base instance.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.areSame((ds instanceof Y.DataSource.Local), true, "Expected Local instance.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich },
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich testConstructorXHR: function() {
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich var ds = new Y.DataSource.XHR();
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.areSame((ds instanceof Y.Base), true, "Expected Base instance.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.areSame((ds instanceof Y.DataSource.Local), true, "Expected Local instance.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.areSame((ds instanceof Y.DataSource.XHR), true, "Expected XHR instance.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich }
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich });
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich var testLocal = new Y.Test.Case({
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich name: "DataSource.Local Tests",
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich testLocalDefaults: function() {
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich var ds = new Y.DataSource.Local({
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich source: ["a","b","c","d"]
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich });
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich var handler = Y.Mock();
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich Y.Mock.expect(handler,{
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich method: "handleSuccess",
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich args: [Y.Mock.Value(function(e){
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ARRAYASSERT.itemsAreSame(["a","b","c","d"], e.response.results, "Expected live array.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich })]
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich });
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ds.sendRequest(null, {
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich success: handler.handleSuccess,
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich failure: function(e) {
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.fail("XHR failure case.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich }
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich });
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich Y.Mock.verify(handler);
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich },
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich testLocalEvents: function() {
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich var ds = new Y.DataSource.Local({
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich source: ["a","b","c","d"]
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich });
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich var handler = Y.Mock();
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich Y.Mock.expect(handler,{
ec749fdeacb8e11cb7a9dd6dcebd1538b953059eAllen Rabinovich method: "handleRequest",
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich args: [Y.Mock.Value(function(e){
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.isNumber(e.tId, "request: Expected transaction ID.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.areSame("a", e.request, "request: Expected request.");
70837a7c5871ea73a84ab67d8c34fcd9d2daa31cAllen Rabinovich ASSERT.areSame("callback", e.callback, "request: Expected callback.");
66900b43227bf441b6a6b1084af2c96ce2c747c8Allen Rabinovich })]
7211944935145abdba8a3ea3b17621ec264d8e21Allen Rabinovich });
ds.subscribe("request", handler.handleRequest);
Y.Mock.expect(handler,{
method: "handleData",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "data: Expected transaction ID.");
ASSERT.areSame("a", e.request, "data: Expected request.");
ASSERT.areSame("callback", e.callback, "data: Expected callback.");
ASSERT.isArray(e.data, "data: Expected raw data.");
})]
});
ds.subscribe("data", handler.handleData);
Y.Mock.expect(handler,{
method: "handleResponse",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "response: Expected transaction ID.");
ASSERT.areSame("a", e.request, "response: Expected request.");
ASSERT.areSame("callback", e.callback, "response: Expected callback.");
ASSERT.isArray(e.data, "response: Expected raw data.");
ASSERT.isObject(e.response, "response: Expected normalized response object.");
ASSERT.isArray(e.response.results, "response: Expected parsed results.");
ASSERT.isObject(e.response.meta, "response: Expected parsed meta data.");
})]
});
ds.subscribe("response", handler.handleResponse);
ds.sendRequest("a", "callback");
Y.Mock.verify(handler);
},
testLocalError: function() {
var ds = new Y.DataSource.Local({
source: ["a","b","c","d"]
});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleError",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "error: Expected transaction ID.");
ASSERT.areSame("a", e.request, "error: Expected request.");
ASSERT.areSame("callback", e.callback, "error: Expected callback.");
ASSERT.isUndefined(e.response, "error: Expected undefined response.");
ASSERT.isObject(e.error, "error: Expected error.");
})]
});
ds.subscribe("error", handler.handleError);
ds.set("source", undefined);
ds.sendRequest("a", "callback");
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
}
});
var testXHR = new Y.Test.Case({
name: "DataSource.XHR Tests",
testXHRDefaults: function() {
var ds = new Y.DataSource.XHR({
source: "/php/ysearch_json_madonna.php"
});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value(function(e){
ASSERT.isNull(e.request, "Expected null request.");
ASSERT.isObject(e.response, "Expected response object.");
OBJECTASSERT.ownsKeys({tId:null,request:null,data:null,response:null,callback:null}, e, "Expected all properties.");
})]
});
ds.sendRequest(null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("XHR failure case.");
}
});
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testXHRPost: function() {
var ds = new Y.DataSource.XHR({
source: "/php/ysearch_json_madonna.php"
});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value(function(e){
ASSERT.isNull(e.request, "Expected null request.");
ASSERT.isObject(e.response, "Expected response object.");
OBJECTASSERT.ownsKeys({tId:null,request:null,data:null,response:null,callback:null}, e, "Expected all properties.");
})]
});
ds.sendRequest(null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("XHR failure case.");
}
},{
method: "POST",
data: "foo=bar"
});
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testXHREvents: function() {
var ds = new Y.DataSource.XHR({
source: "/php/ysearch_json_madonna.php"
});
ds.plug({fn: Y.Plugin.DataSourceJSONSchema, cfg: {
schema: {
resultListLocator: "ResultSet.Result",
resultFields: ["Title"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleRequest",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "request: Expected transaction ID.");
ASSERT.areSame(null, e.request, "request: Expected request.");
ASSERT.areSame("callback", e.callback, "request: Expected callback.");
})]
});
ds.subscribe("request", handler.handleRequest);
Y.Mock.expect(handler,{
method: "handleData",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "data: Expected transaction ID.");
ASSERT.areSame(null, e.request, "data: Expected request.");
ASSERT.areSame("callback", e.callback, "data: Expected callback.");
ASSERT.isObject(e.data, "data: Expected raw data.");
})]
});
ds.subscribe("data", handler.handleData);
Y.Mock.expect(handler,{
method: "handleResponse",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "response: Expected transaction ID.");
ASSERT.areSame(null, e.request, "response: Expected request.");
ASSERT.areSame("callback", e.callback, "response: Expected callback.");
ASSERT.isObject(e.data, "response: Expected raw data.");
ASSERT.isObject(e.response, "response: Expected normalized response object.");
ASSERT.isArray(e.response.results, "response: Expected parsed results.");
ASSERT.isObject(e.response.meta, "response: Expected parsed meta data.");
})]
});
ds.subscribe("response", handler.handleResponse);
ds.sendRequest(null, "callback");
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testXHRError: function() {
var ds = new Y.DataSource.XHR({
source: "/php/ysearch_json_madonna.php"
});
ds.plug({fn: Y.Plugin.DataSourceJSONSchema, cfg: {
schema: {
resultListLocator: "ResultSet.Result",
resultFields: ["Title"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleError",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "error: Expected transaction ID.");
ASSERT.areSame("a", e.request, "error: Expected request.");
ASSERT.areSame("callback", e.callback, "error: Expected callback.");
ASSERT.isObject(e.data, "error: Expected raw data.");
ASSERT.isObject(e.error, "error: Expected error.");
})]
});
ds.subscribe("error", handler.handleError);
ds.set("source", "foo");
ds.sendRequest("a", "callback");
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
}
});
var testScriptNode = new Y.Test.Case({
name: "DataSource.ScriptNode Tests",
testScriptNodeDefaults: function() {
var ds = new Y.DataSource.ScriptNode({
source: "http://query.yahooapis.com/v1/public/yql?format=json&"
});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value(function(e){
ASSERT.areSame("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", e.request, "Expected same request.");
ASSERT.isObject(e.response, "Expected response object.");
OBJECTASSERT.ownsKeys({tId:null,request:null,data:null,response:null,callback:null}, e, "Expected all properties.");
})]
});
ds.sendRequest("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("ScriptNode failure case.");
}
});
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testScriptNodeEvents: function() {
var ds = new Y.DataSource.ScriptNode({
source: "http://query.yahooapis.com/v1/public/yql?format=json&"
});
ds.plug({fn: Y.Plugin.DataSourceJSONSchema, cfg: {
schema: {
resultListLocator: "query.results.result",
resultFields: ["title"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleRequest",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "request: Expected transaction ID.");
ASSERT.areSame("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", e.request, "Expected same request.");
ASSERT.areSame("callback", e.callback, "request: Expected callback.");
})]
});
ds.subscribe("request", handler.handleRequest);
Y.Mock.expect(handler,{
method: "handleData",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "data: Expected transaction ID.");
ASSERT.areSame("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", e.request, "Expected same request.");
ASSERT.areSame("callback", e.callback, "data: Expected callback.");
ASSERT.isObject(e.data, "data: Expected raw data.");
})]
});
ds.subscribe("data", handler.handleData);
Y.Mock.expect(handler,{
method: "handleResponse",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "response: Expected transaction ID.");
ASSERT.areSame("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", e.request, "Expected same request.");
ASSERT.areSame("callback", e.callback, "response: Expected callback.");
ASSERT.isObject(e.data, "response: Expected raw data.");
ASSERT.isObject(e.response, "response: Expected normalized response object.");
ASSERT.isArray(e.response.results, "response: Expected parsed results.");
ASSERT.isObject(e.response.meta, "response: Expected parsed meta data.");
})]
});
ds.subscribe("response", handler.handleResponse);
ds.sendRequest("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", "callback");
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
// Only FF can detect error state in Get Utility...
testScriptNodeError: (Y.UA.gecko) ? function() {
var ds = new Y.DataSource.ScriptNode({
source: "http://query.yahooapis.com/v1/public/yql?format=json&"
});
ds.plug({fn: Y.Plugin.DataSourceJSONSchema, cfg: {
schema: {
resultListLocator: "query.results.result",
resultFields: ["title"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleError",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "error: Expected transaction ID.");
ASSERT.areSame("a", e.request, "error: Expected request.");
ASSERT.areSame("callback", e.callback, "error: Expected callback.");
ASSERT.isUndefined(e.data, "error: Expected undefined data.");
ASSERT.isObject(e.error, "error: Expected error.");
})]
});
ds.subscribe("error", handler.handleError);
ds.set("source", "foo");
ds.sendRequest("a", "callback");
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
} : function(){}
});
var testFunction = new Y.Test.Case({
name: "DataSource.Function Tests",
testFunctionDefaults: function() {
var ds = new Y.DataSource.Function({
source: function() {
return [{type:"a",age:0,name:"c"},{type:"d",age:1,name:"f"},{type:"g",age:-1,name:"i"}];
}
});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value(function(e){
ASSERT.areSame("foo", e.request, "Expected same request.");
ASSERT.isObject(e.response, "Expected response object.");
OBJECTASSERT.ownsKeys({tId:null,request:null,data:null,response:null,callback:null}, e, "Expected all properties.");
})]
});
ds.sendRequest("foo", {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("Function failure case.");
}
});
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testFunctionEvents: function() {
var ds = new Y.DataSource.Function({
source: function() {
return [{type:"a",age:0,name:"c"},{type:"d",age:1,name:"f"},{type:"g",age:-1,name:"i"}];
}
});
ds.plug({fn: Y.Plugin.DataSourceArraySchema, cfg: {
schema: {
resultFields: ["type", "name"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleRequest",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "request: Expected transaction ID.");
ASSERT.areSame("foo", e.request, "Expected same request.");
ASSERT.areSame("callback", e.callback, "request: Expected callback.");
})]
});
ds.subscribe("request", handler.handleRequest);
Y.Mock.expect(handler,{
method: "handleData",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "data: Expected transaction ID.");
ASSERT.areSame("foo", e.request, "Expected same request.");
ASSERT.areSame("callback", e.callback, "data: Expected callback.");
ASSERT.isObject(e.data, "data: Expected raw data.");
})]
});
ds.subscribe("data", handler.handleData);
Y.Mock.expect(handler,{
method: "handleResponse",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "response: Expected transaction ID.");
ASSERT.areSame("foo", e.request, "Expected same request.");
ASSERT.areSame("callback", e.callback, "response: Expected callback.");
ASSERT.isObject(e.data, "response: Expected raw data.");
ASSERT.isObject(e.response, "response: Expected normalized response object.");
ASSERT.isArray(e.response.results, "response: Expected parsed results.");
ASSERT.isObject(e.response.meta, "response: Expected parsed meta data.");
})]
});
ds.subscribe("response", handler.handleResponse);
ds.sendRequest("foo", "callback");
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testFunctionError: function() {
var ds = new Y.DataSource.Function({
source: "foo"
});
ds.plug({fn: Y.Plugin.DataSourceArraySchema, cfg: {
schema: {
resultFields: ["type", "name"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleError",
args: [Y.Mock.Value(function(e){
ASSERT.isNumber(e.tId, "error: Expected transaction ID.");
ASSERT.areSame("a", e.request, "error: Expected request.");
ASSERT.areSame("callback", e.callback, "error: Expected callback.");
ASSERT.isUndefined(e.data, "error: Expected undefined data.");
ASSERT.isObject(e.error, "error: Expected error.");
})]
});
ds.subscribe("error", handler.handleError);
ds.sendRequest("a", "callback");
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
}
});
var testDataSchemaPlugin = new Y.Test.Case({
name: "DataSource DataSchema Plugin Tests",
testJSONSchema: function() {
var ds = new Y.DataSource.XHR({
source: "/php/ysearch_json_madonna.php"
});
ds.plug({fn: Y.Plugin.DataSourceJSONSchema, cfg: {
schema: {
resultListLocator: "ResultSet.Result",
resultFields: ["Title"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value(function(e){
ASSERT.isNull(e.request, "Expected null request.");
ASSERT.isObject(e.response, "Expected normalized response object.");
ASSERT.isArray(e.response.results, "Expected results array.");
ASSERT.areSame(10, e.response.results.length, "Expected 10 results.")
ASSERT.isNotUndefined(e.response.results[0].Title, "Expected Title property")
})]
});
ds.sendRequest(null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("XHR failure case.");
},
scope: this
});
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testXMLSchema: function() {
var ds = new Y.DataSource.XHR({
source: "/php/ysearch_xml_madonna.php"
});
ds.plug({fn: Y.Plugin.DataSourceXMLSchema, cfg: {
schema: {
resultListLocator: "result",
resultFields: [{key:"title", locator:"*[local-name() ='title']"}]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value(function(e){
ASSERT.isNull(e.request, "Expected null request.");
ASSERT.isObject(e.response, "Expected normalized response object.");
ASSERT.isArray(e.response.results, "Expected results array.");
ASSERT.areSame(10, e.response.results.length, "Expected 10 results.")
ASSERT.isString(e.response.results[0].title, "Expected title.")
})]
});
ds.sendRequest(null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("XHR failure case.");
},
scope: this
});
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testArraySchema: function() {
var ds = new Y.DataSource.Local({
source: [{type:"a",age:0,name:"c"},{type:"d",age:1,name:"f"},{type:"g",age:-1,name:"i"}]
});
ds.plug({fn: Y.Plugin.DataSourceArraySchema, cfg: {
schema: {
resultFields: ["type", "name"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value(function(e){
ASSERT.isNull(e.request, "Expected null request.");
ASSERT.isObject(e.response, "Expected normalized response object.");
ASSERT.isArray(e.response.results, "Expected results array.");
ASSERT.areSame(3, e.response.results.length, "Expected 3 results.")
ASSERT.areSame("a", e.response.results[0].type, "Expected first type.");
ASSERT.areSame("g", e.response.results[2].type, "Expected last type.");
ASSERT.areSame("c", e.response.results[0].name, "Expected first name.");
ASSERT.areSame("i", e.response.results[2].name, "Expected last name.");
ASSERT.isUndefined(e.response.results[0].age, "Expected no age on first result.");
ASSERT.isUndefined(e.response.results[2].age, "Expected no age on last result.");
})]
});
ds.sendRequest(null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("Local failure case.");
},
scope: this
});
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
},
testTextSchema: function() {
var ds = new Y.DataSource.Local({
source: "foo\t0\tabc\nbar\t1\tdef\nbat\t-1\tghi"
});
ds.plug({fn: Y.Plugin.DataSourceTextSchema, cfg: {
schema: {
resultDelimiter: "\n",
fieldDelimiter: "\t",
resultFields: [{key:"type"}, {key:"age", parser:"number"}, "name"]
}
}});
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value(function(e){
ASSERT.isNull(e.request, "Expected null request.");
ASSERT.isObject(e.response, "Expected normalized response object.");
ASSERT.isArray(e.response.results, "Expected results array.");
ASSERT.areSame(3, e.response.results.length, "Expected 3 results.")
ASSERT.areSame("foo", e.response.results[0].type, "Expected first type.");
ASSERT.areSame("bat", e.response.results[2].type, "Expected last type.");
ASSERT.areSame(0, e.response.results[0].age, "Expected first age.");
ASSERT.areSame(-1, e.response.results[2].age, "Expected last age.");
ASSERT.areSame("abc", e.response.results[0].name, "Expected first name.");
ASSERT.areSame("ghi", e.response.results[2].name, "Expected last name.");
})]
});
ds.sendRequest(null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("Local failure case.");
},
scope: this
});
this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
}
});
var testCaching = new Y.Test.Case({
name: "DataSource Caching Tests",
testCacheDefaultMax: function() {
var ds = new Y.DataSource.Local();
ds.plug(Y.Plugin.DataSourceCache);
ASSERT.areSame((ds.cache instanceof Y.Cache), true, "Expected Cache instance.");
ASSERT.areSame(ds.cache.get("max"), 0, "Expected 0 max in Cache.");
},
testCacheInitMax: function() {
var ds = new Y.DataSource.Local();
ds.plug({fn:Y.Plugin.DataSourceCache, cfg:{max:3}});
ASSERT.areSame((ds.cache instanceof Y.Cache), true, "Expected Cache instance.");
ASSERT.areSame(ds.cache.get("max"), 3, "Expected 3 max in Cache.");
},
testCacheSetMax: function() {
var ds = new Y.DataSource.Local();
ds.plug({fn:Y.Plugin.DataSourceCache});
ds.cache.set("max", 5);
ASSERT.areSame((ds.cache instanceof Y.Cache), true, "Expected Cache instance.");
ASSERT.areSame(ds.cache.get("max"), 5, "Expected 5 max in Cache.");
},
testLocalCache: function() {
var ds = new Y.DataSource.Local({
source: ["a","b","c","d"]
});
ds.plug({fn:Y.Plugin.DataSourceCache, cfg:{max:3}});
ds.sendRequest("a");
ds.subscribe("data", function(e) {
ASSERT.fail("Entry should be cached -- 'data' event is unexpected");
});
ds.sendRequest("a");
},
testLocalCacheUnplug: function() {
var ds = new Y.DataSource.Local({
source: ["a","b","c","d"]
});
ds.plug({fn:Y.Plugin.DataSourceCache, cfg:{max:3}});
ds.sendRequest("a");
ds.cache.subscribe("retrieve", function(e) {
ASSERT.fail("Cache is unset -- 'retrieve' event is unexpected");
});
ds.unplug("cache");
ds.sendRequest("a");
}
});
var testPolling = new Y.Test.Case({
name: "DataSource Polling Tests",
testClass: function() {
var ds = new Y.DataSource.Local();
ASSERT.isNotUndefined((ds.setInterval), "Expected setInterval() method on Local.");
ASSERT.isNotUndefined((ds.clearInterval), "Expected clearInterval() method on Local.");
ds = new Y.DataSource.XHR();
ASSERT.isNotUndefined((ds.setInterval), "Expected setInterval() method on XHR.");
ASSERT.isNotUndefined((ds.clearInterval), "Expected clearInterval() method on XHR.");
},
testSetAndClear: function() {
var ds = new Y.DataSource.Local();
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value.Any],
callCount: 3
});
var id = ds.setInterval(500, null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("XHR failure case.");
}
});
ASSERT.isNumber(id, "Expected interval id.");
this.wait(function(){
ds.clearInterval(id);
this.wait(function(){
Y.Mock.verify(handler);
}, 1000);
}, 1950);
},
testClearAll: function() {
var ds = new Y.DataSource.Local();
var handler = Y.Mock();
Y.Mock.expect(handler,{
method: "handleSuccess",
args: [Y.Mock.Value.Any],
callCount: 3
});
ds.setInterval(500, null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("XHR failure case.");
}
});
ds.setInterval(500, null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("XHR failure case.");
}
});
ds.setInterval(500, null, {
success: handler.handleSuccess,
failure: function(e) {
ASSERT.fail("XHR failure case.");
}
});
this.wait(function(){
ds.clearAllIntervals();
this.wait(function(){
Y.Mock.verify(handler);
}, 500);
}, 950);
}
});
Y.Test.Runner.add(testClass);
Y.Test.Runner.add(testLocal);
Y.Test.Runner.add(testXHR);
Y.Test.Runner.add(testScriptNode);
Y.Test.Runner.add(testFunction);
Y.Test.Runner.add(testDataSchemaPlugin);
Y.Test.Runner.add(testCaching);
Y.Test.Runner.add(testPolling);
Y.Test.Runner.run();
});
})();
</script>
</body>
</html>