Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

248 Zeilen
6.6 KiB

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