ws.test.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. function timeoutAsync(cb, delay) {
  2. return new Promise((resolve, reject) => {
  3. setTimeout(function() {
  4. cb();
  5. resolve(true);
  6. }, delay);
  7. });
  8. }
  9. function delayAsync(delay) {
  10. return new Promise((resolve, reject) => {
  11. setTimeout(function() {
  12. resolve(true);
  13. }, delay);
  14. });
  15. }
  16. function getId() {
  17. return Date.now().toString(36);
  18. }
  19. (function($) {
  20. $(document).ready(function() {
  21. var $mainMenu = $('#nav-menus');
  22. var $menuRepo = $('#menu-repo');
  23. var $linkToRepo1 = $menuRepo.find('a:first');
  24. var $menuExample = $('#menu-example');
  25. var $menuBtn = $('#menu-btn');
  26. var $homeLink = $('#nav-back-home');
  27. var $editor = $('#editor');
  28. var $editorTabs = $('#tabs');
  29. QUnit.test( "hello test", function( assert ) {
  30. assert.ok( 1 == "1", "Passed!" );
  31. });
  32. QUnit.test( "initial state", function( assert ) {
  33. assert.equal( _ws.files.length, 0, "_ws.files should hold 0 file when app started from root" );
  34. assert.ok( $menuRepo.is(':visible'), "menu repo visible (though menu is hidden)" );
  35. assert.equal( ($menuRepo.find('a')).length, 2, "menu repo should contain 2 links" );
  36. assert.ok( $menuExample.is(':visible'), "menu example visible (though menu is hidden AND menu empty)" );
  37. assert.equal( ($menuExample.find('a')).length, 0, "menu example should contain 0 links" );
  38. assert.ok( ! $editor.is(':visible'), "editor invisible" );
  39. assert.ok( ! $editorTabs.is(':visible'), "editor tabs invisible" );
  40. });
  41. QUnit.test( "test menu toggle", function( assert ) {
  42. var done = assert.async();
  43. assert.equal( $mainMenu.width(), 0, "0. INIT width should be 0" );
  44. assert.ok( $mainMenu.is(':visible'), "0. INIT menu should NOT have class 'in'" );
  45. $menuBtn.trigger('click');
  46. assert.ok( $mainMenu.hasClass('in'), "1.AFTER CLICK menu should have class 'in'" );
  47. assert.ok( $mainMenu.is(':visible'), "1.AFTER CLICK menu should be visible" );
  48. timeoutAsync(() => {
  49. assert.notEqual( $mainMenu.width(), 0, "1.AFTER CLICK menu should have width > 0" );
  50. $menuBtn.trigger('click');
  51. assert.ok( ! $mainMenu.hasClass('in'), "2.AFTER 2ND CLICK menu should NOT have class 'in'" );
  52. }, 120)
  53. .then(() => timeoutAsync(() => {
  54. assert.equal( $mainMenu.width(), 0, "2.AFTER 2ND CLICK width should be 0" );
  55. done();
  56. }, 180));
  57. });
  58. QUnit.test( "test nav to repo", function( assert ) {
  59. var done = assert.async();
  60. assert.equal( window.location.pathname, '/', "location should be / at first" );
  61. $linkToRepo1.trigger('click');
  62. assert.equal( window.location.pathname, '/example-repo-1', "location should be /example-repo-1" );
  63. timeoutAsync(() => {
  64. assert.equal( ($menuExample.find('a')).length, 1, "menu example should contain 1 entry" );
  65. assert.equal( $menuExample.find('a:first').html(), "Test Example", "link to 1st example should be labelled 'Test Example'" );
  66. assert.ok( !! _ws.repo, "_ws.repo should not be undefined" );
  67. assert.equal( _ws.repo.title, "Example Repo 1", '_ws.repo.title is "Example Repo 1"' );
  68. done();
  69. }, 100);
  70. });
  71. QUnit.test( "test nav to example", function( assert ) {
  72. var done = assert.async();
  73. $linkToRepo1.trigger('click');
  74. assert.equal( window.location.pathname, '/example-repo-1', "location should be /example-repo-1" );
  75. assert.equal( ($menuExample.find('a')).length, 1, "menu example should contain 1 entry" );
  76. var $linkToExmp1 = $menuExample.find('a:first');
  77. $linkToExmp1.trigger('click');
  78. assert.equal( window.location.pathname, '/example-repo-1/repo1-example1', "location should be /example-repo-1/repo1-example1" );
  79. timeoutAsync(() => {
  80. assert.ok( $editor.is(':visible'), "editor visible" );
  81. assert.equal( _ws.files.length, 2, "_ws.files should hold 2 files" );
  82. done();
  83. }, 50);
  84. });
  85. QUnit.test( "test adding a collection", function( assert ) {
  86. var done = assert.async();
  87. var $addRepo = $('#add-repo');
  88. var $addRepoAddBtn = $addRepo.find('.add-btn');
  89. var $addRepoForm = $addRepo.find('form');
  90. assert.equal( $addRepo.length, 1, "there should be one #add-repo element" );
  91. assert.equal( $addRepoForm.length, 1, "#add-repo should contain one form element" );
  92. assert.ok( $addRepoForm.is(':visible'), "the form element shouldn't be visible before clicking the + btn" );
  93. $addRepoAddBtn.trigger( 'click' );
  94. assert.ok( $addRepoForm.is(':visible'), "clicking on the + btn should have made the form appear" );
  95. done();
  96. });
  97. QUnit.test( "test adding a file", function( assert ) {
  98. var done = assert.async();
  99. var $addFile = $('#add-file');
  100. var $addFileInput = $('#add-file').find('input[type="text"]');
  101. var $addFileAddBtn = $addFile.find('.add-btn');
  102. var $addFileForm = $addFile.find('form');
  103. $linkToRepo1.trigger('click');
  104. timeoutAsync(() => {
  105. var $linkToExmp1 = $menuExample.find('a:first');
  106. console.log('##### clicked on first example', $linkToExmp1.prop('href'));
  107. $linkToExmp1.trigger( 'click' );
  108. }, 50)
  109. .then(() => delayAsync(400))
  110. .then(() => {
  111. assert.equal( $addFile.length, 1, "there should be one #add-file element" );
  112. assert.equal( $addFileForm.length, 1, "#add-file should contain one form element" );
  113. // assert.ok( ! $addFileForm.is(':visible'), "the form element shouldn't be visible before clicking the + btn" );
  114. assert.ok( $addFileForm.width() === 0, "the form element shouldn't be visible before clicking the + btn" );
  115. $addFileAddBtn.trigger( 'click' );
  116. })
  117. .then(() => timeoutAsync(() => {
  118. console.log( $addFileForm );
  119. // assert.ok( $addFileForm.is(':visible'), "clicking on the + btn should have made the form appear" );
  120. assert.ok( $addFileForm.width() > 0, "clicking on the + btn should have made the form appear" );
  121. $addFileInput.val('script-' + getId() + '.js');
  122. $addFileForm.find('button[type="submit"]')
  123. .trigger( 'click' );
  124. }, 100))
  125. .then(() => delayAsync(400))
  126. .then(done);
  127. });
  128. QUnit.test( "test nav back to home", function( assert ) {
  129. var done = assert.async();
  130. $homeLink.trigger('click');
  131. assert.ok( window.location.pathname, '/', "location should be / when clicking app title" );
  132. timeoutAsync(() => {
  133. assert.equal( ($menuExample.find('a')).length, 0, "menu example should contain 0 entry" );
  134. done();
  135. }, 50);
  136. });
  137. });
  138. })(jQuery);