ws.test.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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($) {
  10. $(document).ready(function() {
  11. var $mainMenu = $('#nav-menus');
  12. var $menuRepo = $('#menu-repo');
  13. var $linkToRepo1 = $menuRepo.find('a:first');
  14. var $menuExample = $('#menu-example');
  15. var $menuBtn = $('#menu-btn');
  16. var $homeLink = $('#nav-back-home');
  17. var $editor = $('#editor');
  18. var $editorTabs = $('#tabs');
  19. QUnit.test( "hello test", function( assert ) {
  20. assert.ok( 1 == "1", "Passed!" );
  21. });
  22. QUnit.test( "initial state", function( assert ) {
  23. assert.equal( _ws.files.length, 0, "_ws.files should hold 0 file when app started from root" );
  24. assert.ok( $menuRepo.is(':visible'), "menu repo visible (though menu is hidden)" );
  25. assert.equal( ($menuRepo.find('a')).length, 2, "menu repo should contain 2 links" );
  26. assert.ok( $menuExample.is(':visible'), "menu example visible (though menu is hidden AND menu empty)" );
  27. assert.equal( ($menuExample.find('a')).length, 0, "menu example should contain 0 links" );
  28. assert.ok( ! $editor.is(':visible'), "editor invisible" );
  29. assert.ok( ! $editorTabs.is(':visible'), "editor tabs invisible" );
  30. });
  31. QUnit.test( "test menu toggle", function( assert ) {
  32. var done = assert.async();
  33. assert.equal( $mainMenu.width(), 0, "0. INIT width should be 0" );
  34. assert.ok( $mainMenu.is(':visible'), "0. INIT menu should NOT have class 'in'" );
  35. $menuBtn.trigger('click');
  36. assert.ok( $mainMenu.hasClass('in'), "1.AFTER CLICK menu should have class 'in'" );
  37. assert.ok( $mainMenu.is(':visible'), "1.AFTER CLICK menu should be visible" );
  38. timeoutAsync(() => {
  39. assert.notEqual( $mainMenu.width(), 0, "1.AFTER CLICK menu should have width > 0" );
  40. $menuBtn.trigger('click');
  41. assert.ok( ! $mainMenu.hasClass('in'), "2.AFTER 2ND CLICK menu should NOT have class 'in'" );
  42. }, 120)
  43. .then(() => timeoutAsync(() => {
  44. assert.equal( $mainMenu.width(), 0, "2.AFTER 2ND CLICK width should be 0" );
  45. done();
  46. }, 180));
  47. });
  48. QUnit.test( "test nav to repo", function( assert ) {
  49. var done = assert.async();
  50. assert.equal( window.location.pathname, '/', "location should be / at first" );
  51. $linkToRepo1.trigger('click');
  52. assert.equal( window.location.pathname, '/example-repo1', "location should be /example-repo1" );
  53. timeoutAsync(() => {
  54. assert.equal( ($menuExample.find('a')).length, 1, "menu example should contain 1 entry" );
  55. assert.equal( $menuExample.find('a:first').html(), "Test Example", "link to 1st example should be labelled 'Test Example'" );
  56. assert.ok( !! _ws.repo, "_ws.repo should not be undefined" );
  57. assert.equal( _ws.repo.title, "Example Repo 1", '_ws.repo.title is "Example Repo 1"' );
  58. done();
  59. }, 100);
  60. });
  61. QUnit.test( "test nav to example", function( assert ) {
  62. var done = assert.async();
  63. $linkToRepo1.trigger('click');
  64. assert.equal( window.location.pathname, '/example-repo1', "location should be /example-repo1" );
  65. assert.equal( ($menuExample.find('a')).length, 1, "menu example should contain 1 entry" );
  66. var $linkToExmp1 = $menuExample.find('a:first');
  67. $linkToExmp1.trigger('click');
  68. assert.equal( window.location.pathname, '/example-repo1/repo1-example1', "location should be /example-repo1/repo1-example1" );
  69. timeoutAsync(() => {
  70. assert.ok( $editor.is(':visible'), "editor visible" );
  71. assert.equal( _ws.files.length, 2, "_ws.files should hold 2 files" );
  72. done();
  73. }, 50);
  74. });
  75. QUnit.test( "test adding a collection", function( assert ) {
  76. var done = assert.async();
  77. var $addRepo = $('#add-repo');
  78. var $addRepoAddBtn = $addRepo.find('.add-btn');
  79. var $addRepoForm = $addRepo.find('form');
  80. assert.equal( $addRepo.length, 1, "there should be one #add-repo element" );
  81. assert.equal( $addRepoForm.length, 1, "#add-repo should contain one form element" );
  82. assert.ok( $addRepoForm.is(':visible'), "the form element shouldn't be visible before clicking the + btn" );
  83. $addRepoAddBtn.trigger( 'click' );
  84. assert.ok( $addRepoForm.is(':visible'), "clicking on the + btn should have made the form appear" );
  85. done();
  86. });
  87. QUnit.test( "test adding a file", function( assert ) {
  88. var done = assert.async();
  89. $linkToRepo1.trigger('click');
  90. var $linkToExmp1 = $menuExample.find('a:first');
  91. $linkToExmp1.trigger( 'click' );
  92. var $addFile = $('#add-file');
  93. var $addFileAddBtn = $addFile.find('.add-btn');
  94. var $addFileForm = $addFile.find('form');
  95. assert.equal( $addFile.length, 1, "there should be one #add-file element" );
  96. assert.equal( $addFileForm.length, 1, "#add-file should contain one form element" );
  97. assert.ok( ! $addFileForm.is(':visible'), "the form element shouldn't be visible before clicking the + btn" );
  98. assert.ok( $addFileForm.width() === 0, "the form element shouldn't be visible before clicking the + btn" );
  99. // $addFileAddBtn.trigger( 'click' );
  100. timeoutAsync(() => {
  101. console.log( $addFileForm );
  102. assert.ok( $addFileForm.is(':visible'), "clicking on the + btn should have made the form appear" );
  103. assert.ok( $addFileForm.width() > 0, "clicking on the + btn should have made the form appear" );
  104. done();
  105. }, 100);
  106. });
  107. QUnit.test( "test nav back to home", function( assert ) {
  108. var done = assert.async();
  109. $homeLink.trigger('click');
  110. assert.ok( window.location.pathname, '/', "location should be / when clicking app title" );
  111. timeoutAsync(() => {
  112. assert.equal( ($menuExample.find('a')).length, 0, "menu example should contain 0 entry" );
  113. done();
  114. }, 50);
  115. });
  116. });
  117. })(jQuery);