definition-registry.spec.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import chai from 'chai';
  2. import {expect} from 'chai';
  3. import {ModelDefinitionValidator} from './model/index.js';
  4. import {DefinitionRegistry} from './definition-registry.js';
  5. import {DatasourceDefinitionValidator} from '../definition/index.js';
  6. const sandbox = chai.spy.sandbox();
  7. describe('DefinitionRegistry', function () {
  8. let S;
  9. beforeEach(function () {
  10. S = new DefinitionRegistry();
  11. });
  12. afterEach(function () {
  13. sandbox.restore();
  14. });
  15. it('sets a given datasource to the state', function () {
  16. const datasource = {name: 'datasource', adapter: 'adapter'};
  17. S.addDatasource(datasource);
  18. const result = S.getDatasource('datasource');
  19. expect(result).to.be.eql(datasource);
  20. });
  21. it('throws an error if a given datasource is already defined', function () {
  22. const datasource1 = {name: 'datasource', adapter: 'adapter'};
  23. const datasource2 = {name: 'datasource', adapter: 'adapter'};
  24. S.addDatasource(datasource1);
  25. const throwable = () => S.addDatasource(datasource2);
  26. expect(throwable).to.throw(
  27. 'The datasource "datasource" is already defined.',
  28. );
  29. });
  30. it('throws an error when getting a not defined datasource', function () {
  31. const throwable = () => S.getDatasource('undefined');
  32. expect(throwable).to.throw('The datasource "undefined" is not defined.');
  33. });
  34. it('uses DatasourceDefinitionValidator to validate a given datasource', function () {
  35. const V = S.getService(DatasourceDefinitionValidator);
  36. sandbox.on(V, 'validate');
  37. const datasource = {name: 'datasource', adapter: 'adapter'};
  38. S.addDatasource(datasource);
  39. expect(V.validate).to.have.been.called.once;
  40. expect(V.validate).to.have.been.called.with.exactly(datasource);
  41. });
  42. it('sets a given model to the state', function () {
  43. const model = {name: 'model'};
  44. S.addModel(model);
  45. const result = S.getModel('model');
  46. expect(result).to.be.eql(model);
  47. });
  48. it('throws an error if a given model is already defined', function () {
  49. const model1 = {name: 'model'};
  50. const model2 = {name: 'model'};
  51. S.addModel(model1);
  52. const throwable = () => S.addModel(model2);
  53. expect(throwable).to.throw('The model "model" is already defined.');
  54. });
  55. it('throws an error when getting a not defined model', function () {
  56. const throwable = () => S.getModel('undefined');
  57. expect(throwable).to.throw('The model "undefined" is not defined.');
  58. });
  59. it('uses ModelDefinitionValidator to validate a given model', function () {
  60. const V = S.getService(ModelDefinitionValidator);
  61. sandbox.on(V, 'validate');
  62. const model = {name: 'model'};
  63. S.addModel(model);
  64. expect(V.validate).to.have.been.called.once;
  65. expect(V.validate).to.have.been.called.with.exactly(model);
  66. });
  67. });