/* eslint-disable prefer-arrow-callback, func-names */
/* eslint-env mocha */
import chai from 'chai';
import gql from 'graphql-tag';
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloClient } from 'apollo-client';
import { loginWithUserId } from './helpers/login';
import { callPromise } from './helpers/callPromise';
import { MeteorLink } from '../../lib/client/apollo-link-meteor';

describe('MeteorLink', function () {
  beforeEach(function () {
    this.link = new MeteorLink();

    this.client = new ApolloClient({
      link: this.link,
      cache: new InMemoryCache(),
    });

    this.client.cache.reset();
  });

  afterEach(function () {
    this.link.subscriptionLink.ddpSubscription.unsubscribe();
  });

  describe('#query', function () {
    it('should return data from the server', async function () {
      const operation = {
        query: gql`query { foo }`,
      };

      const { data, loading } = await this.client.query(operation);

      chai.expect(data).to.deep.equal({ foo: 'bar' });
      chai.expect(loading).to.equal(false);
    });

    describe('when authenticated', function () {
      before(async function () {
        const userId = await callPromise('createTestUser');
        chai.expect(userId).to.be.a('string');
        this.userId = userId;
        await loginWithUserId(userId);
      });

      after(function (done) {
        Meteor.logout((err) => { err ? done(err) : done(); });
      });

      it('returns the userId', async function () {
        const operation = {
          query: gql`query { userId }`,
        };

        const { data } = await this.client.query(operation);

        chai.expect(data.userId).to.be.a('string');
        chai.expect(data.userId).to.equal(this.userId);
      });

      it('returns the meteorUserId', async function () {
        const operation = {
          query: gql`query { meteorUserId }`,
        };

        const { data } = await this.client.query(operation);

        chai.expect(data.meteorUserId).to.be.a('string');
        chai.expect(data.meteorUserId).to.equal(this.userId);
      });
    });
  });
});


@@ -734,16 +734,326 @@
 %0A  %7D);%0A%0A
+  describe('#mutate', function () %7B%0A    it('returns data from the server', async function () %7B%0A      const operation = %7B%0A        mutation: gql%60mutation %7B foo %7D%60,%0A      %7D;%0A%0A      const %7B data %7D = await this.client.mutate(operation);%0A%0A      chai.expect(data).to.deep.equal(%7B foo: 'fooMutated' %7D);%0A    %7D);%0A  %7D);%0A%0A
   descri

test if mutations work with HTTP