使用 MSW 获取身份验证令牌| React-Redux-测试

发布于 2025-01-15 17:09:32 字数 714 浏览 2 评论 0原文

我正在尝试使用 msw 库登录并遵循 Redux 文档。然后我想获取用户身份验证令牌。我正在发送登录请求,但不知道登录后如何获取身份验证令牌。示例中的结构对于登录正确吗?如果不是的话应该怎样

export const handlers = [
  rest.post("/login", (req, res, ctx) => {
    return res(
      ctx.json({
        id: "1234",
        firstName: "John",
        lastName: "Maverick",
      })
    );
  })

const server = setupServer(...handlers);

// Enable API mocking before tests.
beforeAll(() => server.listen());

// Reset any runtime request handlers we may add during the tests.
afterEach(() => server.resetHandlers());

// Disable API mocking after the tests are done.
afterAll(() => server.close());

I'm trying to login using the msw library and following the Redux docs. Then I want to get the user auth token. I'm sending request for login but idk how to get auth token if logged in. Is the structure in the example correct for login? If not, how should it be

export const handlers = [
  rest.post("/login", (req, res, ctx) => {
    return res(
      ctx.json({
        id: "1234",
        firstName: "John",
        lastName: "Maverick",
      })
    );
  })

const server = setupServer(...handlers);

// Enable API mocking before tests.
beforeAll(() => server.listen());

// Reset any runtime request handlers we may add during the tests.
afterEach(() => server.resetHandlers());

// Disable API mocking after the tests are done.
afterAll(() => server.close());

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文