index.html revision 8da7b9f231bcb1e03e7870ad7f01008dde67b5fa
0N/A<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
160N/A<html>
0N/A<head>
0N/A<title>DataSource Tests</title>
0N/A<script type="text/javascript" src="/build/yui/yui.js"></script>
0N/A</head>
0N/A
0N/A<body class="yui-skin-sam">
0N/A<h1>DataSource Tests</h1>
0N/A<p><input type="button" value="Run Tests" id="btnRun" disabled="true" /></p>
0N/A
0N/A<script type="text/javascript">
0N/A
0N/A(function() {
0N/A YUI({
0N/A base: "/build/",
0N/A filter: "debug",
0N/A logInclude:{"TestRunner":true},
0N/A useConsole: true,
0N/A }).use("dump", "test", "console", "io-base", "cache", "base", "plugin", "json", "datatype", "dataschema", "datasource", function(Y) {
0N/A
0N/A var ASSERT = Y.Assert,
0N/A ARRAYASSERT = Y.ArrayAssert,
0N/A OBJECTASSERT = Y.ObjectAssert,
0N/A BTNRUN = Y.get("#btnRun"),
0N/A WAITTIMEOUT = 5000; // On a slow connection set to 5000
0N/A
0N/A // Set up the page
0N/A
0N/A BTNRUN.set("disabled", false);
0N/A Y.on("click", function() {
0N/A Y.Test.Runner.run();
0N/A }, BTNRUN);
0N/A var myConsole = new Y.Console().render();
0N/A
0N/A var testClass = new Y.Test.Case({
0N/A name: "Class Tests",
0N/A
0N/A testConstructor: function() {
0N/A var ds = new Y.DataSource.Local();
0N/A ASSERT.areSame((ds instanceof Y.Base), true, "Expected Base instance.");
0N/A ASSERT.areSame((ds instanceof Y.DataSource.Local), true, "Expected Local instance.");
0N/A },
0N/A
0N/A testConstructorXHR: function() {
0N/A var ds = new Y.DataSource.XHR();
0N/A ASSERT.areSame((ds instanceof Y.Base), true, "Expected Base instance.");
0N/A ASSERT.areSame((ds instanceof Y.DataSource.Local), true, "Expected Local instance.");
0N/A ASSERT.areSame((ds instanceof Y.DataSource.XHR), true, "Expected XHR instance.");
0N/A }
0N/A });
0N/A
0N/A var testLocal = new Y.Test.Case({
0N/A name: "DataSource.Local Tests",
0N/A
0N/A testLocalDefaults: function() {
0N/A var ds = new Y.DataSource.Local({
10N/A source: ["a","b","c","d"]
10N/A });
10N/A
10N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleSuccess",
0N/A args: [Y.Mock.Value(function(e){
0N/A ARRAYASSERT.itemsAreSame(["a","b","c","d"], e.response.results, "Expected live array.");
0N/A })]
0N/A });
0N/A
0N/A ds.sendRequest(null, {
0N/A success: handler.handleSuccess,
0N/A failure: function(e) {
0N/A ASSERT.fail("XHR failure case.");
0N/A }
0N/A });
0N/A
0N/A Y.Mock.verify(handler);
0N/A },
0N/A
0N/A testLocalEvents: function() {
0N/A var ds = new Y.DataSource.Local({
0N/A source: ["a","b","c","d"]
0N/A });
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleRequest",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "request: Expected transaction ID.");
0N/A ASSERT.areSame("a", e.request, "request: Expected request.");
0N/A ASSERT.areSame("callback", e.callback, "request: Expected callback.");
0N/A })]
0N/A });
0N/A ds.subscribe("request", handler.handleRequest);
0N/A
0N/A Y.Mock.expect(handler,{
0N/A method: "handleData",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "data: Expected transaction ID.");
0N/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.");
0N/A })]
0N/A });
0N/A ds.subscribe("data", handler.handleData);
0N/A
0N/A Y.Mock.expect(handler,{
0N/A method: "handleResponse",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "response: Expected transaction ID.");
0N/A ASSERT.areSame("a", e.request, "response: Expected request.");
0N/A ASSERT.areSame("callback", e.callback, "response: Expected callback.");
0N/A ASSERT.isArray(e.data, "response: Expected raw data.");
0N/A ASSERT.isObject(e.response, "response: Expected normalized response object.");
0N/A ASSERT.isArray(e.response.results, "response: Expected parsed results.");
0N/A ASSERT.isObject(e.response.meta, "response: Expected parsed meta data.");
0N/A })]
0N/A });
0N/A ds.subscribe("response", handler.handleResponse);
0N/A
0N/A ds.sendRequest("a", "callback");
0N/A Y.Mock.verify(handler);
0N/A },
0N/A
0N/A testLocalError: function() {
0N/A var ds = new Y.DataSource.Local({
0N/A source: ["a","b","c","d"]
0N/A });
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleError",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "error: Expected transaction ID.");
0N/A ASSERT.areSame("a", e.request, "error: Expected request.");
0N/A ASSERT.areSame("callback", e.callback, "error: Expected callback.");
0N/A ASSERT.isUndefined(e.response, "error: Expected undefined response.");
0N/A ASSERT.isObject(e.error, "error: Expected error.");
0N/A })]
0N/A });
0N/A ds.subscribe("error", handler.handleError);
0N/A
0N/A ds.set("source", undefined);
0N/A ds.sendRequest("a", "callback");
0N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A }
0N/A });
0N/A
0N/A var testXHR = new Y.Test.Case({
0N/A name: "DataSource.XHR Tests",
0N/A
0N/A testXHRDefaults: function() {
0N/A var ds = new Y.DataSource.XHR({
0N/A source: "/php/ysearch_json_madonna.php"
0N/A });
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleSuccess",
0N/A args: [Y.Mock.Value(function(e){
0N/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.");
0N/A })]
0N/A });
0N/A
0N/A ds.sendRequest(null, {
0N/A success: handler.handleSuccess,
0N/A failure: function(e) {
0N/A ASSERT.fail("XHR failure case.");
0N/A }
0N/A });
0N/A
0N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A },
0N/A
0N/A testXHREvents: function() {
0N/A var ds = new Y.DataSource.XHR({
0N/A source: "/php/ysearch_json_madonna.php"
0N/A });
0N/A ds.plug({fn: Y.plugin.DataSourceJSONSchema, cfg: {
0N/A schema: {
0N/A resultListLocator: "ResultSet.Result",
0N/A resultFields: ["Title"]
0N/A }
0N/A }});
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleRequest",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "request: Expected transaction ID.");
0N/A ASSERT.areSame(null, e.request, "request: Expected request.");
0N/A ASSERT.areSame("callback", e.callback, "request: Expected callback.");
0N/A })]
420N/A });
420N/A ds.subscribe("request", handler.handleRequest);
420N/A
420N/A Y.Mock.expect(handler,{
420N/A method: "handleData",
420N/A args: [Y.Mock.Value(function(e){
420N/A ASSERT.isNumber(e.tId, "data: Expected transaction ID.");
420N/A ASSERT.areSame(null, e.request, "data: Expected request.");
0N/A ASSERT.areSame("callback", e.callback, "data: Expected callback.");
0N/A ASSERT.isObject(e.data, "data: Expected raw data.");
0N/A })]
0N/A });
0N/A ds.subscribe("data", handler.handleData);
0N/A
0N/A Y.Mock.expect(handler,{
0N/A method: "handleResponse",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "response: Expected transaction ID.");
0N/A ASSERT.areSame(null, e.request, "response: Expected request.");
0N/A ASSERT.areSame("callback", e.callback, "response: Expected callback.");
0N/A ASSERT.isObject(e.data, "response: Expected raw data.");
0N/A ASSERT.isObject(e.response, "response: Expected normalized response object.");
0N/A ASSERT.isArray(e.response.results, "response: Expected parsed results.");
0N/A ASSERT.isObject(e.response.meta, "response: Expected parsed meta data.");
0N/A })]
0N/A });
0N/A ds.subscribe("response", handler.handleResponse);
0N/A
0N/A ds.sendRequest(null, "callback");
0N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A },
0N/A
0N/A testXHRError: function() {
0N/A var ds = new Y.DataSource.XHR({
0N/A source: "/php/ysearch_json_madonna.php"
0N/A });
0N/A ds.plug({fn: Y.plugin.DataSourceJSONSchema, cfg: {
0N/A schema: {
0N/A resultListLocator: "ResultSet.Result",
0N/A resultFields: ["Title"]
0N/A }
0N/A }});
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleError",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "error: Expected transaction ID.");
0N/A ASSERT.areSame("a", e.request, "error: Expected request.");
0N/A ASSERT.areSame("callback", e.callback, "error: Expected callback.");
0N/A ASSERT.isObject(e.data, "error: Expected raw data.");
0N/A ASSERT.isObject(e.error, "error: Expected error.");
0N/A })]
0N/A });
0N/A ds.subscribe("error", handler.handleError);
0N/A
0N/A ds.set("source", "foo");
0N/A ds.sendRequest("a", "callback");
0N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A }
0N/A });
0N/A
0N/A var testScriptNode = new Y.Test.Case({
0N/A name: "DataSource.ScriptNode Tests",
0N/A
0N/A testScriptNodeDefaults: function() {
0N/A var ds = new Y.DataSource.ScriptNode({
0N/A source: "http://query.yahooapis.com/v1/public/yql?format=json&"
0N/A });
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleSuccess",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.areSame("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", e.request, "Expected same request.");
167N/A ASSERT.isObject(e.response, "Expected response object.");
167N/A OBJECTASSERT.ownsKeys({tId:null,request:null,data:null,response:null,callback:null}, e, "Expected all properties.");
167N/A })]
0N/A });
0N/A
0N/A ds.sendRequest("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", {
0N/A success: handler.handleSuccess,
0N/A failure: function(e) {
0N/A ASSERT.fail("ScriptNode failure case.");
0N/A }
0N/A });
0N/A
0N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A },
0N/A
0N/A testScriptNodeEvents: function() {
0N/A var ds = new Y.DataSource.ScriptNode({
0N/A source: "http://query.yahooapis.com/v1/public/yql?format=json&"
0N/A });
0N/A ds.plug({fn: Y.plugin.DataSourceJSONSchema, cfg: {
0N/A schema: {
0N/A resultListLocator: "query.results.result",
0N/A resultFields: ["title"]
0N/A }
0N/A }});
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleRequest",
0N/A args: [Y.Mock.Value(function(e){
0N/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 })]
0N/A });
0N/A ds.subscribe("request", handler.handleRequest);
0N/A
0N/A Y.Mock.expect(handler,{
0N/A method: "handleData",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "data: 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, "data: Expected callback.");
0N/A ASSERT.isObject(e.data, "data: Expected raw data.");
0N/A })]
0N/A });
0N/A ds.subscribe("data", handler.handleData);
0N/A
0N/A Y.Mock.expect(handler,{
0N/A method: "handleResponse",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "response: 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, "response: Expected callback.");
0N/A ASSERT.isObject(e.data, "response: Expected raw data.");
0N/A ASSERT.isObject(e.response, "response: Expected normalized response object.");
0N/A ASSERT.isArray(e.response.results, "response: Expected parsed results.");
0N/A ASSERT.isObject(e.response.meta, "response: Expected parsed meta data.");
0N/A })]
0N/A });
0N/A ds.subscribe("response", handler.handleResponse);
0N/A
0N/A ds.sendRequest("q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22", "callback");
0N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A },
0N/A
0N/A // Only FF can detect error state in Get Utility...
0N/A testScriptNodeError: (Y.UA.gecko) ? function() {
0N/A var ds = new Y.DataSource.ScriptNode({
0N/A source: "http://query.yahooapis.com/v1/public/yql?format=json&"
0N/A });
0N/A ds.plug({fn: Y.plugin.DataSourceJSONSchema, cfg: {
0N/A schema: {
0N/A resultListLocator: "query.results.result",
0N/A resultFields: ["title"]
0N/A }
0N/A }});
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleError",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "error: Expected transaction ID.");
0N/A ASSERT.areSame("a", e.request, "error: Expected request.");
0N/A ASSERT.areSame("callback", e.callback, "error: Expected callback.");
0N/A ASSERT.isUndefined(e.data, "error: Expected undefined data.");
0N/A ASSERT.isObject(e.error, "error: Expected error.");
0N/A })]
0N/A });
0N/A ds.subscribe("error", handler.handleError);
0N/A
0N/A ds.set("source", "foo");
0N/A ds.sendRequest("a", "callback");
0N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A } : function(){}
0N/A });
0N/A
0N/A var testFunction = new Y.Test.Case({
533N/A name: "DataSource.Function Tests",
533N/A
0N/A testFunctionDefaults: function() {
0N/A var ds = new Y.DataSource.Function({
0N/A source: function() {
533N/A return [{type:"a",age:0,name:"c"},{type:"d",age:1,name:"f"},{type:"g",age:-1,name:"i"}];
533N/A }
0N/A });
533N/A
533N/A var handler = Y.Mock();
533N/A Y.Mock.expect(handler,{
533N/A method: "handleSuccess",
533N/A args: [Y.Mock.Value(function(e){
533N/A ASSERT.areSame("foo", e.request, "Expected same request.");
533N/A ASSERT.isObject(e.response, "Expected response object.");
533N/A OBJECTASSERT.ownsKeys({tId:null,request:null,data:null,response:null,callback:null}, e, "Expected all properties.");
533N/A })]
533N/A });
533N/A
533N/A ds.sendRequest("foo", {
533N/A success: handler.handleSuccess,
533N/A failure: function(e) {
533N/A ASSERT.fail("Function failure case.");
533N/A }
533N/A });
533N/A
533N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
533N/A },
533N/A
533N/A testFunctionEvents: function() {
533N/A var ds = new Y.DataSource.Function({
533N/A source: function() {
533N/A return [{type:"a",age:0,name:"c"},{type:"d",age:1,name:"f"},{type:"g",age:-1,name:"i"}];
533N/A }
533N/A });
533N/A ds.plug({fn: Y.plugin.DataSourceArraySchema, cfg: {
533N/A schema: {
533N/A resultFields: ["type", "name"]
533N/A }
533N/A }});
533N/A
533N/A var handler = Y.Mock();
533N/A Y.Mock.expect(handler,{
533N/A method: "handleRequest",
533N/A args: [Y.Mock.Value(function(e){
533N/A ASSERT.isNumber(e.tId, "request: Expected transaction ID.");
533N/A ASSERT.areSame("foo", e.request, "Expected same request.");
533N/A ASSERT.areSame("callback", e.callback, "request: Expected callback.");
533N/A })]
533N/A });
533N/A ds.subscribe("request", handler.handleRequest);
533N/A
533N/A Y.Mock.expect(handler,{
533N/A method: "handleData",
533N/A args: [Y.Mock.Value(function(e){
533N/A ASSERT.isNumber(e.tId, "data: Expected transaction ID.");
533N/A ASSERT.areSame("foo", e.request, "Expected same request.");
533N/A ASSERT.areSame("callback", e.callback, "data: Expected callback.");
533N/A ASSERT.isObject(e.data, "data: Expected raw data.");
533N/A })]
533N/A });
533N/A ds.subscribe("data", handler.handleData);
533N/A
533N/A Y.Mock.expect(handler,{
0N/A method: "handleResponse",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNumber(e.tId, "response: Expected transaction ID.");
0N/A ASSERT.areSame("foo", e.request, "Expected same request.");
0N/A ASSERT.areSame("callback", e.callback, "response: Expected callback.");
0N/A ASSERT.isObject(e.data, "response: Expected raw data.");
0N/A ASSERT.isObject(e.response, "response: Expected normalized response object.");
0N/A ASSERT.isArray(e.response.results, "response: Expected parsed results.");
0N/A ASSERT.isObject(e.response.meta, "response: Expected parsed meta data.");
0N/A })]
0N/A });
0N/A ds.subscribe("response", handler.handleResponse);
0N/A
0N/A ds.sendRequest("foo", "callback");
0N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A },
0N/A
0N/A testFunctionError: function() {
0N/A var ds = new Y.DataSource.Function({
0N/A source: "foo"
0N/A });
0N/A ds.plug({fn: Y.plugin.DataSourceArraySchema, cfg: {
0N/A schema: {
0N/A resultFields: ["type", "name"]
0N/A }
10N/A }});
10N/A
10N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleError",
10N/A args: [Y.Mock.Value(function(e){
10N/A ASSERT.isNumber(e.tId, "error: Expected transaction ID.");
10N/A ASSERT.areSame("a", e.request, "error: Expected request.");
10N/A ASSERT.areSame("callback", e.callback, "error: Expected callback.");
10N/A ASSERT.isUndefined(e.data, "error: Expected undefined data.");
10N/A ASSERT.isObject(e.error, "error: Expected error.");
10N/A })]
10N/A });
10N/A ds.subscribe("error", handler.handleError);
10N/A
10N/A ds.sendRequest("a", "callback");
10N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
0N/A }
10N/A });
0N/A
10N/A var testDataSchemaPlugin = new Y.Test.Case({
0N/A name: "DataSource DataSchema Plugin Tests",
0N/A
160N/A testJSONSchema: function() {
160N/A var ds = new Y.DataSource.XHR({
160N/A source: "/php/ysearch_json_madonna.php"
160N/A });
160N/A ds.plug({fn: Y.plugin.DataSourceJSONSchema, cfg: {
160N/A schema: {
160N/A resultListLocator: "ResultSet.Result",
10N/A resultFields: ["Title"]
10N/A }
10N/A }});
10N/A
10N/A var handler = Y.Mock();
160N/A Y.Mock.expect(handler,{
160N/A method: "handleSuccess",
10N/A args: [Y.Mock.Value(function(e){
10N/A ASSERT.isNull(e.request, "Expected null request.");
10N/A ASSERT.isObject(e.response, "Expected normalized response object.");
10N/A ASSERT.isArray(e.response.results, "Expected results array.");
10N/A ASSERT.areSame(10, e.response.results.length, "Expected 10 results.")
10N/A ASSERT.isNotUndefined(e.response.results[0].Title, "Expected Title property")
10N/A })]
10N/A });
10N/A
10N/A ds.sendRequest(null, {
10N/A success: handler.handleSuccess,
10N/A failure: function(e) {
10N/A ASSERT.fail("XHR failure case.");
10N/A },
10N/A scope: this
10N/A });
10N/A
10N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
10N/A },
0N/A
0N/A testXMLSchema: function() {
0N/A var ds = new Y.DataSource.XHR({
0N/A source: "/php/ysearch_xml_madonna.php"
0N/A });
0N/A ds.plug({fn: Y.plugin.DataSourceXMLSchema, cfg: {
0N/A schema: {
0N/A resultListLocator: "result",
0N/A resultFields: [{key:"title", locator:"*[local-name() ='title']"}]
0N/A }
0N/A }});
0N/A
0N/A var handler = Y.Mock();
0N/A Y.Mock.expect(handler,{
0N/A method: "handleSuccess",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNull(e.request, "Expected null request.");
0N/A ASSERT.isObject(e.response, "Expected normalized response object.");
0N/A ASSERT.isArray(e.response.results, "Expected results array.");
0N/A ASSERT.areSame(10, e.response.results.length, "Expected 10 results.")
0N/A ASSERT.isString(e.response.results[0].title, "Expected title.")
0N/A })]
0N/A });
0N/A
0N/A ds.sendRequest(null, {
0N/A success: handler.handleSuccess,
0N/A failure: function(e) {
0N/A ASSERT.fail("XHR failure case.");
0N/A },
0N/A scope: this
117N/A });
117N/A
117N/A this.wait(function(){Y.Mock.verify(handler);}, WAITTIMEOUT);
117N/A },
117N/A
117N/A testArraySchema: function() {
117N/A var ds = new Y.DataSource.Local({
117N/A source: [{type:"a",age:0,name:"c"},{type:"d",age:1,name:"f"},{type:"g",age:-1,name:"i"}]
117N/A });
117N/A ds.plug({fn: Y.plugin.DataSourceArraySchema, cfg: {
117N/A schema: {
117N/A resultFields: ["type", "name"]
117N/A }
117N/A }});
117N/A
117N/A var handler = Y.Mock();
117N/A Y.Mock.expect(handler,{
117N/A method: "handleSuccess",
0N/A args: [Y.Mock.Value(function(e){
0N/A ASSERT.isNull(e.request, "Expected null request.");
0N/A ASSERT.isObject(e.response, "Expected normalized response object.");
0N/A ASSERT.isArray(e.response.results, "Expected results array.");
0N/A ASSERT.areSame(3, e.response.results.length, "Expected 3 results.")
0N/A ASSERT.areSame("a", e.response.results[0].type, "Expected first type.");
0N/A ASSERT.areSame("g", e.response.results[2].type, "Expected last type.");
0N/A ASSERT.areSame("c", e.response.results[0].name, "Expected first name.");
0N/A 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>