wrapComponent.jsx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import React from "react"
  2. import { render } from "enzyme"
  3. import System from "core/system"
  4. describe("wrapComponents", () => {
  5. describe("should wrap a component and provide a reference to the original", () => {
  6. it("with stateless components", function(){
  7. // Given
  8. const system = new System({
  9. plugins: [
  10. {
  11. components: {
  12. wow: ({ name }) => <div>{name} component</div>
  13. }
  14. },
  15. {
  16. wrapComponents: {
  17. wow: (OriginalComponent) => (props) => {
  18. return <container>
  19. <OriginalComponent {...props}></OriginalComponent>
  20. <OriginalComponent name="Wrapped"></OriginalComponent>
  21. </container>
  22. }
  23. }
  24. }
  25. ]
  26. })
  27. // When
  28. let Component = system.getSystem().getComponents("wow")
  29. const wrapper = render(<Component name="Normal" />)
  30. const container = wrapper.children().first()
  31. expect(container[0].name).toEqual("container")
  32. const children = container.children()
  33. expect(children.length).toEqual(2)
  34. expect(children.eq(0).text()).toEqual("Normal component")
  35. expect(children.eq(1).text()).toEqual("Wrapped component")
  36. })
  37. it("with React classes", function(){
  38. class MyComponent extends React.Component {
  39. render() {
  40. return <div>{this.props.name} component</div>
  41. }
  42. }
  43. // Given
  44. const system = new System({
  45. plugins: [
  46. {
  47. components: {
  48. wow: MyComponent
  49. }
  50. },
  51. {
  52. wrapComponents: {
  53. wow: (OriginalComponent) => {
  54. return class WrapperComponent extends React.Component {
  55. render() {
  56. return <container>
  57. <OriginalComponent {...this.props}></OriginalComponent>
  58. <OriginalComponent name="Wrapped"></OriginalComponent>
  59. </container>
  60. }
  61. }
  62. }
  63. }
  64. }
  65. ]
  66. })
  67. // When
  68. let Component = system.getSystem().getComponents("wow")
  69. const wrapper = render(<Component name="Normal" />)
  70. const container = wrapper.children().first()
  71. expect(container[0].name).toEqual("container")
  72. const children = container.children()
  73. expect(children.length).toEqual(2)
  74. expect(children.eq(0).text()).toEqual("Normal component")
  75. expect(children.eq(1).text()).toEqual("Wrapped component")
  76. })
  77. })
  78. it("should provide a reference to the system to the wrapper", function(){
  79. // Given
  80. const mySystem = new System({
  81. plugins: [
  82. {
  83. // Make a selector
  84. statePlugins: {
  85. doge: {
  86. selectors: {
  87. wow: () => () => {
  88. return "WOW much data"
  89. }
  90. }
  91. }
  92. }
  93. },
  94. {
  95. // Create a component
  96. components: {
  97. wow: () => <div>Original component</div>
  98. }
  99. },
  100. {
  101. // Wrap the component and use the system
  102. wrapComponents: {
  103. wow: (OriginalComponent, system) => (props) => {
  104. return <container>
  105. <OriginalComponent {...props}></OriginalComponent>
  106. <div>{system.dogeSelectors.wow()}</div>
  107. </container>
  108. }
  109. }
  110. }
  111. ]
  112. })
  113. // Then
  114. let Component = mySystem.getSystem().getComponents("wow")
  115. const wrapper = render(<Component name="Normal" />)
  116. const container = wrapper.children().first()
  117. expect(container[0].name).toEqual("container")
  118. const children = container.children()
  119. expect(children.length).toEqual(2)
  120. expect(children.eq(0).text()).toEqual("Original component")
  121. expect(children.eq(1).text()).toEqual("WOW much data")
  122. })
  123. it("should wrap correctly when registering more plugins", function(){
  124. // Given
  125. const mySystem = new System({
  126. plugins: [
  127. () => {
  128. return {
  129. statePlugins: {
  130. doge: {
  131. selectors: {
  132. wow: () => () => {
  133. return "WOW much data"
  134. }
  135. }
  136. }
  137. },
  138. components: {
  139. wow: () => <div>Original component</div>
  140. }
  141. }
  142. }
  143. ]
  144. })
  145. mySystem.register([
  146. function() {
  147. return {
  148. // Wrap the component and use the system
  149. wrapComponents: {
  150. wow: (OriginalComponent, system) => (props) => {
  151. return <container>
  152. <OriginalComponent {...props}></OriginalComponent>
  153. <div>{system.dogeSelectors.wow()}</div>
  154. </container>
  155. }
  156. }
  157. }
  158. }
  159. ])
  160. // Then
  161. let Component = mySystem.getSystem().getComponents("wow")
  162. const wrapper = render(<Component name="Normal" />)
  163. const container = wrapper.children().first()
  164. expect(container[0].name).toEqual("container")
  165. const children = container.children()
  166. expect(children.length).toEqual(2)
  167. expect(children.eq(0).text()).toEqual("Original component")
  168. expect(children.eq(1).text()).toEqual("WOW much data")
  169. })
  170. it("should wrap correctly when building a system twice", function(){
  171. // Given
  172. const pluginOne = {
  173. statePlugins: {
  174. doge: {
  175. selectors: {
  176. wow: () => () => {
  177. return "WOW much data"
  178. }
  179. }
  180. }
  181. },
  182. components: {
  183. wow: () => <div>Original component</div>
  184. }
  185. }
  186. const pluginTwo = {
  187. // Wrap the component and use the system
  188. wrapComponents: {
  189. wow: (OriginalComponent, system) => (props) => {
  190. return <container>
  191. <OriginalComponent {...props}></OriginalComponent>
  192. <div>{system.dogeSelectors.wow()}</div>
  193. </container>
  194. }
  195. }
  196. }
  197. const bothPlugins = () => [pluginOne, pluginTwo]
  198. new System({
  199. plugins: bothPlugins
  200. })
  201. const secondSystem = new System({
  202. plugins: bothPlugins
  203. })
  204. // Then
  205. let Component = secondSystem.getSystem().getComponents("wow")
  206. const wrapper = render(<Component name="Normal" />)
  207. const container = wrapper.children().first()
  208. expect(container[0].name).toEqual("container")
  209. const children = container.children()
  210. expect(children.length).toEqual(2)
  211. expect(children.eq(0).text()).toEqual("Original component")
  212. expect(children.eq(1).text()).toEqual("WOW much data")
  213. })
  214. })