test-utils.js 5.0 KB

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