crowi.test.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const chai = require('chai')
  2. , expect = chai.expect
  3. , sinonChai = require('sinon-chai')
  4. , helpers = require('@commons/util/helpers')
  5. ;
  6. chai.use(sinonChai);
  7. describe('Test for Crowi application context', function() {
  8. const Crowi = require('@server/crowi')
  9. , mongoose = require('mongoose')
  10. ;
  11. describe('construction', function() {
  12. it('initialize crowi context', function() {
  13. const crowi = new Crowi(helpers.root());
  14. expect(crowi).to.be.instanceof(Crowi);
  15. expect(crowi.version).to.equal(require('../../../package.json').version);
  16. expect(crowi.env).to.be.an('Object');
  17. });
  18. it('config getter, setter', function() {
  19. const crowi = new Crowi(helpers.root());
  20. expect(crowi.getConfig()).to.deep.equals({});
  21. crowi.setConfig({test: 1});
  22. expect(crowi.getConfig()).to.deep.equals({test: 1});
  23. });
  24. it('model getter, setter', function() {
  25. const crowi = new Crowi(helpers.root());
  26. // set
  27. crowi.model('hoge', { fuga: 1 });
  28. expect(crowi.model('hoge')).to.deep.equals({ fuga: 1 });
  29. });
  30. });
  31. describe('.setupDatabase', function() {
  32. before(function() {
  33. mongoose.disconnect(); // avoid error of Trying to open unclosed connection
  34. });
  35. it('setup completed', function(done) {
  36. const crowi = new Crowi(helpers.root());
  37. // set
  38. const p = crowi.setupDatabase();
  39. expect(p).to.instanceof(Promise);
  40. p.then(function() {
  41. expect(mongoose.connection.readyState).to.equals(1);
  42. done();
  43. }).catch(function(err) {
  44. //console.log('readyState', mongoose.connection.readyState);
  45. if (mongoose.connection.readyState === 2 || mongoose.connection.readyState === 1) { // alreaady connected
  46. // throught
  47. } else {
  48. expect(mongoose.connection.readyState).to.equals(0);
  49. }
  50. done();
  51. });
  52. });
  53. });
  54. });