0

I'm trying to follow the authentication guide of the urql documentation. Lets start with minimal context: I have a react app and a graphql server. We are introducing the refresh token feature and I'm implementing on the web client the refresh token flow. For that I've modified the client configuration to have the authExchange which returns a set of functions that will be used for handling specific auth operations like: didAuthError, addAuthToOperation, async refreshAuth.

My understanding is that didAuthError has the logic to determine whether the error returned by the server should trigger refreshAuth or not. Until now I've succeed implementing this when I try the app on the browser, as soon as the server returns a 401 error with the message: token has expired the refresh token mutation implemented inside the `refreshAuth is called and the new pair of tokens is used to retry the previous request and everything is fine. All that being said, the problem I'm facing is that I can't reproduce this on the test I'm writing (urql docs about testing).

const TestComponent = () => {
  const [res, execute] = useQuery({ query: 'query{ user {name }}' })
  return <div></div>
}    
it('refreshToken', async () => {
        clientConfig.executeQuery = jest.fn()
          .mockReturnValueOnce(
            fromValue({
              error: new CombinedError({
                networkError: new Error('token has expired'),
              }),
            }),
          )
          .mockReturnValueOnce(fromValue({
            data: { user: { name: 'user name' } }
          }))
    
        const wrapper = renderer.create(
          <Provider value={clientConfig}>
            <TestComponent />
          </Provider>)
    
        expect(clientConfig.executeQuery).toHaveBeenCalledTimes(2);
      });

    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 2
    Received number of calls: 1

One thing that I noticed by adding some logs is that the didAuthError returned by authExchange is not being executed on the test.

Wolgan Ens
  • 385
  • 1
  • 11

0 Answers0