Skip to content

TypeError: Class constructor Mutation cannot be invoked without 'new' #5

@joffotron

Description

@joffotron

Hi, I'm having a little trouble testing a mutation:

I'm getting

TypeError: Class constructor Mutation cannot be invoked without 'new'

      1 | import Relay from "react-relay/classic";
      2 |
    > 3 | export default class UpdateBidMutation extends Relay.Mutation {
      4 |   getMutation() {
      5 |     return Relay.QL`mutation{updateBid}`;
      6 |   }

When testing the following code - could this be a problem with how the testing utils are mocking out Relay? The code works perfectly in production, it just blows up on this test

// test/__mocks__/react-relay.js:

import relayTestingUtils from 'relay-testing-utils'
const relay = jest.genMockFromModule('react-relay/classic');
export default relayTestingUtils.relayMock(relay)
// component.js:

import React from "react";
import Relay from "react-relay/classic";
import UpdateBidMutation from "../../mutations/update-bid";
//...
class UndoButton extends React.Component {

  undo() {
    const newState = { // ... irrelevant stuff here};

    let mutation = new UpdateBidMutation({
      bid: {
        id: this.props.bid.id,
        ...newState
      }
    });
  //...
  }
}

export default Relay.createContainer(UndoButton, {
  fragments: {
    bid: () => Relay.QL`
      fragment on Bid {
        id
        state
      }  
    `
  }
});
// mutations/update-bid.js:

import Relay from "react-relay/classic";

export default class UpdateBidMutation extends Relay.Mutation {
  getMutation() {
    return Relay.QL`mutation{updateBid}`;
  }
//.. etc etc
// undo-button.spec.js:

import React from "react";
import Relay from "react-relay/classic";
import {mount} from "enzyme";
import relayTestingUtils from "relay-testing-utils";
import UndoButton from "./undo-button";
//...
  describe("Undo mutation", () => {
    let spy;
    let wrapper;

    beforeEach(() => {
      spy = jest.fn();
      Relay.Store.mockCommitUpdate(spy);
      wrapper = mount(relayTestingUtils.relayWrap(<UndoButton {...fixture} {...props}/>));
    });

    it("fires an UpdateBidMutation", () => {
      wrapper.find("a").simulate("click");
      expect(spy.mock.calls[0][0].getVariables().text).toBe("something");
    });
  });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions