dimanche 3 février 2019

Unit testing Apollo, errorring on mutation mock

I'm trying to mock out a graphql mutation for my unit tests. I'm using the MockedProvider but I'm getting the error: 'UnhandledPromiseRejectionWarning: Error: No more mocked responses for the query: mutation LoginMutation'

I've done a lot of googling, and copy pasted everything into one file so that there can be no issues with imports

const LOGIN_MUTATION = gql`
    mutation LoginMutation($email: String!, $password: String!) {
    login(email: $email, password: $password) {
      token
      user {
        name
      }
    }
  }
`;

const mocks = [
  {
    request: {
      query: LOGIN_MUTATION,
      variables: { email: 'test@test.com', password: 'password' },
    },
    result: {
      data: {
        login: {
          token: 'fakeToken',
          user: {
            name: 'Testy McTestface'
          }
        }
      }
    }
  }
]

describe('LoginForm', () => {
  let wrapper;
  let store;

  beforeEach(() => {
    store = mockStore(initialState);
  });

  it('triggers the doLogin action creator after the graphql call', () => {
    const mockDoLogin = jest.fn()
    let mockedWrapper = mount(
      <MockedProvider mocks={mocks} addTypename={false}>
        <Provider store={store}>
          <LoginForm doLogin={mockDoLogin} />
        </Provider>
      </MockedProvider>,
    );
    mockedWrapper.find('#loginButton').simulate('click')
    expect(mockDoLogin).toHaveBeenCalledTimes(1)
  })
});

I expect this test to pass (the tag searched for on the wrapper is the correct one) but I get the error described above, so I'm assuming the issue is with my mock but it looks good to me?

Sorry if this is something ridiculously obvious but I've been staring at it for hours now.

Aucun commentaire:

Enregistrer un commentaire