No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

332 líneas
7.4 KiB

  1. /* eslint-env mocha */
  2. import expect from "expect"
  3. import System from "core/system"
  4. import { fromJS } from "immutable"
  5. describe("bound system", function(){
  6. describe("wrapActions", function(){
  7. it("should replace an action", function(){
  8. // Given
  9. const system = new System({
  10. plugins: {
  11. statePlugins: {
  12. josh: {
  13. actions: {
  14. simple: () => {
  15. return { type: "simple" }
  16. }
  17. },
  18. wrapActions: {
  19. simple: () => () => {
  20. return { type: "newSimple" }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. })
  27. // When
  28. var action = system.getSystem().joshActions.simple(1)
  29. expect(action).toEqual({
  30. type: "newSimple"
  31. })
  32. })
  33. it("should expose the original action, and the system as args", function(){
  34. // Given
  35. const simple = () => ({type: "simple" })
  36. const system = new System({
  37. plugins: {
  38. statePlugins: {
  39. josh: {
  40. actions: { simple },
  41. wrapActions: {
  42. simple: (oriAction, system) => (actionArg) => {
  43. return {
  44. type: "newSimple",
  45. oriActionResult: oriAction(),
  46. system: system.getSystem(),
  47. actionArg
  48. }
  49. }
  50. }
  51. }
  52. }
  53. }
  54. })
  55. // When
  56. var action = system.getSystem().joshActions.simple(1)
  57. expect(action).toEqual({
  58. type: "newSimple",
  59. oriActionResult: { type: "simple" },
  60. system: system.getSystem(),
  61. actionArg: 1
  62. })
  63. })
  64. it("should support multiple wraps of the same action", function(){
  65. const system = new System({
  66. plugins: [
  67. {
  68. statePlugins: {
  69. kyle: {
  70. actions: {
  71. simple: () => {
  72. return {
  73. type: "simple",
  74. }
  75. }
  76. }
  77. }
  78. }
  79. },
  80. {
  81. statePlugins: {
  82. kyle: {
  83. wrapActions: {
  84. simple: (ori) => () => {
  85. return {
  86. ...ori(),
  87. firstWrap: true
  88. }
  89. }
  90. }
  91. }
  92. }
  93. },
  94. {
  95. statePlugins: {
  96. kyle: {
  97. wrapActions: {
  98. simple: (ori) => () => {
  99. return {
  100. ...ori(),
  101. secondWrap: true
  102. }
  103. }
  104. }
  105. }
  106. }
  107. }
  108. ]
  109. })
  110. // When
  111. var action = system.getSystem().kyleActions.simple(1)
  112. expect(action).toEqual({
  113. type: "simple",
  114. firstWrap: true,
  115. secondWrap: true,
  116. })
  117. })
  118. it("should execute wrapActions in the order they appear ( via plugins )", function(){
  119. const system = new System({
  120. plugins: [
  121. {
  122. statePlugins: {
  123. kyle: {
  124. actions: {
  125. simple: () => {
  126. return {
  127. type: "one",
  128. }
  129. }
  130. }
  131. }
  132. }
  133. },
  134. {
  135. statePlugins: {
  136. kyle: {
  137. wrapActions: {
  138. simple: (ori) => () => {
  139. const obj = ori()
  140. obj.type += "-two"
  141. return obj
  142. }
  143. }
  144. }
  145. }
  146. },
  147. {
  148. statePlugins: {
  149. kyle: {
  150. wrapActions: {
  151. simple: (ori) => () => {
  152. const obj = ori()
  153. obj.type += "-three"
  154. return obj
  155. }
  156. }
  157. }
  158. }
  159. }
  160. ]
  161. })
  162. // When
  163. var action = system.getSystem().kyleActions.simple(1)
  164. expect(action.type).toEqual("one-two-three")
  165. })
  166. it("should have a the latest system", function(){
  167. // Given
  168. const system = new System({
  169. plugins: [
  170. {
  171. statePlugins: {
  172. kyle: {
  173. actions: {
  174. simple: () => {
  175. return {
  176. type: "one",
  177. }
  178. }
  179. },
  180. wrapActions: {
  181. simple: (ori, {joshActions}) => () => {
  182. return joshActions.hello()
  183. }
  184. }
  185. }
  186. }
  187. },
  188. ]
  189. })
  190. // When
  191. const kyleActions = system.getSystem().kyleActions
  192. system.register({
  193. statePlugins: {
  194. josh: {
  195. actions: {
  196. hello(){ return {type: "hello" } }
  197. }
  198. }
  199. }
  200. })
  201. const action = kyleActions.simple()
  202. expect(action).toEqual({ type: "hello"})
  203. })
  204. it.skip("should be able to create async actions", function(){
  205. const system = new System({
  206. plugins: [
  207. {
  208. statePlugins: {
  209. kyle: {
  210. actions: {
  211. simple: () => {
  212. return {
  213. type: "one",
  214. }
  215. }
  216. }
  217. }
  218. }
  219. },
  220. {
  221. statePlugins: {
  222. kyle: {
  223. wrapActions: {
  224. // eslint-disable-next-line no-unused-vars
  225. simple: (ori) => (arg) => (sys) => {
  226. return { type: "called" }
  227. }
  228. }
  229. }
  230. }
  231. },
  232. ]
  233. })
  234. // When
  235. var action = system.getSystem().kyleActions.simple(1)
  236. expect(action.type).toEqual("called")
  237. })
  238. })
  239. describe("selectors", function(){
  240. it("should have the first arg be the nested state, and all other args to follow", function(){
  241. // Given
  242. const system = new System({
  243. state: {
  244. josh: {
  245. one: 1
  246. }
  247. },
  248. plugins: {
  249. statePlugins: {
  250. josh: {
  251. selectors: {
  252. simple: (state, arg1) => {
  253. return { state, arg1 }
  254. }
  255. }
  256. }
  257. }
  258. }
  259. })
  260. // When
  261. var res = system.getSystem().joshSelectors.simple(1)
  262. expect(res).toEqual({
  263. state: fromJS({
  264. one: 1
  265. }),
  266. arg1: 1
  267. })
  268. })
  269. describe("when selector returns a funtcion", function(){
  270. it("should pass the system to that function", function(){
  271. // Given
  272. const system = new System({
  273. plugins: {
  274. statePlugins: {
  275. josh: {
  276. selectors: {
  277. advanced: () => (mySystem) => {
  278. // Then
  279. expect(mySystem).toEqual(system.getSystem())
  280. return "hi"
  281. }
  282. }
  283. }
  284. }
  285. }
  286. })
  287. // When
  288. var res = system.getSystem().joshSelectors.advanced(1)
  289. expect(res).toEqual("hi")
  290. })
  291. })
  292. })
  293. })