mitjajez/SONCE

View on GitHub
imports/startup/client/routes.app-test.js

Summary

Maintainability
A
0 mins
Test Coverage
/* eslint-env mocha */

import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { DDP } from 'meteor/ddp-client';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { assert } from 'meteor/practicalmeteor:chai';
import { Promise } from 'meteor/promise';
import { $ } from 'meteor/jquery';

import { generateData } from './../../api/generate-data.app-tests.js';
import { Circuits } from '../../api/circuits/circuits.js';
import { Elements } from '../../api/elements/elements.js';


// Utility -- returns a promise which resolves when all subscriptions are done
const waitForSubscriptions = () => new Promise(resolve => {
  const poll = Meteor.setInterval(() => {
    if (DDP._allSubscriptionsReady()) {
      clearInterval(poll);
      resolve();
    }
  }, 200);
});

// Tracker.afterFlush runs code when all consequent of a tracker based change
//   (such as a route change) have occured. This makes it a promise.
const afterFlushPromise = Promise.denodeify(Tracker.afterFlush);

if (Meteor.isClient) {
  describe('data available when routed', () => {
    beforeEach(done => {
      // First, ensure the data that we expect is loaded on the server
      generateData()
        // Then, route the app to the homepage
        .then(() => FlowRouter.go('/'))
        // Nodeify is a API to return any error from the promise
        // to the node-style done callback in the style it expects
        .nodeify(done);
    });

    describe('when logged out', () => {
      it('has all public circuits at homepage', () => {
        assert.equal(Circuits.find().count(), 3);
      });

      it('renders the correct circuit when routed to', done => {
        const circuit = Circuits.findOne();
        FlowRouter.go('Circuits.show', { _id: circuit._id });

        afterFlushPromise()
          .then(() => {
            assert.equal($('.title-wrapper').html(), circuit.name);
          })
          .then(() => waitForSubscriptions())
          .then(() => {
            assert.equal(Elements.find({ cid: circuit._id }).count(), 3);
          })
          .nodeify(done);
      });
    });
  });
}