Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 7 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. simple: (ori) => (arg) => (sys) => {
  225. return { type: "called" }
  226. }
  227. }
  228. }
  229. }
  230. },
  231. ]
  232. })
  233. // When
  234. var action = system.getSystem().kyleActions.simple(1)
  235. expect(action.type).toEqual("called")
  236. })
  237. })
  238. describe("selectors", function(){
  239. it("should have the first arg be the nested state, and all other args to follow", function(){
  240. // Given
  241. const system = new System({
  242. state: {
  243. josh: {
  244. one: 1
  245. }
  246. },
  247. plugins: {
  248. statePlugins: {
  249. josh: {
  250. selectors: {
  251. simple: (state, arg1) => {
  252. return { state, arg1 }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. })
  259. // When
  260. var res = system.getSystem().joshSelectors.simple(1)
  261. expect(res).toEqual({
  262. state: fromJS({
  263. one: 1
  264. }),
  265. arg1: 1
  266. })
  267. })
  268. describe("when selector returns a funtcion", function(){
  269. it("should pass the system to that function", function(){
  270. // Given
  271. const system = new System({
  272. plugins: {
  273. statePlugins: {
  274. josh: {
  275. selectors: {
  276. advanced: () => (mySystem) => {
  277. // Then
  278. expect(mySystem).toEqual(system.getSystem())
  279. return "hi"
  280. }
  281. }
  282. }
  283. }
  284. }
  285. })
  286. // When
  287. var res = system.getSystem().joshSelectors.advanced(1)
  288. expect(res).toEqual("hi")
  289. })
  290. })
  291. })
  292. })