UiHelper.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. var common = require('../common');
  3. describe('UiHelper', function () {
  4. // TODO: As per https://github.com/tmpvar/jsdom/issues/1565 there is no navigation support in jsdom, yet.
  5. // for now we use a mock function to trigger the event
  6. describe('historyChange', function () {
  7. this.timeout(30000);
  8. beforeEach(function () {
  9. $.PrivateBin.Helper.reset();
  10. cleanup();
  11. });
  12. jsc.property(
  13. 'redirects to home, when the state is null',
  14. common.jscUrl(false, false),
  15. function (url) {
  16. const expected = common.urlToString(url),
  17. clean = jsdom('', {url: expected});
  18. // make window.location.href writable
  19. Object.defineProperty(window.location, 'href', {
  20. writable: true,
  21. value: window.location.href
  22. });
  23. $.PrivateBin.UiHelper.mockHistoryChange();
  24. $.PrivateBin.Helper.reset();
  25. var result = window.location.href;
  26. clean();
  27. return expected === result;
  28. }
  29. );
  30. jsc.property(
  31. 'does not redirect to home, when a new paste is created',
  32. common.jscUrl(false),
  33. jsc.nearray(common.jscBase64String()),
  34. function (url, fragment) {
  35. url.fragment = fragment.join('');
  36. const expected = common.urlToString(url),
  37. clean = jsdom('', {url: expected});
  38. // make window.location.href writable
  39. Object.defineProperty(window.location, 'href', {
  40. writable: true,
  41. value: window.location.href
  42. });
  43. $.PrivateBin.UiHelper.mockHistoryChange([
  44. {type: 'newpaste'}, '', expected
  45. ]);
  46. $.PrivateBin.Helper.reset();
  47. var result = window.location.href;
  48. clean();
  49. return expected === result;
  50. }
  51. );
  52. });
  53. describe('reloadHome', function () {
  54. this.timeout(30000);
  55. before(function () {
  56. $.PrivateBin.Helper.reset();
  57. });
  58. jsc.property(
  59. 'redirects to home',
  60. common.jscUrl(),
  61. function (url) {
  62. const clean = jsdom('', {url: common.urlToString(url)});
  63. delete(url.query);
  64. delete(url.fragment);
  65. const expected = common.urlToString(url);
  66. // make window.location.href writable
  67. Object.defineProperty(window.location, 'href', {
  68. writable: true,
  69. value: window.location.href
  70. });
  71. $.PrivateBin.UiHelper.reloadHome();
  72. $.PrivateBin.Helper.reset();
  73. var result = window.location.href;
  74. clean();
  75. return expected === result;
  76. }
  77. );
  78. });
  79. describe('isVisible', function () {
  80. // TODO As per https://github.com/tmpvar/jsdom/issues/1048 there is no layout support in jsdom, yet.
  81. // once it is supported or a workaround is found, uncomment the section below
  82. /*
  83. before(function () {
  84. $.PrivateBin.Helper.reset();
  85. });
  86. jsc.property(
  87. 'detect visible elements',
  88. jsc.nearray(common.jscAlnumString()),
  89. jsc.nearray(common.jscA2zString()),
  90. function (id, element) {
  91. id = id.join('');
  92. element = element.join('');
  93. var clean = jsdom(
  94. '<' + element + ' id="' + id + '"></' + element + '>'
  95. );
  96. var result = $.PrivateBin.UiHelper.isVisible($('#' + id));
  97. clean();
  98. return result;
  99. }
  100. );
  101. */
  102. });
  103. describe('scrollTo', function () {
  104. // TODO Did not find a way to test that, see isVisible test above
  105. });
  106. });