Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.62 KB | None | 0 0
  1. function testOnPage(className) {
  2. return async ({ rendered }) => {
  3. await wait(() => expect(rendered.container.querySelector(className)).toBeInTheDocument());
  4. };
  5. }
  6.  
  7. async function testLoading({ rendered: { container, getByText, history, debug } }) {
  8. await wait(() => expect(getByText('Loading...')).toBeInTheDocument(), { container });
  9. }
  10.  
  11. const journeyMachine = Machine(
  12. {
  13. id: 'journey',
  14. initial: 'IDLE',
  15. context: {},
  16. states: {
  17. IDLE: {
  18. on: {
  19. DEEP_LINK: {
  20. target: 'LOADING_CUSTOMER',
  21. actions: assign(
  22. (_context, { deepLink }) =>
  23. deepLink && {
  24. deepLink,
  25. },
  26. ),
  27. },
  28. },
  29. },
  30. LOADING_CUSTOMER: {
  31. meta: {
  32. test: testLoading,
  33. },
  34. onExit: assign((_context, { customer }) => ({
  35. customer,
  36. })),
  37. on: {
  38. LOADED_CUSTOMER: [
  39. {
  40. target: 'LOADING_SERVICEPLANS',
  41. cond: 'isPlanSelectorDeepLinked',
  42. },
  43. {
  44. target: 'CHANGE',
  45. cond: 'isChangeDeepLinked',
  46. },
  47. {
  48. target: 'LOGIN',
  49. cond: 'isLoginDeepLinkedAndUnauthenticated',
  50. },
  51. {
  52. target: 'MOVE_ADDRESS_SEARCH',
  53. cond: 'isMoveDeepLinked',
  54. },
  55. {
  56. target: 'MOVE_OR_CHANGE',
  57. cond: 'isBroadbandCustomer',
  58. },
  59. {
  60. target: 'LANDING_PAGE',
  61. },
  62. ],
  63. },
  64. },
  65. SORRY: {
  66. meta: {
  67. test: testOnPage('.SorryContainer'),
  68. },
  69. },
  70. CHANGE: {
  71. entry: assign({ journeyType: 'CHANGE' }),
  72. on: {
  73. '': [
  74. {
  75. target: 'LOGIN',
  76. cond: 'isUnauthenticated',
  77. },
  78. {
  79. target: 'CHANGE_ADDRESS_SEARCH',
  80. cond: 'customerElidUnavalibe',
  81. },
  82. {
  83. target: 'LOADING_SERVICEPLANS',
  84. },
  85. ],
  86. },
  87. },
  88. LANDING_PAGE: {
  89. meta: {
  90. test: testOnPage('.landing-container'),
  91. },
  92. on: {
  93. LOGIN_CLICK: {
  94. target: 'LOGIN',
  95. cond: 'isUnauthenticated',
  96. },
  97. ADDRESS_ENTERED: {
  98. target: 'LOADING_SERVICEPLANS',
  99. },
  100. },
  101. },
  102. LOGIN: {
  103. meta: {
  104. test: testOnPage('.LoginContainer'),
  105. },
  106. on: {
  107. LOGIN: {
  108. target: 'LOADING_CUSTOMER',
  109. },
  110. },
  111. },
  112. MOVE_OR_CHANGE: {
  113. meta: {
  114. test: testOnPage('.MoveOrChange'),
  115. },
  116. on: {
  117. MOVE_CLICKED: 'MOVE_ADDRESS_SEARCH',
  118. CHANGE_CLICKED: 'CHANGE',
  119. },
  120. },
  121. CHANGE_ADDRESS_SEARCH: {
  122. meta: {
  123. test: testOnPage('.MoveOrChange'),
  124. },
  125. on: {
  126. ADDRESS_ENTERED: {
  127. target: 'LOADING_SERVICEPLANS',
  128. },
  129. },
  130. },
  131. MOVE_ADDRESS_SEARCH: {
  132. meta: {
  133. test: testOnPage('.MoveAddress__container'),
  134. },
  135. entry: assign({ journeyType: 'CHANGE' }),
  136. on: {
  137. '': {
  138. target: 'LOGIN',
  139. cond: 'isUnauthenticated',
  140. },
  141. ADDRESS_ENTERED: {
  142. target: 'LOADING_SERVICEPLANS',
  143. },
  144. },
  145. },
  146. LOADING_SERVICEPLANS: {
  147. meta: {
  148. test: testLoading,
  149. },
  150. onExit: assign((context, { plans }) => ({
  151. plans,
  152. })),
  153. on: {
  154. LOADED: [
  155. {
  156. target: 'NO_PLAN',
  157. cond: 'noPlansAvalible',
  158. },
  159. {
  160. target: 'QUESTIONS',
  161. cond: 'needsQuestions',
  162. },
  163. {
  164. target: 'PLAN_SELECTOR',
  165. },
  166. ],
  167. },
  168. },
  169. NO_PLAN: {
  170. meta: {
  171. test: testOnPage('.NoPlanOptions'),
  172. },
  173. },
  174. PLAN_SELECTOR: {
  175. meta: {
  176. test: testOnPage('.configurator-wrapper'),
  177. },
  178. on: {
  179. ADDRESS_ENTERED: {
  180. target: 'LOADING_SERVICEPLANS',
  181. },
  182. },
  183. },
  184. QUESTIONS: {
  185. meta: {
  186. test: testOnPage('.QuestionsContainer'),
  187. },
  188. on: {
  189. USAGE_SUBMITED: {
  190. target: 'PLAN_SELECTOR',
  191. },
  192. },
  193. },
  194. },
  195. },
  196. {
  197. guards: {
  198. isNotBroadbandCustomer: withCustomer(customer => !customer.isBroadbandCustomer),
  199. isBroadbandCustomer: withCustomer(customer => customer.isBroadbandCustomer),
  200. isMoveDeepLinked: (context, event) => context.deepLink === 'MOVE' || context.journeyType === 'MOVE',
  201. isPlanSelectorDeepLinked: (context, event) => context.deepLink === 'PLAN_SELECTOR',
  202. isChangeDeepLinked: (context, event) => context.deepLink === 'CHANGE' || context.journeyType === 'CHANGE',
  203. isUnauthenticated: withCustomer(customer => !customer.isAuthenticated),
  204. isLoginDeepLinkedAndUnauthenticated: withCustomer(
  205. (customer, context) => !customer.isAuthenticated && context.deepLink === 'LOGIN',
  206. ),
  207. isAuthenticated: withCustomer(customer => customer.isAuthenticated),
  208. noPlansAvalible: withPlans(plans => plans.noPlansAvalible),
  209. needsQuestions: withPlans(plans => plans.needsQuestions),
  210. customerElidUnavalibe: withCustomer(customer => !customer.elid),
  211. },
  212. },
  213. );
  214.  
  215. function withCustomer(predicate) {
  216. return (context, event) => {
  217. const customer = event.customer || context.customer;
  218. return !!customer && predicate(customer, context, event);
  219. };
  220. }
  221.  
  222. function withPlans(predicate) {
  223. return (context, event) => {
  224. const plans = event.plans || context.plans;
  225. return !!plans && predicate(plans);
  226. };
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement