test-utils.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import React from 'react'; // jshint ignore:line
  2. import ReactDOM from 'react-dom';
  3. import ReactTestUtils from 'react-addons-test-utils';
  4. // clean test mounts from components
  5. export function render(containerOrComponent, Component) {
  6. if (Component) {
  7. return ReactDOM.render(
  8. Component, document.getElementById(containerOrComponent + '-mount'));
  9. } else {
  10. return ReactDOM.render(
  11. containerOrComponent, document.getElementById('test-mount'));
  12. }
  13. }
  14. export function unmountComponents() {
  15. ReactDOM.unmountComponentAtNode(document.getElementById('dropdown-mount'));
  16. ReactDOM.unmountComponentAtNode(document.getElementById('modal-mount'));
  17. ReactDOM.unmountComponentAtNode(document.getElementById('page-mount'));
  18. ReactDOM.unmountComponentAtNode(document.getElementById('test-mount'));
  19. }
  20. // global utility for mocking context
  21. export function contextClear(misago) {
  22. misago._context = {};
  23. }
  24. export function mockUser(overrides) {
  25. let user = {
  26. id : 42,
  27. absolute_url: "/user/loremipsum-42/",
  28. api_url: {
  29. avatar: "/test-api/users/42/avatar/",
  30. change_email: "/test-api/users/42/change-email/",
  31. change_password: "/test-api/users/42/change-password/",
  32. options: "/test-api/users/42/forum-options/",
  33. username: "/test-api/users/42/username/"
  34. },
  35. avatar_hash: "5c6a04b4",
  36. email: "test@example.com",
  37. is_hiding_presence: false,
  38. joined_on: "2015-05-09T16:13:33.973603Z",
  39. limits_private_thread_invites_to: 0,
  40. new_notifications: 0,
  41. posts: 30,
  42. rank: {
  43. id: 1,
  44. css_class: "team",
  45. description: '<p>Lorem ipsum dolor met sit amet elit, si vis pacem para bellum.</p>\n<p>To help see <a href="http://wololo.com/something.php?page=2131">http://wololo.com/something.php?page=2131</a></p>',
  46. is_tab: true,
  47. name: "Forum team",
  48. slug: "forum-team",
  49. title: "Team"
  50. },
  51. slug: "loremipsum",
  52. subscribe_to_replied_threads: 2,
  53. subscribe_to_started_threads: 1,
  54. threads: 0,
  55. title: "",
  56. unread_private_threads: 0,
  57. username: "LoremIpsum",
  58. status: null,
  59. acl: {}
  60. };
  61. if (overrides) {
  62. return Object.assign(user, overrides);
  63. } else {
  64. return user;
  65. }
  66. }
  67. export function contextGuest(misago) {
  68. misago._context = Object.assign({}, misago._context, {
  69. isAuthenticated: false,
  70. user: {
  71. id : null,
  72. acl: {}
  73. }
  74. });
  75. }
  76. export function contextAuthenticated(misago, overrides) {
  77. misago._context = Object.assign({}, misago._context, {
  78. isAuthenticated: true,
  79. user: mockUser(overrides)
  80. });
  81. }
  82. // global utility function for store mocking
  83. export function initEmptyStore(store) {
  84. store.constructor();
  85. store.addReducer('tick', function(state={}, action=null) { return {}; }, {}); // jshint ignore:line
  86. store.init();
  87. }
  88. export function snackbarStoreMock() {
  89. return {
  90. message: null,
  91. _callback: null,
  92. callback: function(callback) {
  93. this._callback = callback;
  94. },
  95. dispatch: function(action) {
  96. if (action.type === 'SHOW_SNACKBAR') {
  97. this.message = {
  98. message: action.message,
  99. type: action.messageType
  100. };
  101. if (this._callback) {
  102. window.setTimeout(() => {
  103. this._callback(this.message);
  104. }, 100);
  105. }
  106. }
  107. }
  108. };
  109. }
  110. // global init function for modal and dropdown services
  111. export function initModal(modal) {
  112. $('#modal-mount').off();
  113. modal.init(document.getElementById('modal-mount'));
  114. }
  115. export function initDropdown(dropdown) {
  116. dropdown.init(document.getElementById('dropdown-mount'));
  117. }
  118. // global util for reseting snackbar
  119. export function snackbarClear(snackbar) {
  120. // NOTE: Never ever cause situation when snackbar is triggered more than once
  121. // in the single test, because this results in race condition within tests
  122. // suite where one tests check's snackbar state before it has reopened with
  123. // new message set by current test
  124. if (snackbar._timeout) {
  125. window.clearTimeout(snackbar._timeout);
  126. snackbar._timeout = null;
  127. }
  128. }
  129. // global util functions for events
  130. export function simulateClick(selector) {
  131. if ($(selector).length) {
  132. ReactTestUtils.Simulate.click($(selector).get(0));
  133. } else {
  134. throw 'selector "' + selector + '" did not match anything';
  135. }
  136. }
  137. export function simulateSubmit(selector) {
  138. if ($(selector).length) {
  139. ReactTestUtils.Simulate.submit($(selector).get(0));
  140. } else {
  141. throw 'selector "' + selector + '" did not match anything';
  142. }
  143. }
  144. export function simulateChange(selector, value) {
  145. if ($(selector).length) {
  146. $(selector).val(value);
  147. ReactTestUtils.Simulate.change($(selector).get(0));
  148. } else {
  149. throw 'selector "' + selector + '" did not match anything';
  150. }
  151. }
  152. export function afterAjax(callback) {
  153. window.setTimeout(function() {
  154. callback();
  155. }, 200);
  156. }
  157. export function onElement(selector, callback) {
  158. let _getElement = function() {
  159. window.setTimeout(function() {
  160. let element = $(selector);
  161. if (element.length >= 1) {
  162. callback(element);
  163. } else {
  164. _getElement();
  165. }
  166. }, 50);
  167. };
  168. _getElement();
  169. }