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