{"size": 2860, "ext": "js", "max_stars_repo_path": "test/server/stages.test.js", "max_stars_repo_name": "keenethics/pelorus", "max_stars_repo_stars_event_min_datetime": "2016-01-24T20:48:44.000Z", "max_stars_repo_stars_event_max_datetime": "2018-08-22T12:00:48.000Z", "max_issues_repo_path": "test/server/stages.test.js", "max_issues_repo_name": "keenethics/pelorus", "max_issues_repo_issues_event_min_datetime": "2016-06-25T20:55:12.000Z", "max_issues_repo_issues_event_max_datetime": "2016-10-06T19:07:56.000Z", "max_forks_repo_path": "test/server/stages.test.js", "max_forks_repo_name": "keenethics/pelorus", "max_forks_repo_forks_event_min_datetime": "2016-06-22T12:52:18.000Z", "max_forks_repo_forks_event_max_datetime": "2016-06-22T12:52:18.000Z"}
{"max_stars_count": 8, "max_issues_count": 5, "max_forks_count": 1, "avg_line_length": 30.752688172, "max_line_length": 95, "alphanum_fraction": 0.65}
import { resetDatabase } from 'meteor/xolvio:cleaner';
import { Random } from 'meteor/random';
import { Goals } from '/imports/api/goals/goals.js';
import { Stages } from '/imports/api/stages/stages.js';
import yaml from 'js-yaml';
import { _ } from 'meteor/underscore';
import { addStage } from '/imports/api/stages/methods.js';
import { Accounts } from 'meteor/accounts-base';
import { assert } from 'meteor/practicalmeteor:chai';

const langs = ["uk", "ru", "en" ];

describe('stages', function (done) {
  beforeEach(function () {
    resetDatabase();
  });
  describe('addStage', function() {
    it('should add new stage', function() {
      const userId = Accounts.createUser({
        username: 'username',
        email : 'useremail@mail.com',
        password : "password",
        profile  : {
          language: langs[Math.round(Random.fraction()*2 )],
        }
      });
      const user = Meteor.users.findOne({ _id: userId });
      const seeds = yaml.safeLoad(Assets.getText('intro_seeds.yml'), 'utf8').seeds;
      loadingSeeds(seeds, user);
    });
  });
}); 

function loadingSeeds(seeds, user) {
  seeds.forEach(seed => {
    const userId = user._id;
    const stage = {
      period: seed.period,
      type: seed.type,
    }
    const copyGoals = !!Math.round(Random.fraction());
    const newStageId = addStage.run.call(
        { userId: userId },
        { stage, copyGoals },
    );
    const newStage = Stages.findOne({ _id: newStageId }); 
    const goal = {
      title: seed.title,
      rank: 0,
      progress: 0,
      stageId: newStageId,
      userId: userId,
    };

    Goals.insert(goal);

    testNewStage(newStage, stage, user);

    testCopiedGoal(newStage, copyGoals);
    if (seed.children) loadingSeeds(seed.children, user);
  });
}

function testNewStage(newStage, stage, user) {
  const bounds = Stages.boundsFor(stage.period, stage.type, user.profile.language);
  
  assert.equal(newStage.period, stage.period);
  assert.equal(newStage.type, stage.type);
  assert.equal(newStage.userId, user._id);
  assert.equal(newStage.startsAt.toString(), bounds.startsAt.toString());
  assert.equal(newStage.endsAt.toString(), bounds.endsAt.toString());
};

function testCopiedGoal(newStage, copyGoals) {
  const copiedGoals = Goals.find({ stageId: newStage._id, parentId: {$exists: true} }).fetch();
  
  if (copyGoals && newStage.type !== 'years') {
    copiedGoals.forEach(goal => {
      const parentGoal = Goals.findOne(goal.parentId);
      const parentStage = newStage.parent();

      assert.equal(parentGoal.stageId, parentStage._id);

      assert.equal(goal.title, parentGoal.title);
      assert.equal(goal.rank, parentGoal.rank);
      assert.equal(goal.progress, parentGoal.progress);
      assert.equal(goal.userId, parentGoal.userId);
    });
    return false;
  };
  assert.equal(copiedGoals.length, 0);
};
