PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/MS Silk/Scripts/tests/mstats.fillups.tests.js

#
JavaScript | 224 lines | 158 code | 36 blank | 30 comment | 1 complexity | 9d25309d5641cfffde733b90061d00b4 MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-2.0
  1. //===================================================================================
  2. // Microsoft patterns & practices
  3. // Silk : Web Client Guidance
  4. //===================================================================================
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
  7. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
  8. // LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  9. // FITNESS FOR A PARTICULAR PURPOSE.
  10. //===================================================================================
  11. // The example companies, organizations, products, domain names,
  12. // e-mail addresses, logos, people, places, and events depicted
  13. // herein are fictitious. No association with any real company,
  14. // organization, product, domain name, email address, logo, person,
  15. // places, or events is intended or should be inferred.
  16. //===================================================================================
  17. /*jslint onevar: true, undef: true, newcap: true, regexp: true, plusplus: true, bitwise: true, devel: true, maxerr: 50 */
  18. (function ($) {
  19. module('Vehicle Fillups Widget', {
  20. setup: function () {
  21. this.savedAjax = $.ajax;
  22. $('#qunit-fixture').append('<div class="tab opened section" id="fillups-pane">' +
  23. '<a class="trigger" href="/Fillup/List/1"></a>' +
  24. '<div class="content">' +
  25. '<div class="header">' +
  26. '<form action="/Fillup/Add/1" method="get">' +
  27. '<button class="button generic small" type="submit" data-action="fillup-add-selected">' +
  28. '<img title="Add Fill up" alt="Add" src="/Content/button-add.png" />' +
  29. '</button>' +
  30. '</form>' +
  31. '</div>' +
  32. '<div class="aside">' +
  33. '<div class="list nav">' +
  34. '<a class="list-item selected" href="/Fillup/Details/1">' +
  35. '<h1 class="date">1 Apr 2011</h1>' +
  36. '<p class="vendor">Margie&apos;s Travel</p>' +
  37. '<p class="total-cost">62.18</p>' +
  38. '</a>' +
  39. '<a class="list-item " href="/Fillup/Details/2">' +
  40. '<h1 class="date">19 Mar 2011</h1>' +
  41. '<p class="vendor">Adventure Works</p>' +
  42. '<p class="total-cost">62.46</p>' +
  43. '</a>' +
  44. '</div>' +
  45. '</div>' +
  46. '</div>' +
  47. '</div>');
  48. },
  49. teardown: function () {
  50. $.ajax = this.savedAjax;
  51. $('#fillups-pane').fillups('destroy');
  52. }
  53. });
  54. /****************************************************************
  55. * Data Loading Tests
  56. ****************************************************************/
  57. test('while loading data, then the widget ensures the contents are hidden', function () {
  58. expect(1);
  59. var fillups = $('#fillups-pane').fillups({
  60. sendRequest: function (options) {
  61. ok($('.content:hidden').length > 0, 'contents are hidden');
  62. options.success({});
  63. }
  64. });
  65. // force a data refresh
  66. fillups.fillups('option', 'selectedVehicleId', 1);
  67. });
  68. test('when loading data is complete, then the contents are shown', function () {
  69. expect(1);
  70. var fillups = $('#fillups-pane').fillups({
  71. sendRequest: function (options) { options.success({}); }
  72. });
  73. // force a data refresh
  74. fillups.fillups('option', 'selectedVehicleId', 1);
  75. setTimeout(function () { // this allows the animation to finish
  76. ok($('.content:hidden').length === 0, 'contents are shown');
  77. start();
  78. }, 2500);
  79. stop(3000);
  80. });
  81. test('when loading data errors out, then the widget ensures the contents are hidden', function () {
  82. expect(1);
  83. var fillups = $('#fillups-pane').fillups({
  84. sendRequest: function (options) {
  85. ok($('.content:hidden').length > 0, 'contents are hidden');
  86. options.error({});
  87. },
  88. templateId: 'testTemplate'
  89. });
  90. // force a data refresh
  91. fillups.fillups('option', 'selectedVehicleId', 1);
  92. });
  93. test('when loading data errors out, then triggers error status', function () {
  94. expect(2);
  95. var eventType = 'loadError',
  96. fillups = $('#fillups-pane').fillups({
  97. templateId: '#testTemplate',
  98. sendRequest: function (options) { options.error({}); },
  99. publish: function (event, status) {
  100. if (status.type === eventType) {
  101. ok(status, 'status object passed to publisher');
  102. equal(status.type, eventType, 'status is of type : ' + eventType);
  103. }
  104. }
  105. });
  106. // force a data refresh
  107. fillups.fillups('option', 'selectedVehicleId', 1);
  108. });
  109. test('when refreshData is called, then sendRequest is called', function () {
  110. expect(1);
  111. $('#fillups-pane').fillups({
  112. sendRequest: function (options) { options.success({}); }
  113. });
  114. $('#fillups-pane').fillups('option', 'sendRequest', function () {
  115. ok(true, 'sendRequest was called properly');
  116. });
  117. $('#fillups-pane').fillups('refreshData');
  118. });
  119. test('when requeryData is called, then cache invalidated', function () {
  120. expect(1);
  121. $('#fillups-pane').fillups({
  122. sendRequest: function (options) { options.success({}); },
  123. dataUrl: 'someEndpoint',
  124. invalidateData: function (endpoint) {
  125. equal(this.dataUrl, endpoint);
  126. }
  127. });
  128. $('#fillups-pane').fillups('requeryData');
  129. });
  130. test('when requeryData is called, then sendRequest called properly', function () {
  131. expect(1);
  132. $('#fillups-pane').fillups({
  133. sendRequest: function (options) {
  134. ok(true, 'requeryData invoked sendRequest');
  135. }
  136. });
  137. $('#fillups-pane').fillups('requeryData');
  138. });
  139. /****************************************************************
  140. * Data Templating Tests
  141. ****************************************************************/
  142. module('Fillups Widget Template Tests', {
  143. setup: function () {
  144. $('#qunit-fixture').append(
  145. '<div id="fillups-pane" class="tab opened section"><div class="content"/></div>' +
  146. '<script id="testTemplate" type="text/x-jquery-tmpl"><p>contents go here</p></script>' +
  147. '<script id="testTemplate2" type="text/x-jquery-tmpl"><p>other contents</p></script>'
  148. );
  149. this.savedAjax = $.ajax;
  150. },
  151. teardown: function () {
  152. $.ajax = this.savedAjax;
  153. $('#fillups-pane').fillups('destroy');
  154. }
  155. });
  156. test('when selected vehicle id is set and sendRequest not specified, then widget calls $.ajax', function () {
  157. expect(1);
  158. $.ajax = function () {
  159. ok(true, '$.ajax was called');
  160. };
  161. var fillups = $('#fillups-pane').fillups();
  162. // force a data refresh
  163. fillups.fillups('option', 'selectedVehicleId', 1);
  164. });
  165. test('when widget data is retrieved, then widget contents are replaced by the template', function () {
  166. expect(1);
  167. var fillups = $('#fillups-pane').fillups({
  168. sendRequest: function (options) { options.success({}); },
  169. templateId: '#testTemplate'
  170. });
  171. // force a data refresh
  172. fillups.fillups('option', 'selectedVehicleId', 1);
  173. equal($.trim($('#fillups-pane').html()), $.trim($('#testTemplate').html().toLowerCase()), 'Template applied');
  174. });
  175. test('when refreshData is called, then template is re-applied', function () {
  176. expect(2);
  177. var fillups = $('#fillups-pane').fillups({
  178. sendRequest: function (options) { options.success({}); },
  179. templateId: '#testTemplate'
  180. });
  181. // force a data refresh
  182. fillups.fillups('option', 'selectedVehicleId', 1);
  183. equal($.trim($('#fillups-pane').html()), $.trim($('#testTemplate').html().toLowerCase()), 'Template applied');
  184. $('#fillups-pane').fillups('option', 'templateId', '#testTemplate2');
  185. $('#fillups-pane').fillups('refreshData');
  186. equal($.trim($('#fillups-pane').html()), $.trim($('#testTemplate2').html().toLowerCase()), 'Template applied');
  187. });
  188. } (jQuery));