You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

217 lines
5.3 KiB

  1. function moduleTests(ui) {
  2. var
  3. module = ui.module,
  4. element = ui.element,
  5. singleton = ui.singleton,
  6. name = $.fn[module].settings.name,
  7. testValue = 'Test',
  8. fixtures = jasmine.getFixtures(),
  9. originalSettings,
  10. $modules,
  11. $oneModule,
  12. $module,
  13. $clone
  14. ;
  15. // set fixture path
  16. fixtures.fixturesPath = 'base/test/fixtures/';
  17. // disable debug
  18. $.fn[module].settings.debug = false;
  19. $.fn[module].settings.performance = false;
  20. $.fn[module].settings.verbose = false;
  21. beforeEach(function() {
  22. // load fixtures
  23. fixtures.load(module + '.html');
  24. // save settings
  25. originalSettings = $.fn[module].settings;
  26. // module available in scope
  27. $module = $(element);
  28. // one module available in fixture
  29. if($module.size() == 1) {
  30. $oneModule = $module;
  31. $clone = $module.clone().appendTo( $(sandbox()) );
  32. $modules = $clone.add($module);
  33. }
  34. // multiple modules available in fixture
  35. else {
  36. $modules = $(element);
  37. $clone = $module.eq(1);
  38. $oneModule = $modules.first();
  39. }
  40. });
  41. afterEach(function() {
  42. // restore settings
  43. $.fn[module].settings = originalSettings;
  44. // remove element
  45. $(element).remove();
  46. });
  47. /*******************************
  48. Module
  49. *******************************/
  50. /*-------------------
  51. Instantiation
  52. --------------------*/
  53. describe('Module', function() {
  54. it("allows chaining when no settings returned", function() {
  55. var $chain = $modules[module]();
  56. expect($chain).toExist();
  57. expect($chain.size()).toBe($modules.size());
  58. });
  59. it("returns a string when one setting returned", function() {
  60. var result = $oneModule[module]('setting', 'name');
  61. expect(typeof result).toBe('string');
  62. });
  63. it("returns an array when multiple settings returned", function() {
  64. var result = $modules[module]('setting', 'name');
  65. expect( $.isArray(result) ).toBeTruthy();
  66. });
  67. it("has an instance in metadata after init", function() {
  68. $oneModule[module]();
  69. expect($module).toHaveData('module-' + module);
  70. });
  71. });
  72. /*-------------------
  73. Settings
  74. --------------------*/
  75. describe('Settings', function() {
  76. it("clears settings on re-init", function() {
  77. $oneModule[module]({
  78. name: testValue
  79. });
  80. var retrievedValue = $oneModule[module]('setting', 'name');
  81. expect(retrievedValue).toBe(testValue);
  82. // reinit
  83. $oneModule[module]();
  84. retrievedValue = $oneModule[module]('setting', 'name');
  85. expect(retrievedValue).toBe(name);
  86. });
  87. it("allows default settings to be changed", function() {
  88. $.fn[module].settings.name = testValue;
  89. $oneModule[module]();
  90. var retrievedValue = $oneModule[module]('setting', 'name');
  91. $.fn[module].settings.name = name;
  92. expect(retrievedValue).toBe(testValue);
  93. });
  94. it("allows settings to be changed during init", function() {
  95. $oneModule[module]({
  96. name: testValue
  97. });
  98. var retrievedValue = $oneModule[module]('setting', 'name');
  99. expect(retrievedValue).toBe(testValue);
  100. });
  101. it("allows settings to be changed during runtime", function() {
  102. $oneModule[module]();
  103. var retrievedValue = $oneModule[module]('setting', 'name');
  104. expect(retrievedValue).toBe(name);
  105. });
  106. });
  107. /*-------------------
  108. Groups
  109. --------------------*/
  110. if(!singleton) {
  111. describe('Group Contamination', function() {
  112. it("creates settings for all instances", function() {
  113. $modules[module]('setting', 'name', testValue);
  114. var retrievedValue = $oneModule[module]('setting', 'name');
  115. var clonedSetting = $clone[module]('setting', 'name');
  116. expect(retrievedValue).toBe(testValue);
  117. expect(clonedSetting).toBe(testValue);
  118. $oneModule[module]({
  119. 'name': testValue
  120. });
  121. expect(retrievedValue).toBe(testValue);
  122. expect(clonedSetting).toBe(testValue);
  123. });
  124. it("does not change other elements settings when changing one element", function() {
  125. $modules[module]();
  126. $oneModule[module]('setting', 'name', testValue);
  127. var retrievedValue = $oneModule[module]('setting', 'name');
  128. var clonedSetting = $clone[module]('setting', 'name');
  129. expect(retrievedValue).toBe(testValue);
  130. expect(clonedSetting).toBe(name);
  131. });
  132. it("does not change other elements when re-initialized", function() {
  133. $modules[module]();
  134. $oneModule[module]({
  135. 'name': testValue
  136. });
  137. var retrievedValue = $oneModule[module]('setting', 'name');
  138. var clonedSetting = $clone[module]('setting', 'name');
  139. expect(retrievedValue).toBe(testValue);
  140. expect(clonedSetting).toBe(name);
  141. });
  142. });
  143. }
  144. /*-------------------
  145. Destroy
  146. --------------------*/
  147. describe('Destroy', function() {
  148. it("removes all events from page", function() {
  149. $module[module]('destroy');
  150. if($.events().length > 0) {
  151. dump($.events());
  152. }
  153. expect($.events().length).toBe(0);
  154. });
  155. it("removes instance metadata", function() {
  156. $module[module]('destroy');
  157. expect( $module.data('module-'+ module) ).toBe(undefined);
  158. });
  159. });
  160. }