diff --git a/packages/form-js-viewer/test/spec/Form.spec.js b/packages/form-js-viewer/test/spec/Form.spec.js index 30e3c3d6b..d7268df23 100644 --- a/packages/form-js-viewer/test/spec/Form.spec.js +++ b/packages/form-js-viewer/test/spec/Form.spec.js @@ -146,7 +146,7 @@ describe('Form', function() { }); // then - expect(form.get('formFieldRegistry').getAll()).to.have.length(17); + expect(form.get('formFieldRegistry').getAll()).to.have.length(countComponents(groupsSchema)); }); @@ -254,6 +254,7 @@ describe('Form', function() { // then expect(form.get('formFieldRegistry').getAll()).to.have.length(1); + expect(form.get('formFieldInstanceRegistry').getAll()).to.have.length(1); }); @@ -329,6 +330,43 @@ describe('Form', function() { }); + it('should register form field instance', async function() { + + // given + const data = { + creditor: 'John Doe Company', + amount: 456, + invoiceNumber: 'C-123', + approved: true, + approvedBy: 'John Doe', + mailto: [ 'regional-manager', 'approver' ], + product: 'camunda-cloud', + tags: [ 'tag1','tag2', 'tag3' ], + language: 'english', + documents: [ + { + title: 'invoice.pdf', + author: 'John Doe' + }, + { + title: 'products.pdf' + } + ] + }; + + // when + await bootstrapForm({ + container, + data, + schema + }); + + // then + const repeatedDynamicListElements = 2; + expect(form.get('formFieldInstanceRegistry').getAll()).to.have.length(countComponents(schema) + repeatedDynamicListElements); + }); + + it('should fail instantiation with import error', async function() { // given @@ -765,6 +803,7 @@ describe('Form', function() { expect(formClearSpy).to.have.been.calledOnce; expect(form.get('formFieldRegistry').getAll()).to.be.empty; + expect(form.get('formFieldInstanceRegistry').getAll()).to.be.empty; }); diff --git a/packages/form-js-viewer/test/spec/core/FormFieldInstanceRegistry.spec.js b/packages/form-js-viewer/test/spec/core/FormFieldInstanceRegistry.spec.js new file mode 100644 index 000000000..cbcd54fb0 --- /dev/null +++ b/packages/form-js-viewer/test/spec/core/FormFieldInstanceRegistry.spec.js @@ -0,0 +1,252 @@ +import { + bootstrapForm, + getForm, + inject +} from 'test/TestHelper'; + + +describe('FormFieldInstanceRegistry', function() { + + beforeEach(bootstrapForm()); + + afterEach(function() { + getForm().destroy(); + }); + + + describe('#add', function() { + + it('should add form field', inject(function(formFieldRegistry, formFieldInstanceRegistry) { + + // given + const formField = { + id: 'foo' + }; + + // when + formFieldRegistry.add(formField); + const key = formFieldInstanceRegistry.add({ + id: formField.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: {} + }); + + // then + expect(formFieldInstanceRegistry.getAll()).to.have.length(1); + expect(key).to.equal('foo'); + })); + + + it('should use indexes for instance ID', inject(function(formFieldRegistry, formFieldInstanceRegistry) { + + // given + const formField = { + id: 'foo' + }; + + // when + formFieldRegistry.add(formField); + const key = formFieldInstanceRegistry.add({ + id: formField.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: { + bar: 2 + } + }); + + // then + expect(formFieldInstanceRegistry.getAll()).to.have.length(1); + expect(key).to.equal('foo_2'); + + })); + + + it('should throw error if form field with ID already exists', inject(function(formFieldRegistry, formFieldInstanceRegistry) { + + // given + const formField = { + id: 'foo' + }; + + formFieldRegistry.add(formField); + formFieldInstanceRegistry.add({ + id: formField.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: {} + }); + + // when + // then + expect(() => formFieldInstanceRegistry.add({ + id: formField.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: {} + })).to.throw('this form field instance is already registered'); + + })); + + + it('should not throw error when two form fields with same ID with different indexes are added', inject(function(formFieldRegistry, formFieldInstanceRegistry) { + + // given + const formField = { + id: 'foo' + }; + + formFieldRegistry.add(formField); + formFieldInstanceRegistry.add({ + id: formField.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: { bar: 1 } + }); + + // when + // then + expect(() => formFieldInstanceRegistry.add({ + id: formField.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: { bar: 2 } + })).not.to.throw(); + + })); + + }); + + + describe('#remove', function() { + + let formField; + let formFieldInstanceKey; + + beforeEach(inject(function(formFieldRegistry, formFieldInstanceRegistry) { + formField = { + id: 'foo' + }; + + formFieldRegistry.add(formField); + formFieldInstanceKey = formFieldInstanceRegistry.add({ + id: formField.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: {} + }); + })); + + + it('should remove form field', inject(function(formFieldInstanceRegistry) { + + // when + formFieldInstanceRegistry.remove(formFieldInstanceKey); + + // then + expect(formFieldInstanceRegistry.getAll()).to.have.length(0); + })); + + }); + + + describe('#getAll', function() { + + let formField1, + formField2; + + beforeEach(inject(function(formFieldRegistry, formFieldInstanceRegistry) { + formField1 = { + id: 'foo' + }; + + formFieldRegistry.add(formField1); + formFieldInstanceRegistry.add({ + id: formField1.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: {} + }); + + formField2 = { + id: 'bar' + }; + + formFieldRegistry.add(formField2); + formFieldInstanceRegistry.add({ + id: formField2.id, + expressionContextInfo: {}, + valuePath: [ 'bar' ], + indexes: {} + }); + })); + + + it('should get all form fields', inject(function(formFieldInstanceRegistry) { + + // when + const formFieldInstances = formFieldInstanceRegistry.getAll(); + + // then + expect(formFieldInstances).to.have.length(2); + expect(formFieldInstances.map(({ id }) => id)).to.eql([ formField1.id, formField2.id ]); + })); + + }); + + + describe('#clear', function() { + + let formField1, + formField2; + + beforeEach(inject(function(formFieldRegistry, formFieldInstanceRegistry) { + formField1 = { + id: 'foo' + }; + + formFieldRegistry.add(formField1); + formFieldInstanceRegistry.add({ + id: formField1.id, + expressionContextInfo: {}, + valuePath: [ 'foo' ], + indexes: {} + }); + + formField2 = { + id: 'bar' + }; + + formFieldRegistry.add(formField2); + formFieldInstanceRegistry.add({ + id: formField2.id, + expressionContextInfo: {}, + valuePath: [ 'bar' ], + indexes: {} + }); + })); + + + it('should clear', inject(function(formFieldInstanceRegistry) { + + // when + formFieldInstanceRegistry.clear(); + + // then + expect(formFieldInstanceRegistry.getAll()).to.have.length(0); + })); + + + it('should clear on form.clear', inject(function(eventBus, formFieldInstanceRegistry) { + + // when + eventBus.fire('form.clear'); + + // then + expect(formFieldInstanceRegistry.getAll()).to.have.length(0); + })); + + }); + +}); \ No newline at end of file