Skip to content

Commit

Permalink
chore: formFieldInstanceRegistry tests
Browse files Browse the repository at this point in the history
Closes #1142
  • Loading branch information
Skaiir committed Apr 12, 2024
1 parent 70058fe commit cd4a4d5
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 1 deletion.
41 changes: 40 additions & 1 deletion packages/form-js-viewer/test/spec/Form.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
});


Expand Down Expand Up @@ -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);
});


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
});


Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}));

});

});

0 comments on commit cd4a4d5

Please sign in to comment.