将更新属性添加到突变功能中断模拟的结果中的结果

发布于 2025-02-08 07:01:53 字数 2277 浏览 2 评论 0原文

我有以下功能在表单提交上触发的功能

const [register, { loading }] = useMutation(RegisterDocument);

const router = useRouter();

const onSubmit = async (values: FormValues) => {
  const v = { ...values };
  delete v.confirmPassword;
  const res = await register({
    variables: { options: v },
    update: (cache, { data }) => {
      cache.writeQuery<MeQuery>({
        query: MeDocument,
        data: {
          __typename: 'Query',
          me: data?.register.user,
        },
      });
    },
  });
  if (res.data?.register.user) {
    router.push('/');
  }
};

进行以下测试以提交表格,

test('it should submit form without error', async () => {
  const firstName = faker.name.firstName();
  const surname = faker.name.lastName();
  const username = faker.internet.userName().replace('@', '');
  const email = faker.internet.email();
  const password = faker.internet.password(6, false, /^[a-zA-Z0-9_.-]*$/);

  const cache = new InMemoryCache().restore({});

  const variables = {
    options: { email, firstName, password, surname, username },
  };

  const user = { email, firstName, surname, username, id: 1, activated: false, photo: null };

  const mocks = [
    {
      request: { query: RegisterDocument, variables },
      result: { data: { register: { errors: null, user } } },
    },
  ];

  const { queryByTestId, container } = renderWithTheme(
    <MockedProvider mocks={mocks} cache={cache}>
      <Register />
    </MockedProvider>,
  );

  await updateRegisterInputs(container); // util function that updates input values for submission

  await submitForm({ queryByTestId, testId: 'register-submit', loadingTestId: 'register-loading' }); // util function that submits form

  await waitFor(() => expect(onPush).toBeCalledWith('/'));
});

,然后在我运行此测试res

{ data: { register: {} } }

但是,一旦我删除了更新 寄存器中的属性突变功能,res返回以下内容。

{ data: { register: { errors: null, user: [Object] } } }

为什么仅在添加update> update属性函数时,为什么模拟返回值返回register属性的空对象?

即使只是实例化更新属性也是如此;

update: () => {}

仍然打破了突变的反应。

I've got the following function that gets triggered on a form submission

const [register, { loading }] = useMutation(RegisterDocument);

const router = useRouter();

const onSubmit = async (values: FormValues) => {
  const v = { ...values };
  delete v.confirmPassword;
  const res = await register({
    variables: { options: v },
    update: (cache, { data }) => {
      cache.writeQuery<MeQuery>({
        query: MeDocument,
        data: {
          __typename: 'Query',
          me: data?.register.user,
        },
      });
    },
  });
  if (res.data?.register.user) {
    router.push('/');
  }
};

I then have the following test to submit the form

test('it should submit form without error', async () => {
  const firstName = faker.name.firstName();
  const surname = faker.name.lastName();
  const username = faker.internet.userName().replace('@', '');
  const email = faker.internet.email();
  const password = faker.internet.password(6, false, /^[a-zA-Z0-9_.-]*$/);

  const cache = new InMemoryCache().restore({});

  const variables = {
    options: { email, firstName, password, surname, username },
  };

  const user = { email, firstName, surname, username, id: 1, activated: false, photo: null };

  const mocks = [
    {
      request: { query: RegisterDocument, variables },
      result: { data: { register: { errors: null, user } } },
    },
  ];

  const { queryByTestId, container } = renderWithTheme(
    <MockedProvider mocks={mocks} cache={cache}>
      <Register />
    </MockedProvider>,
  );

  await updateRegisterInputs(container); // util function that updates input values for submission

  await submitForm({ queryByTestId, testId: 'register-submit', loadingTestId: 'register-loading' }); // util function that submits form

  await waitFor(() => expect(onPush).toBeCalledWith('/'));
});

When I run this test res returns the following

{ data: { register: {} } }

However, once I remove the update property inside the register mutation function, res returns the following.

{ data: { register: { errors: null, user: [Object] } } }

Any ideas why the mocked return value returns an empty object for the register property only when the update property function is added?

Even just instantiating the update property like so;

update: () => {}

still breaks the response from the mutation.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2025-02-15 07:01:53

我意识到GraphQl Doc需要在模拟中相关位置中的typename属性,

因此我必须更新模拟以包含类型。

const user = { email, firstName, surname, username, id: 1, activated: false, photo: null, __typename: 'User' };

const mocks = [
  {
    request: { query: RegisterDocument, variables },
    result: { data: { register: { errors: null, user, __typename: 'UserResponse' } } },
  },
];

I realised that the graphql doc required the __typename property in the relevant places in my mocks

So I have to update the mock to include the typenames.

const user = { email, firstName, surname, username, id: 1, activated: false, photo: null, __typename: 'User' };

const mocks = [
  {
    request: { query: RegisterDocument, variables },
    result: { data: { register: { errors: null, user, __typename: 'UserResponse' } } },
  },
];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文