如何将redux saga产量导入我的排毒+开玩笑的测试文件中。我需要在测试设置中访问redux存储中存储的数据

时间:2018-08-17 22:33:24

标签: react-native redux react-redux jestjs redux-saga

这是一个本机应用程序,我目前正在编写一些端到端测试。

令牌存储在以下所示的redux存储中,我正在使用排毒/玩笑测试登录功能。我需要在我的login.spec.js中检测令牌是否存在于商店中。如果令牌存在,我想从商店中删除它,以便当我重新加载应用程序以将用户带回另一个场景时,该用户不会自动登录。有问题的主要功能是refreshUserToken()和以下行:-

VISIT_KEY PAT_KEY DEPT_KEY CONTACT_DT_KEY APPT_CHECKIN_DT  HOSP_ADMIT_DT    HOSP_DISCHRG_DT
85383519  8208755 5153     20150204       2/4/15 9:03 AM   NA               NA
85394064  8208755 4655     20150205       NULL             2/5/15 11:32 AM  2/8/15 7:09 PM
85482578  8208755 4931     20150213       2/13/15 2:41 PM  NA               NA
85687769  8208755 4690     20150325       3/25/15 1:25 PM  NA               NA
85706446  8208755 4804     20150325       3/25/15 1:23 PM  NA               NA
84492982  8211959 4780     20141221       NULL             12/21/14 7:22 PM 12/21/14 9:15 PM
81006886  8215742 4923     20140611       6/11/14 3:56 PM  NA               NA
83502242  8216600 4777     20141016       10/16/14 1:15 PM NA               NA
81239904  8216694 4995     20140531       5/31/14 2:19 PM  NA               NA
81825248  8216694 4655     20140616       NULL             6/16/14 9:19 AM  6/19/14 12:48 PM
81854416  8216694 4995     20141008       10/8/14 8:59 AM  NA               NA
83842766  8216694 8111     20141107       NULL             11/7/14 3:15 AM  11/10/14 4:45 PM
83883100  8216694 4777     20141113       11/13/14 2:42 PM NA               NA
85747526  8216694 4716     20150227       NULL             2/27/15 8:23 AM  2/28/15 3:04 PM
81342608  8217928 4780     20140514       NULL             5/14/14 4:07 AM  5/14/14 5:36 AM
81385263  8217928 4780     20140516       NULL             5/16/14 2:04 PM  5/16/14 5:35 PM
82517665  8218267 4777     20140808       8/8/14 2:36 PM   NA               NA
81239905  8220393 4653     20140511       5/11/14 3:44 PM  NA               NA
86144271  8223186 4780     20150326       NULL             3/26/15 4:19 PM  3/26/15 6:28 PM
83012245  8223203 4780     20140914       NULL             9/14/14 3:38 PM  9/14/14 8:22 PM
85174511  8224572 11611    20150122       1/22/15 12:44 PM NA               NA
83279874  8225752 4780     20141002       NULL             10/2/14 9:59 PM  10/3/14 2:33 AM
84456795  8227359 4777     20141219       12/19/14 9:05 AM NA               NA
85727889  8227359 4998     20150227       NULL             2/27/15 1:15 PM  2/28/15 6:40 PM
86017880  8227509 5061     20150319       3/19/15 2:48 PM  NA               NA
82909082  8230810 11611    20140912       9/12/14 8:23 AM  NA               NA

这是Redux传奇文件User.js,位于:-MyApp / App / Sagas / User.js

const { refresh_token } = yield select(token);

这是我的排毒/笑话规格文件,位于:-MyApp / App / e2e / login.spec.js

import { call, put, takeEvery, select } from "redux-saga/effects";
import Config from "MyApp/App/Config";
import API from "MyApp/App/Services/API";
import { when } from "MyApp/App/Helpers/Predicate";
import Credentials from "MyApp/App/Helpers/Credentials";
import ActionCreator from "MyApp/App/Actions";

const appendPayload = payload => {
  return {
    ...payload,
    // Removed because no longer needed unless for testing purposes.
    // username: Config.TEST_USERNAME,
    // password: Config.TEST_PASSWORD,
    client_id: Config.CLIENT_ID,
    client_secret: Config.CLIENT_SECRET,
  };
};

const token = state => state.token;

const user = state => state.user;


const attemptUserLogin = function*(action) {
  const { payload } = action;
  const login = "/oauth/token";
  const grant_type = "password";
  const loginPayload = appendPayload(payload);

  action.payload = {
    ...loginPayload,
    grant_type,
  };

  yield attemptUserAuthorisation(login, action);
};

const attemptUserRegister = function*(action) {
  const register = "/api/signup";
  const { payload } = action;
  yield Credentials.save(payload);
  yield put(ActionCreator.saveUserCredentials(payload));
  yield attemptUserAuthorisation(register, action);
};

const refreshUserToken = function*(action) {
  const login = "/oauth/token";
  const grant_type = "refresh_token";
  const { refresh_token } = yield select(token);

  action.payload = {
    ...action.payload,
    grant_type,
    refresh_token,
  };
  yield attemptUserAuthorisation(login, action);
};

const watchExampleSaga = function*() {
  yield takeEvery(ActionCreator.AUTO_USER_LOGIN, autoUserLogin);
  yield takeEvery(ActionCreator.USER_LOGIN, attemptUserLogin);
  yield takeEvery(ActionCreator.USER_REGISTER, attemptUserRegister);
  yield takeEvery(ActionCreator.USER_REFRESH_TOKEN, refreshUserToken);
};

export default watchExampleSaga;

2 个答案:

答案 0 :(得分:0)

除非有人可以给我一个解决方案,而不是模拟状态,否则看起来实际上是无法完成的,在这种情况下,我的应用程序仍然会检查真实状态以自动登录。

我确实进入了创建新动作getUserToken并将其导出到我的jest文件中的阶段。但是,该操作将返回未定义的操作,因为jest文件需要类似于container.js中的调度方法。如果有人可以用笑话为我提供这种方法,我将非常高兴。

答案 1 :(得分:0)

这是我处理类似情况的方式:

在package.json脚本中

: “ start:detox”:“ RN_SRC_EXT = e2e.tsx,e2e.ts节点node_modules / react-native / local-cli / cli.js start”,

在我的排毒配置中: “ build”:“ ENVFILE = .env.dev; RN_SRC_EXT = e2e.tsx,e2e,ts npx react-native run-ios --simulator ='iPhone 7'”,

这让我写了MyFile.e2e.tsx,它在排毒运行时替换了MyFile.tsx

在该组件的测试版本中,我有一些在测试中被点击的按钮,并且这些按钮可以调度redux操作