crowi.test.js 2.0 KB

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