Advertisement
Guest User

Untitled

a guest
Apr 10th, 2019
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.49 KB | None | 0 0
  1. import * as api from "../api";
  2. import * as act from "../../actions/types";
  3. import cloneDeep from "lodash/cloneDeep";
  4. import set from "lodash/set";
  5. import get from "lodash/fp/get";
  6. import { DEFAULT_REQUEST_STATE } from "../util";
  7. import {
  8. PROPOSAL_VOTING_ACTIVE,
  9. PROPOSAL_STATUS_PUBLIC,
  10. PROPOSAL_STATUS_UNREVIEWED,
  11. MANAGE_USER_EXPIRE_NEW_USER_VERIFICATION,
  12. MANAGE_USER_EXPIRE_UPDATE_KEY_VERIFICATION,
  13. MANAGE_USER_EXPIRE_RESET_PASSWORD_VERIFICATION,
  14. MANAGE_USER_CLEAR_USER_PAYWALL,
  15. MANAGE_USER_UNLOCK,
  16. MANAGE_USER_DEACTIVATE,
  17. MANAGE_USER_REACTIVATE
  18. } from "../../constants";
  19. import { request, receive } from "../util";
  20. import {
  21. testReceiveReducer,
  22. testReceiveProposalsReducer,
  23. testRequestReducer,
  24. testResetReducer,
  25. testResetMultipleReducer
  26. } from "./helpers";
  27.  
  28. const getUserFromState = state => state.user.response.user;
  29.  
  30. describe("test api reducer", () => {
  31. const MOCK_STATE = {
  32. commentslikes: {
  33. response: {
  34. commentslikes: [
  35. {
  36. action: "0",
  37. commentid: "1",
  38. token: "token_1"
  39. },
  40. {
  41. action: "0",
  42. commentid: "2",
  43. token: "token_1"
  44. }
  45. ]
  46. }
  47. },
  48. proposalComments: {
  49. response: {
  50. comments: [
  51. {
  52. token: "token_1",
  53. parentid: "0",
  54. comment: "This is a comment",
  55. signature: "sign",
  56. publickey: "pubkey",
  57. commentid: "1",
  58. receipt: "receipt",
  59. timestamp: 1532180179,
  60. totalvotes: 0,
  61. resultvotes: 0,
  62. userid: "0",
  63. username: "admin"
  64. },
  65. {
  66. token: "token_1",
  67. parentid: "0",
  68. comment: "This is a comment",
  69. signature: "sign",
  70. publickey: "pubkey",
  71. commentid: "2",
  72. receipt: "receipt",
  73. timestamp: 1532180179,
  74. totalvotes: 0,
  75. resultvotes: 0,
  76. userid: "0",
  77. username: "admin"
  78. }
  79. ]
  80. }
  81. },
  82. me: {
  83. response: {
  84. username: "admin",
  85. isadmin: true
  86. }
  87. },
  88. proposal: {
  89. response: {
  90. proposal: {
  91. censorshiprecord: {
  92. token: "censortoken"
  93. },
  94. files: [],
  95. username: ""
  96. }
  97. }
  98. },
  99. unvetted: {
  100. response: {
  101. proposals: [
  102. {
  103. censorshiprecord: {
  104. token: "censortoken"
  105. }
  106. },
  107. {
  108. censorshiprecord: {
  109. token: "anothertoken"
  110. }
  111. }
  112. ]
  113. }
  114. },
  115. user: {
  116. response: {
  117. user: {
  118. proposalcredits: 3
  119. }
  120. }
  121. },
  122. vetted: DEFAULT_REQUEST_STATE,
  123. userProposals: DEFAULT_REQUEST_STATE,
  124. proposalsVoteStatus: DEFAULT_REQUEST_STATE,
  125. manageUser: DEFAULT_REQUEST_STATE
  126. };
  127.  
  128. const MOCK_PROPOSALS_LOAD = [
  129. {
  130. censorshiprecord: {
  131. token: "censortoken"
  132. },
  133. status: PROPOSAL_STATUS_UNREVIEWED
  134. },
  135. {
  136. censorshiprecord: {
  137. token: "randomtoken"
  138. },
  139. status: PROPOSAL_STATUS_UNREVIEWED
  140. },
  141. {
  142. censorshiprecord: {
  143. token: "randomtoken2"
  144. },
  145. status: PROPOSAL_STATUS_PUBLIC
  146. }
  147. ];
  148.  
  149. const getCommentVoteFromState = (state, token, commentid) =>
  150. state.commentslikes.response.commentslikes.filter(
  151. cv => cv.token === token && cv.commentid === commentid
  152. )[0];
  153. const getProposalCommentFromState = (state, token, commentid) =>
  154. state.proposalComments.response.comments.filter(
  155. c => c.token === token && c.commentid === commentid
  156. )[0];
  157.  
  158. const assertStateAfterCommentVote = (
  159. state,
  160. actionPayload,
  161. expAction,
  162. expTotal,
  163. expResult
  164. ) => {
  165. const { token, commentid } = actionPayload;
  166. const initialState = cloneDeep(state);
  167.  
  168. const newState = api.onReceiveSyncLikeComment(state, {
  169. payload: actionPayload
  170. });
  171. const newCommentVote = getCommentVoteFromState(newState, token, commentid);
  172. const newComment = getProposalCommentFromState(newState, token, commentid);
  173. expect(newCommentVote).toEqual({ token, commentid, action: expAction });
  174. expect(newComment.totalvotes).toEqual(expTotal);
  175. expect(newComment.resultvotes).toEqual(expResult);
  176. expect(newState.commentslikes.backup).toEqual(
  177. initialState.commentslikes.response.commentslikes
  178. );
  179. expect(newState.proposalComments.backup).toEqual(
  180. initialState.proposalComments.response.comments
  181. );
  182.  
  183. const reducerState = api.default(state, {
  184. type: act.RECEIVE_SYNC_LIKE_COMMENT,
  185. payload: actionPayload
  186. });
  187. const newCommentVoteR = getCommentVoteFromState(
  188. reducerState,
  189. token,
  190. commentid
  191. );
  192. const newCommentR = getProposalCommentFromState(
  193. reducerState,
  194. token,
  195. commentid
  196. );
  197. expect(newCommentVoteR).toEqual({ token, commentid, action: expAction });
  198. expect(newCommentR.totalvotes).toEqual(expTotal);
  199. expect(newCommentR.resultvotes).toEqual(expResult);
  200. expect(reducerState.commentslikes.backup).toEqual(
  201. initialState.commentslikes.response.commentslikes
  202. );
  203. expect(reducerState.proposalComments.backup).toEqual(
  204. initialState.proposalComments.response.comments
  205. );
  206.  
  207. return newState;
  208. };
  209.  
  210. const addNewCommentToState = (state, action) => {
  211. return receive(
  212. "newComment",
  213. {
  214. ...state,
  215. proposalComments: {
  216. ...state.proposalComments,
  217. response: {
  218. ...state.proposalComments.response,
  219. comments: [
  220. ...state.proposalComments.response.comments,
  221. {
  222. ...state.newComment.payload,
  223. token: state.proposal.payload,
  224. userid: "1",
  225. username: state.me.response.username,
  226. isadmin: state.me.response.isadmin,
  227. totalvotes: 0,
  228. resultvotes: 0,
  229. commentid: "3"
  230. }
  231. ]
  232. }
  233. }
  234. },
  235. action
  236. );
  237. };
  238.  
  239. test("default tests for api reducer", () => {
  240. expect(api.DEFAULT_STATE).toEqual({
  241. me: DEFAULT_REQUEST_STATE,
  242. unvettedStatus: DEFAULT_REQUEST_STATE,
  243. init: DEFAULT_REQUEST_STATE,
  244. policy: DEFAULT_REQUEST_STATE,
  245. newUser: DEFAULT_REQUEST_STATE,
  246. verifyNewUser: DEFAULT_REQUEST_STATE,
  247. login: DEFAULT_REQUEST_STATE,
  248. logout: DEFAULT_REQUEST_STATE,
  249. censorComment: DEFAULT_REQUEST_STATE,
  250. vetted: DEFAULT_REQUEST_STATE,
  251. unvetted: DEFAULT_REQUEST_STATE,
  252. proposal: DEFAULT_REQUEST_STATE,
  253. invoice: DEFAULT_REQUEST_STATE,
  254. adminInvoices: DEFAULT_REQUEST_STATE,
  255. userInvoices: DEFAULT_REQUEST_STATE,
  256. proposalComments: DEFAULT_REQUEST_STATE,
  257. invoiceComments: DEFAULT_REQUEST_STATE,
  258. proposalsVoteStatus: DEFAULT_REQUEST_STATE,
  259. proposalVoteStatus: DEFAULT_REQUEST_STATE,
  260. commentslikes: DEFAULT_REQUEST_STATE,
  261. userProposals: DEFAULT_REQUEST_STATE,
  262. newProposal: DEFAULT_REQUEST_STATE,
  263. editProposal: DEFAULT_REQUEST_STATE,
  264. newInvoice: DEFAULT_REQUEST_STATE,
  265. editInvoice: DEFAULT_REQUEST_STATE,
  266. newComment: DEFAULT_REQUEST_STATE,
  267. forgottenPassword: DEFAULT_REQUEST_STATE,
  268. passwordReset: DEFAULT_REQUEST_STATE,
  269. changeUsername: DEFAULT_REQUEST_STATE,
  270. changePassword: DEFAULT_REQUEST_STATE,
  271. updateUserKey: DEFAULT_REQUEST_STATE,
  272. verifyUserKey: DEFAULT_REQUEST_STATE,
  273. likeComment: DEFAULT_REQUEST_STATE,
  274. userSearch: DEFAULT_REQUEST_STATE,
  275. proposalPaywallPayment: DEFAULT_REQUEST_STATE,
  276. rescanUserPayments: DEFAULT_REQUEST_STATE,
  277. user: DEFAULT_REQUEST_STATE,
  278. proposalPaywallDetails: DEFAULT_REQUEST_STATE,
  279. email: "",
  280. keyMismatch: false,
  281. lastLoaded: {}
  282. });
  283.  
  284. expect(api.default(undefined, { type: "" })).toEqual(api.DEFAULT_STATE);
  285.  
  286. // logout action
  287. let action = {
  288. type: act.RECEIVE_LOGOUT
  289. };
  290.  
  291. expect(api.default(undefined, action)).toEqual(api.DEFAULT_STATE);
  292.  
  293. // key mismatch action
  294. action = {
  295. type: act.KEY_MISMATCH,
  296. payload: "warning msg"
  297. };
  298.  
  299. expect(api.default({}, action)).toEqual({ keyMismatch: action.payload });
  300.  
  301. // load me action
  302. action = {
  303. type: act.LOAD_ME,
  304. payload: "me data"
  305. };
  306.  
  307. expect(api.default({}, action)).toEqual({ me: action.payload });
  308.  
  309. // set email action
  310. action = {
  311. type: act.SET_EMAIL,
  312. payload: "any@any.com"
  313. };
  314.  
  315. expect(api.default({}, action)).toEqual({ email: action.payload });
  316.  
  317. // clean errors action
  318. action = {
  319. type: act.CLEAN_ERRORS
  320. };
  321.  
  322. const state = {
  323. me: {
  324. error: true
  325. },
  326. api: {
  327. error: true
  328. },
  329. app: "test"
  330. };
  331.  
  332. expect(api.default(state, action)).toEqual({
  333. me: {
  334. error: null
  335. },
  336. api: {
  337. error: null
  338. },
  339. app: "test"
  340. });
  341. });
  342.  
  343. test("correctly updates the state for onReceiveSyncLikeComment", () => {
  344. const token = "token_1";
  345. const commentid = "1";
  346. const actionPayload = { token, commentid };
  347. const initialState = cloneDeep(MOCK_STATE);
  348.  
  349. actionPayload.action = 1;
  350. let state = {
  351. ...assertStateAfterCommentVote(initialState, actionPayload, 1, 1, 1)
  352. };
  353.  
  354. // note: stateAux is being used only to bypass eslint
  355. actionPayload.action = 1;
  356. const stateAux = assertStateAfterCommentVote(state, actionPayload, 0, 0, 0);
  357.  
  358. actionPayload.action = 1;
  359. state = assertStateAfterCommentVote(stateAux, actionPayload, 1, 1, 1);
  360.  
  361. actionPayload.action = -1;
  362. state = assertStateAfterCommentVote(state, actionPayload, -1, 1, -1);
  363.  
  364. actionPayload.action = -1;
  365. state = assertStateAfterCommentVote(state, actionPayload, 0, 0, 0);
  366.  
  367. actionPayload.action = -1;
  368. state = assertStateAfterCommentVote(state, actionPayload, -1, 1, -1);
  369.  
  370. actionPayload.action = 1;
  371. state = assertStateAfterCommentVote(state, actionPayload, 1, 1, 1);
  372.  
  373. actionPayload.commentid = "2";
  374. actionPayload.action = 1;
  375. state = assertStateAfterCommentVote(state, actionPayload, 1, 1, 1);
  376. });
  377.  
  378. test("correctly reset the state for onResetSyncLikeComment", () => {
  379. const token = "token_1";
  380. const commentid = "1";
  381. const actionPayload = { token, commentid };
  382. const initialState = cloneDeep(MOCK_STATE);
  383.  
  384. actionPayload.action = 1;
  385. const state = assertStateAfterCommentVote(
  386. MOCK_STATE,
  387. actionPayload,
  388. 1,
  389. 1,
  390. 1
  391. );
  392. const stateAfterReset = api.onResetSyncLikeComment(state);
  393. const stateAfterResetR = api.default(state, {
  394. type: act.RESET_SYNC_LIKE_COMMENT
  395. });
  396. expect(stateAfterReset.commentslikes.response.commentslikes).toEqual(
  397. initialState.commentslikes.response.commentslikes
  398. );
  399. expect(stateAfterReset.proposalComments.response.comments).toEqual(
  400. initialState.proposalComments.response.comments
  401. );
  402. expect(stateAfterResetR.commentslikes.response.commentslikes).toEqual(
  403. initialState.commentslikes.response.commentslikes
  404. );
  405. expect(stateAfterResetR.proposalComments.response.comments).toEqual(
  406. initialState.proposalComments.response.comments
  407. );
  408. });
  409.  
  410. test("correctly updates state for onReceiveNewComment, adds new comment and nothing else", () => {
  411. const action = {
  412. type: act.RECEIVE_NEW_COMMENT,
  413. payload: {
  414. userid: "1",
  415. commentid: "3"
  416. },
  417. error: false
  418. };
  419.  
  420. let state = {
  421. ...MOCK_STATE,
  422. newComment: {
  423. payload: {
  424. parentid: "0",
  425. comment: "This is a new comment",
  426. signature: "sign",
  427. publickey: "pubkey",
  428. receipt: "receipt"
  429. }
  430. }
  431. };
  432.  
  433. let newState = api.onReceiveNewComment(state, action);
  434. let expectedState = addNewCommentToState(state, action);
  435. for (
  436. let i = 0;
  437. i < newState.proposalComments.response.comments.length;
  438. i++
  439. ) {
  440. if (newState.proposalComments.response.comments[i])
  441. delete newState.proposalComments.response.comments[i].timestamp;
  442. }
  443. expect(expectedState).toEqual(newState);
  444.  
  445. const reducerState = api.default(state, action);
  446. for (
  447. let i = 0;
  448. i < reducerState.proposalComments.response.comments.length;
  449. i++
  450. ) {
  451. if (reducerState.proposalComments.response.comments[i])
  452. delete reducerState.proposalComments.response.comments[i].timestamp;
  453. }
  454. expect(expectedState).toEqual(reducerState);
  455.  
  456. state = {
  457. ...expectedState,
  458. newComment: {
  459. payload: {
  460. parentid: "0",
  461. comment: "This is another new comment",
  462. signature: "sign",
  463. publickey: "pubkey",
  464. receipt: "receipt"
  465. }
  466. }
  467. };
  468.  
  469. newState = api.onReceiveNewComment(state, action);
  470. expectedState = addNewCommentToState(state, action);
  471. for (
  472. let i = 0;
  473. i < newState.proposalComments.response.comments.length;
  474. i++
  475. ) {
  476. if (newState.proposalComments.response.comments[i])
  477. delete newState.proposalComments.response.comments[i].timestamp;
  478. }
  479. expect(expectedState).toEqual(newState);
  480.  
  481. const action2 = {
  482. payload: "",
  483. error: true
  484. };
  485.  
  486. expect(api.onReceiveNewComment({}, action2)).toEqual(
  487. receive("newComment", {}, action2)
  488. );
  489. });
  490.  
  491. test("correctly updates status state for onReceiveSetStatus (unvetted -> vetted)", () => {
  492. const proposalUpdated = {
  493. ...MOCK_STATE.unvetted.response.proposals[0],
  494. status: 4,
  495. files: [],
  496. username: ""
  497. };
  498.  
  499. const action = {
  500. type: act.RECEIVE_SETSTATUS_PROPOSAL,
  501. payload: {
  502. proposal: proposalUpdated
  503. },
  504. error: false
  505. };
  506.  
  507. let state = request("setStatusProposal", MOCK_STATE, action);
  508. state = api.onReceiveSetStatus(state, action);
  509.  
  510. expect(api.default(state, action).proposal.response.proposal).toEqual(
  511. proposalUpdated
  512. );
  513.  
  514. // updates status to 'vetted' for the proposal with token 'censortoken'
  515. expect(state.proposal.response.proposal).toEqual(proposalUpdated);
  516.  
  517. // make sure the proposal was removed from the unvetted list
  518. expect(state.unvetted.response.proposals).toEqual(
  519. MOCK_STATE.unvetted.response.proposals.filter((_, i) => i !== 0)
  520. );
  521.  
  522. // make sure the proposal was added to the vetted list
  523. expect(state.vetted.response.proposals).toEqual([proposalUpdated]);
  524. });
  525.  
  526. test("correcly updates status state for onReceiveSetStatus (unvetted -> censored)", () => {
  527. const proposalUpdated = {
  528. ...MOCK_STATE.unvetted.response.proposals[0],
  529. status: 3,
  530. files: [],
  531. username: ""
  532. };
  533.  
  534. const action = {
  535. type: act.RECEIVE_SETSTATUS_PROPOSAL,
  536. payload: {
  537. proposal: proposalUpdated
  538. },
  539. error: false
  540. };
  541.  
  542. let state = request("setStatusProposal", MOCK_STATE, action);
  543. state = api.onReceiveSetStatus(state, action);
  544.  
  545. expect(api.default(state, action).proposal.response.proposal).toEqual(
  546. proposalUpdated
  547. );
  548.  
  549. // updates status to 'censored' for the proposal with token 'censortoken'
  550. expect(state.proposal.response.proposal).toEqual(proposalUpdated);
  551.  
  552. // make sure the proposal was updated and kept in the unvetted list
  553. expect(state.unvetted.response.proposals[0]).toEqual(proposalUpdated);
  554.  
  555. // make sure vetted list is still the same
  556. expect(state.vetted.response.proposals).toEqual([]);
  557. });
  558.  
  559. test("correcly updates state for onReceiveStartVote", () => {
  560. const action = {
  561. type: act.RECEIVE_START_VOTE,
  562. payload: {
  563. token: "token"
  564. },
  565. error: false
  566. };
  567.  
  568. const newVoteStatus = {
  569. token: action.payload.token,
  570. status: PROPOSAL_VOTING_ACTIVE,
  571. optionsresult: null,
  572. totalvotes: 0
  573. };
  574.  
  575. const state = request("startVote", MOCK_STATE, action);
  576.  
  577. const stateWithoutVotes = {
  578. ...state,
  579. proposalsVoteStatus: {
  580. response: {
  581. votesstatus: null
  582. }
  583. },
  584. proposalVoteStatus: {
  585. response: {
  586. newVoteStatus: null
  587. }
  588. }
  589. };
  590.  
  591. const stateWithVotes = {
  592. ...state,
  593. proposalsVoteStatus: {
  594. response: {
  595. votesstatus: [
  596. { token: "anothertoken" },
  597. { token: action.payload.token }
  598. ]
  599. }
  600. },
  601. proposalVoteStatus: {
  602. response: {
  603. newVoteStatus: newVoteStatus
  604. }
  605. }
  606. };
  607.  
  608. let newState = api.onReceiveStartVote(stateWithoutVotes, action);
  609. expect(newState.proposalsVoteStatus.response.votesstatus[0]).toEqual(
  610. newVoteStatus
  611. );
  612. const reducerState = api.default(stateWithVotes, action);
  613. expect(reducerState.proposalsVoteStatus.response.votesstatus).toEqual([
  614. { token: "anothertoken" },
  615. newVoteStatus
  616. ]);
  617. newState = api.onReceiveStartVote(stateWithVotes, action);
  618. expect(newState.proposalsVoteStatus.response.votesstatus).toEqual([
  619. { token: "anothertoken" },
  620. newVoteStatus
  621. ]);
  622. });
  623.  
  624. test("correctly updates the state for onReceiveProposals", () => {
  625. const key = "userProposals";
  626.  
  627. const action = {
  628. type: act.RECEIVE_USER_PROPOSALS,
  629. payload: {
  630. proposals: MOCK_PROPOSALS_LOAD
  631. }
  632. };
  633. const state = api.onReceiveProposals(key, MOCK_STATE, action);
  634.  
  635. // check if the unvetted proposals were correctly updated
  636. const { proposals: unvettedResult } = state.unvetted.response;
  637. expect(unvettedResult.length).toEqual(3);
  638. expect(unvettedResult[0].censorshiprecord.token).toEqual("censortoken");
  639.  
  640. // check if the vetted proposals were correctly updated
  641. const { proposals: vettedResult } = state.vetted.response;
  642. expect(vettedResult.length).toEqual(1);
  643. expect(vettedResult[0].censorshiprecord.token).toEqual("randomtoken2");
  644. });
  645.  
  646. test("correctly updates the state for onReceiveUser", () => {
  647. const action = {
  648. type: act.RECEIVE_USER,
  649. payload: {
  650. user: {
  651. proposals: MOCK_PROPOSALS_LOAD
  652. }
  653. }
  654. };
  655.  
  656. const state = api.onReceiveUser(MOCK_STATE, action);
  657.  
  658. // check if the unvetted proposals were correctly updated
  659. const { proposals: unvettedResult } = state.unvetted.response;
  660. expect(unvettedResult.length).toEqual(3);
  661. expect(unvettedResult[0].censorshiprecord.token).toEqual("censortoken");
  662.  
  663. // check if the vetted proposals were correctly updated
  664. const { proposals: vettedResult } = state.vetted.response;
  665. expect(vettedResult.length).toEqual(1);
  666. expect(vettedResult[0].censorshiprecord.token).toEqual("randomtoken2");
  667. });
  668.  
  669. test("correctly updates the state for for onReceiveRescanUserPayments", () => {
  670. const action = {
  671. type: act.RECEIVE_RESCAN_USER_PAYMENTS,
  672. payload: {
  673. newcredits: [{ paywallid: 1 }, { paywallid: 2 }]
  674. }
  675. };
  676.  
  677. const state = api.onReceiveRescanUserPayments(MOCK_STATE, action);
  678.  
  679. // make sure the user credits are updated
  680. const { user } = state.user.response;
  681. expect(user.proposalcredits).toEqual(
  682. getUserFromState(MOCK_STATE).proposalcredits + 2
  683. );
  684.  
  685. // make sure the state has received the entire response from the request
  686. expect(state.rescanUserPayments.response.newcredits).toBeTruthy();
  687. });
  688.  
  689. test("correctly updates the state for onReceiveManageUser", () => {
  690. const getUserFromState = state => get(["user", "response", "user"], state);
  691. const updateStateForManageUser = (state, manageUserAction) => {
  692. // simulate the update of state for 'REQUEST_MANAGE_USER'
  693. const stateAfterRequest = set(state, ["manageUser", "payload"], {
  694. action: manageUserAction
  695. });
  696.  
  697. const action = {
  698. type: act.RECEIVE_MANAGE_USER,
  699. payload: {}
  700. };
  701.  
  702. return api.onReceiveManageUser(stateAfterRequest, action);
  703. };
  704. let resultState = null;
  705. let user = null;
  706.  
  707. resultState = updateStateForManageUser(
  708. MOCK_STATE,
  709. MANAGE_USER_EXPIRE_UPDATE_KEY_VERIFICATION
  710. );
  711. user = getUserFromState(resultState);
  712. expect(user.updatekeyverificationexpiry).toBeTruthy();
  713. expect(user.updatekeyverificationexpiry * 1000).toBeLessThan(
  714. new Date().getTime()
  715. );
  716.  
  717. resultState = updateStateForManageUser(
  718. MOCK_STATE,
  719. MANAGE_USER_EXPIRE_NEW_USER_VERIFICATION
  720. );
  721. user = getUserFromState(resultState);
  722. expect(user.newuserverificationexpiry).toBeTruthy();
  723. expect(user.newuserverificationexpiry * 1000).toBeLessThan(
  724. new Date().getTime()
  725. );
  726.  
  727. resultState = updateStateForManageUser(
  728. MOCK_STATE,
  729. MANAGE_USER_EXPIRE_RESET_PASSWORD_VERIFICATION
  730. );
  731. user = getUserFromState(resultState);
  732. expect(user.resetpasswordverificationexpiry).toBeTruthy();
  733. expect(user.resetpasswordverificationexpiry * 1000).toBeLessThan(
  734. new Date().getTime()
  735. );
  736.  
  737. // test state after clearing user paywall
  738. resultState = updateStateForManageUser(
  739. MOCK_STATE,
  740. MANAGE_USER_CLEAR_USER_PAYWALL
  741. );
  742. user = getUserFromState(resultState);
  743. expect(user.newuserpaywalladdress).toEqual("");
  744. expect(user.newuserpaywallamount).toEqual(0);
  745.  
  746. // test state after unlocking user
  747. resultState = updateStateForManageUser(MOCK_STATE, MANAGE_USER_UNLOCK);
  748. user = getUserFromState(resultState);
  749. expect(user.islocked).toEqual(false);
  750.  
  751. // test state after deactivating user
  752. resultState = updateStateForManageUser(MOCK_STATE, MANAGE_USER_DEACTIVATE);
  753. user = getUserFromState(resultState);
  754. expect(user.isdeactivated).toEqual(true);
  755.  
  756. // test state after reactivating user
  757. resultState = updateStateForManageUser(MOCK_STATE, MANAGE_USER_REACTIVATE);
  758. user = getUserFromState(resultState);
  759. expect(user.isdeactivated).toEqual(false);
  760. });
  761.  
  762. test("correctly updates state for reducers using request/receive/reset", () => {
  763. const reducers = [
  764. { action: act.REQUEST_ME, key: "me", type: "request" },
  765. { action: act.RECEIVE_ME, key: "me", type: "receive" },
  766. { action: act.UPDATE_ME, key: "me", type: "receive" },
  767. { action: act.REQUEST_INIT_SESSION, key: "init", type: "request" },
  768. { action: act.RECEIVE_INIT_SESSION, key: "init", type: "receive" },
  769. { action: act.REQUEST_POLICY, key: "policy", type: "request" },
  770. { action: act.RECEIVE_POLICY, key: "policy", type: "receive" },
  771. { action: act.REQUEST_NEW_USER, key: "newUser", type: "request" },
  772. { action: act.RECEIVE_NEW_USER, key: "newUser", type: "receive" },
  773. { action: act.RESET_NEW_USER, key: "newUser", type: "reset" },
  774. {
  775. action: act.REQUEST_VERIFY_NEW_USER,
  776. key: "verifyNewUser",
  777. type: "request"
  778. },
  779. {
  780. action: act.RECEIVE_VERIFY_NEW_USER,
  781. key: "verifyNewUser",
  782. type: "receive"
  783. },
  784. { action: act.REQUEST_USER, key: "user", type: "request" },
  785. { action: act.REQUEST_LOGIN, key: "login", type: "request" },
  786. { action: act.RECEIVE_LOGIN, key: "login", type: "receive" },
  787. {
  788. action: act.REQUEST_CHANGE_USERNAME,
  789. key: "changeUsername",
  790. type: "request"
  791. },
  792. {
  793. action: act.RECEIVE_CHANGE_USERNAME,
  794. key: "changeUsername",
  795. type: "receive"
  796. },
  797. {
  798. action: act.REQUEST_CHANGE_PASSWORD,
  799. key: "changePassword",
  800. type: "request"
  801. },
  802. {
  803. action: act.RECEIVE_CHANGE_PASSWORD,
  804. key: "changePassword",
  805. type: "receive"
  806. },
  807. {
  808. action: act.REQUEST_USER_PROPOSALS,
  809. key: "userProposals",
  810. type: "request"
  811. },
  812. { action: act.REQUEST_VETTED, key: "vetted", type: "request" },
  813. { action: act.REQUEST_UNVETTED, key: "unvetted", type: "request" },
  814. { action: act.REQUEST_PROPOSAL, key: "proposal", type: "request" },
  815. { action: act.RECEIVE_PROPOSAL, key: "proposal", type: "receive" },
  816. {
  817. action: act.REQUEST_PROPOSAL_COMMENTS,
  818. key: "proposalComments",
  819. type: "request"
  820. },
  821. {
  822. action: act.RECEIVE_PROPOSAL_COMMENTS,
  823. key: "proposalComments",
  824. type: "receive"
  825. },
  826. {
  827. action: act.REQUEST_INVOICE,
  828. key: "invoice",
  829. type: "request"
  830. },
  831. { action: act.REQUEST_LIKE_COMMENT, key: "likeComment", type: "request" },
  832. { action: act.RECEIVE_LIKE_COMMENT, key: "likeComment", type: "receive" },
  833. {
  834. action: act.REQUEST_LIKED_COMMENTS,
  835. key: "commentslikes",
  836. type: "request"
  837. },
  838. {
  839. action: act.RECEIVE_LIKED_COMMENTS,
  840. key: "commentslikes",
  841. type: "receive"
  842. },
  843. { action: act.REQUEST_MANAGE_USER, key: "manageUser", type: "request" },
  844. { action: act.REQUEST_NEW_PROPOSAL, key: "newProposal", type: "request" },
  845. { action: act.RECEIVE_NEW_PROPOSAL, key: "newProposal", type: "receive" },
  846. {
  847. action: act.REQUEST_EDIT_PROPOSAL,
  848. key: "editProposal",
  849. type: "request"
  850. },
  851. {
  852. action: act.RECEIVE_EDIT_PROPOSAL,
  853. key: "editProposal",
  854. type: "receive"
  855. },
  856. { action: act.REQUEST_NEW_COMMENT, key: "newComment", type: "request" },
  857. {
  858. action: act.REQUEST_PROPOSAL_PAYWALL_DETAILS,
  859. key: "proposalPaywallDetails",
  860. type: "request"
  861. },
  862. {
  863. action: act.RECEIVE_PROPOSAL_PAYWALL_DETAILS,
  864. key: "proposalPaywallDetails",
  865. type: "receive"
  866. },
  867. {
  868. action: act.REQUEST_UPDATE_PROPOSAL_CREDITS,
  869. key: "updateProposalCredits",
  870. type: "request"
  871. },
  872. {
  873. action: act.RECEIVE_UPDATE_PROPOSAL_CREDITS,
  874. key: "updateProposalCredits",
  875. type: "receive"
  876. },
  877. {
  878. action: act.REQUEST_USER_PROPOSAL_CREDITS,
  879. key: "userProposalCredits",
  880. type: "request"
  881. },
  882. {
  883. action: act.RECEIVE_USER_PROPOSAL_CREDITS,
  884. key: "userProposalCredits",
  885. type: "receive"
  886. },
  887. {
  888. action: act.REQUEST_FORGOTTEN_PASSWORD_REQUEST,
  889. key: "forgottenPassword",
  890. type: "request"
  891. },
  892. {
  893. action: act.RECEIVE_FORGOTTEN_PASSWORD_REQUEST,
  894. key: "forgottenPassword",
  895. type: "receive"
  896. },
  897. {
  898. action: act.RESET_FORGOTTEN_PASSWORD_REQUEST,
  899. key: "forgottenPassword",
  900. type: "reset"
  901. },
  902. {
  903. action: act.REQUEST_RESEND_VERIFICATION_EMAIL,
  904. key: "resendVerificationEmail",
  905. type: "request"
  906. },
  907. {
  908. action: act.RECEIVE_RESEND_VERIFICATION_EMAIL,
  909. key: "resendVerificationEmail",
  910. type: "receive"
  911. },
  912. {
  913. action: act.RESET_RESEND_VERIFICATION_EMAIL,
  914. key: "resendVerificationEmail",
  915. type: "reset"
  916. },
  917. {
  918. action: act.REQUEST_PASSWORD_RESET_REQUEST,
  919. key: "passwordReset",
  920. type: "request"
  921. },
  922. {
  923. action: act.RECEIVE_PASSWORD_RESET_REQUEST,
  924. key: "passwordReset",
  925. type: "receive"
  926. },
  927. {
  928. action: act.RESET_PROPOSAL,
  929. key: ["newProposal", "editProposal"],
  930. type: "resetMultiple"
  931. },
  932. {
  933. action: act.RESET_INVOICE,
  934. key: ["newInvoice", "editInvoice"],
  935. type: "resetMultiple"
  936. },
  937. {
  938. action: act.REQUEST_SETSTATUS_PROPOSAL,
  939. key: "setStatusProposal",
  940. type: "request"
  941. },
  942. { action: act.REQUEST_START_VOTE, key: "startVote", type: "request" },
  943. {
  944. action: act.REQUEST_UPDATED_KEY,
  945. key: "updateUserKey",
  946. type: "request"
  947. },
  948. {
  949. action: act.RECEIVE_UPDATED_KEY,
  950. key: "updateUserKey",
  951. type: "receive"
  952. },
  953. {
  954. action: act.REQUEST_VERIFIED_KEY,
  955. key: "verifyUserKey",
  956. type: "request"
  957. },
  958. {
  959. action: act.RECEIVE_VERIFIED_KEY,
  960. key: "verifyUserKey",
  961. type: "receive"
  962. },
  963. { action: act.REQUEST_LOGOUT, key: "logout", type: "request" },
  964. {
  965. action: act.REQUEST_PROPOSALS_VOTE_STATUS,
  966. key: "proposalsVoteStatus",
  967. type: "request"
  968. },
  969. {
  970. action: act.RECEIVE_PROPOSALS_VOTE_STATUS,
  971. key: "proposalsVoteStatus",
  972. type: "receive"
  973. },
  974. {
  975. action: act.REQUEST_PROPOSAL_VOTE_STATUS,
  976. key: "proposalVoteStatus",
  977. type: "request"
  978. },
  979. {
  980. action: act.RECEIVE_PROPOSAL_VOTE_STATUS,
  981. key: "proposalVoteStatus",
  982. type: "receive"
  983. },
  984. {
  985. action: act.REQUEST_PROPOSAL_PAYWALL_PAYMENT,
  986. key: "proposalPaywallPayment",
  987. type: "request"
  988. },
  989. {
  990. action: act.RECEIVE_PROPOSAL_PAYWALL_PAYMENT,
  991. key: "proposalPaywallPayment",
  992. type: "receive"
  993. },
  994. { action: act.REQUEST_USER_SEARCH, key: "userSearch", type: "request" },
  995. { action: act.RECEIVE_USER_SEARCH, key: "userSearch", type: "receive" },
  996. {
  997. action: act.REQUEST_RESCAN_USER_PAYMENTS,
  998. key: "rescanUserPayments",
  999. type: "request"
  1000. }
  1001. ];
  1002.  
  1003. reducers.map(({ action, key, type }) => {
  1004. const MOCK_ACTION = {
  1005. type: action,
  1006. payload: "data",
  1007. error: false
  1008. };
  1009.  
  1010. const MOCK_PROPOSALS_ACTION = {
  1011. type: action,
  1012. payload: {
  1013. proposals: []
  1014. },
  1015. error: false
  1016. };
  1017.  
  1018. switch (type) {
  1019. case "request":
  1020. testRequestReducer(api.default, key, {}, MOCK_ACTION);
  1021. break;
  1022. case "receive":
  1023. testReceiveReducer(api.default, key, {}, MOCK_ACTION);
  1024. break;
  1025. case "receiveProposals":
  1026. testReceiveProposalsReducer(
  1027. api.default,
  1028. key,
  1029. {},
  1030. MOCK_PROPOSALS_ACTION
  1031. );
  1032. break;
  1033. case "reset":
  1034. testResetReducer(api.default, key, {}, MOCK_ACTION);
  1035. break;
  1036. case "resetMultiple":
  1037. testResetMultipleReducer(api.default, key, {}, MOCK_ACTION);
  1038. break;
  1039. default:
  1040. break;
  1041. }
  1042. });
  1043. });
  1044. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement