test("publish user", ()=>{
      const org1 = Factory.createOrg();
      const user = Factory.createUser();
      const org2 = Factory.createOrg();

      TH.loginAs(user);

      spy(User, 'observeId');
      spy(Org, 'onChange');

      // Subscribe
      const sub = TH.mockSubscribe(v, 's123', 'Self');

      assert.msg("should be logged in user")
        .same(v.conn.userId, user._id);


      // Test initial data
      assert.calledWith(v.conn.added, 'User', user._id, user.attributes);
      assert.calledWith(v.conn.added, 'Org', org1._id, org1.attributes, undefined);
      assert.calledWith(v.conn.added, 'Org', org2._id, org2.attributes, {email: true});


      // Test changes
      user.name = 'new name';
      user.$$save();

      assert.calledWith(v.conn.changed, 'User', user._id, {name: 'new name'});

      org1.$remove();
      assert.calledWith(v.conn.removed, 'Org', org1._id);


      // *** test stopping ***
      assert.calledWith(User.observeId, user._id);
      const uStop = spy(User.observeId.firstCall.returnValue, 'stop');

      assert.calledOnce(Org.onChange);
      const oStop = spy(Org.onChange.firstCall.returnValue, 'stop');

      sub.stop();

      assert.called(uStop);
      assert.called(oStop);
    });
Example #2
0
        test("authorized", ()=>{
          const otherOrg = Factory.createOrg();
          const user = Factory.createUser();
          TH.loginAs(user);

          assert.accessDenied(function () {
            rpc("Result.setBoulderScore", result._id, 1, 1, 3, 7);
          });
        });
Example #3
0
    test("Route.root.onBaseEntry", ()=>{
      assert.same(Route.root.routeVar, 'orgSN');
      assert.same(Route.root.async, true);
      stub (App, 'subscribe');

      const selfSub = App.subscribe.withArgs('Self').returns({stop: stub()});
      const orgSub = App.subscribe.withArgs('Org');

      App.start();
      selfSub.yield();

      /** test diff orgSN */
      Route.root.onBaseEntry('x', {orgSN: 'org2'}, v.callback = stub());
      refute.called(v.callback);
      assert.calledWith(App.subscribe, 'Org', 'org2');

      Factory.createOrg({shortName: 'org2'});
      orgSub.yield();
      assert.called(v.callback);
    });
isClient && define(function (require, exports, module) {
  const Dom         = require('koru/dom');
  const Route       = require('koru/ui/route');
  const UserAccount = require('koru/user-account');
  const Org         = require('models/org');
  const User        = require('models/user');
  const Factory     = require('test/factory');
  const App         = require('./app');
  const TH          = require('./test-helper');

  const SystemSetup = require('./system-setup');
  var test, v;

  TH.testCase(module, {
    setUp() {
      test = this;
      v = {};
      App.orgId = Factory.createOrg()._id;
      v.su = Factory.createUser('su');
      TH.loginAs(v.su);
      App.setAccess();
    },

    tearDown() {
      TH.tearDown();
      v = null;
    },

    "test onEntry onExit"() {
      Route.gotoPage(SystemSetup);

      assert.dom('#SystemSetup', function () {
        assert.dom('.menu', function () {
          assert.dom('button.link', {count: 2});
        });
      });


      Dom.SystemSetup.onBaseExit();

      refute.dom('#SystemSetup');
    },

    "test renders orgs list"() {
      var orgs = Factory.createList(2, 'createOrg');

      Route.gotoPage(Dom.SystemSetup);

      assert.dom('table.orgs', function () {
        assert.dom('td', orgs[0].name);
      });
    },

    "test renders users list"() {
      v.org = Factory.createOrg({name: 'org1'});

      setOrg();

      var users = Factory.createList(2, 'createUser');

      assert.dom('h1', 'org1 Users');
      assert.dom('table.users', function () {
        assert.dom('td', users[0].name);
      });
    },

    "test addOrg"() {
      Route.gotoPage(Dom.SystemSetup);

      assert.dom('#SystemSetup', function () {
        TH.click('[name=addOrg]');
      });
      assert.dom('#OrgForm', function () {
        assert.dom('label', 'Name', function () {
          TH.input('[name=name]', 'Foo Bar');
        });
        TH.input('[name=shortName]', 'FB');
        TH.input('[name=email]', 'FB@foo.com');
        TH.click('[type=submit]');
      });

      refute.dom('#OrgForm');
      assert.dom('#SystemSetup');

      var org = Org.query.where('shortName', 'FB').fetchOne();

      assert(org);

      assert.attributesEqual(org, {name: 'Foo Bar', shortName: 'FB', email: 'fb@foo.com'}, ['_id']);
    },
    "edit org": {
      setUp() {
        v.org = Factory.createOrg();
        v.org2 = Factory.createOrg();

        Route.gotoPage(Dom.SystemSetup.Index);

        TH.click('td', v.org.name);
      },

      "test change name"() {
        assert.dom('#OrgForm', function () {
          TH.input('[name=name]', {value: v.org.name}, 'new name');
          TH.click('[type=submit]');
        });

        assert.dom('#SystemSetup', function () {
          assert.dom('td', 'new name');
        });
      },

      "test delete"() {
        assert.dom('#OrgForm', function () {
          TH.click('[name=delete]');
        });

        assert.dom('.Dialog.Confirm', function () {
          assert.dom('h1', 'Delete ' + v.org.name + '?');
          TH.click('[name=cancel]');
        });

        refute.dom('.Dialog');

        assert(Org.exists(v.org._id));

        TH.click('#OrgForm [name=delete]');

        assert.dom('.Dialog', function () {
          TH.click('[name=okay]', 'Delete');
        });

        refute.dom('#OrgForm');

        refute(Org.exists(v.org._id));
      },
    },

    "test addUser"() {
      v.org = Factory.createOrg();
      setOrg();

      assert.dom('#SystemSetup', function () {
        TH.click('[name=addUser]');
      });
      assert.dom('#UserForm', function () {
        assert.dom('label', 'Name', function () {
          TH.input('[name=name]', 'Foo Bar');
        });
        TH.input('[name=initials]', 'FB');
        TH.change('[name=role]', 'a');
        TH.input('[name=email]', 'FB@foo.com');
        TH.click('[type=submit]');
      });


      var user = User.query.where('name', 'Foo Bar').fetchOne();
      assert(user);
      assert.attributesEqual(user, {
        org_id: v.org._id, name: 'Foo Bar', initials: 'FB', email: 'fb@foo.com',
        role: 'a',
      }, ['_id']);

      refute.dom('#UserForm');
      assert.dom('#SystemSetup');
    },

    "edit user": {
      setUp() {
        v.org = Factory.createOrg();
        v.user = Factory.createUser();
        v.user2 = Factory.createUser();

        setOrg();

        TH.click('td', v.user.name);
      },

      "test change name"() {
        assert.dom('#UserForm', function () {
          TH.input('[name=name]', {value: v.user.name}, 'new name');
          TH.click('[type=submit]');
        });

        assert.dom('#SystemSetup', function () {
          assert.dom('td', 'new name');
        });
      },

      "test delete"() {
        assert.dom('#UserForm', function () {
          TH.click('[name=delete]');
        });

        assert.dom('.Dialog.Confirm', function () {
          assert.dom('h1', 'Delete ' + v.user.name + '?');
          TH.click('[name=cancel]');
        });

        refute.dom('.Dialog');

        assert(User.exists(v.user._id));

        TH.click('#UserForm [name=delete]');

        assert.dom('.Dialog', function () {
          TH.click('[name=okay]', 'Delete');
        });

        refute.dom('#UserForm');

        assert.same(v.user.$reload().attributes.role, undefined);
      },
    },
  });

  function setOrg() {
    App.orgId = v.org._id;
    Dom.addClass(document.body, 'inOrg');
    App._setOrgShortName(v.org.shortName);

    Route.replacePage(SystemSetup, {orgSN: v.org.shortName});
  }
});
Example #5
0
define((require, exports, module)=>{
  const koru            = require('koru');
  const Dom             = require('koru/dom');
  const localStorage    = require('koru/local-storage');
  require('koru/ui/helpers');
  const Route           = require('koru/ui/route');
  const KoruUITH        = require('koru/ui/test-helper');
  const util            = require('koru/util');
  const BaseTH          = require('test-helper');
  const Factory         = require('test/factory');
  const App             = require('ui/app');

  koru.onunload(module, 'reload');

  const TH = {
    __proto__: BaseTH,

    setOrg(org) {
      org = org || Factory.createOrg();
      App.orgId = org._id;
      localStorage.setItem('orgSN', org.shortName);
      Dom.addClass(document.body, 'inOrg');
    },

    tearDown(v) {
      TH.clearDB();
      TH.domTearDown();
      util.thread.userId = null;
    },

    addStyles(styles) {
      var style = Dom.h({style: '', class: "testStyle"});

      style.innerHTML = styles;
      document.head.appendChild(style);
    },

    pointer(node, eventName, args) {
      if (typeof node === 'string') {
        assert.elideFromStack.dom(node, elm =>{TH.pointer(elm, eventName, args)});
      } else {
        assert.elideFromStack(node,'node not found');
        if (typeof eventName === 'object') {
          args = eventName;
          eventName = 'pointermove';
        } else {
          eventName = eventName || 'pointermove';
          args = args || {};
        }
        var bbox = node.getBoundingClientRect();

        args.clientX = bbox.left + bbox.width/2;
        args.clientY = bbox.top + bbox.height/2;

        var event =  TH.buildEvent(eventName, args);

        if (document.createEvent) {
          TH.dispatchEvent(node, event);
        } else {
          node.fireEvent("on" + event.__name, event);
        }
        return event;
      }
    },

    stubRAF(v) {
      let func = null;
      TH.intercept(window, 'requestAnimationFrame', arg =>{
        func = arg;
        return 123;
      });
      v.nextRaf = ()=>{
        if (func !== null) {
          func();
          func = null;
        }
      };
    },

    confirmRemove(func) {
      const dialog = document.body.querySelector('.Dialog #ConfirmRemove');
      assert.elideFromStack.msg('should display Confirm Dialog')(dialog);
      TH.confirm(func);
    },

    confirm(func) {
      const dialog = document.body.querySelector('.Dialog');
      assert.elideFromStack.msg('should display Confirm Dialog')(dialog);
      assert.elideFromStack.dom(dialog, elm=>{
        const data = Dom.current.data(elm);
        assert.isFunction(data.onConfirm);
        func && func(elm);
        TH.click('[name=okay]');
      });
    },
  };

  util.mergeOwnDescriptors(TH, KoruUITH);

  let onAnimationEnd;

  TH.Core.onStart(()=>{
    onAnimationEnd = Dom.Ctx.prototype.onAnimationEnd;
    Dom.Ctx.prototype.onAnimationEnd = function (func, repeat) {
      func(this, this.element());
    };
  });

  TH.Core.onEnd(()=>{Dom.Ctx.prototype.onAnimationEnd = onAnimationEnd});

  module.exports = TH;
});