Skip to content

Instantly share code, notes, and snippets.

@mikberg
Created January 19, 2016 22:28
Show Gist options
  • Save mikberg/07b4006e22aacf31ffe6 to your computer and use it in GitHub Desktop.
Save mikberg/07b4006e22aacf31ffe6 to your computer and use it in GitHub Desktop.

Revisions

  1. mikberg created this gist Jan 19, 2016.
    57 changes: 57 additions & 0 deletions mockRelay.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    import Relay from 'real-react-relay';

    export class Mutation extends Relay.Mutation {
    _resolveProps(props) {
    this.props = props;
    }
    }

    export class MockStore {
    reset() {
    this.successResponse = undefined;
    }

    succeedWith(response) {
    this.reset();
    this.successResponse = response;
    }

    failWith(response) {
    this.reset();
    this.failureResponse = response;
    }

    update(callbacks) {
    if (this.successResponse) {
    callbacks.onSuccess(this.successResponse);
    } else if (this.failureResponse) {
    callbacks.onFailure(this.failureResponse);
    }
    this.reset();
    }

    commitUpdate(mutation, callbacks) {
    return this.update(callbacks);
    }

    applyUpdate(mutation, callbacks) {
    return this.update(callbacks);
    }
    }

    export const Store = new MockStore();
    export const Route = Relay.Route;
    export const PropTypes = Relay.PropTypes;

    export default {
    QL: Relay.QL,
    Mutation,
    Route: Relay.Route,
    PropTypes: Relay.PropTypes,
    createContainer: (component, specs) => {
    /* eslint no-param-reassign:0 */
    component.getRelaySpecs = () => specs;
    return component;
    },
    Store,
    };