Skip to content

Commit

Permalink
[MNT-22051] - Fix form field mapping when value is zero (#6517)
Browse files Browse the repository at this point in the history
* [MNT-22051] - Fix form field mapping when value is zero

* PR changes

* add validation for null
  • Loading branch information
SilviuCPopa authored Jan 19, 2021
1 parent 885792b commit 9870510
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 22 deletions.
8 changes: 8 additions & 0 deletions lib/core/form/components/widgets/core/form.model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ describe('FormModel', () => {
expect(form.readOnly).toBeTruthy();
});

it('should set form values when variable value is 0', () => {
const variables = {
pfx_property_one: 0
};
const form = new FormModel(fakeMetadataForm, variables, true);
expect(form.getFormFields()[0].fields[1][0].value).toEqual(0);
});

it('should check tabs', () => {
const form = new FormModel();

Expand Down
6 changes: 5 additions & 1 deletion lib/core/form/components/widgets/core/form.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,17 @@ export class FormModel {
for (const field of this.getFormFields()) {
const variableId = `variables.${field.name}`;

if (formValues[variableId] || formValues[field.id]) {
if (this.isDefined(formValues[variableId]) || this.isDefined(formValues[field.id])) {
field.json.value = formValues[variableId] || formValues[field.id];
field.value = field.parseValue(field.json);
}
}
}

private isDefined(value: string): boolean {
return value !== undefined && value !== null;
}

/**
* Returns a form variable that matches the identifier.
* @param identifier The `name` or `id` value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ export class TaskVariableCloud {
constructor(obj) {
this.id = obj.name || null;
this.name = obj.name || null;
this.value = obj.value || null;
this.value = this.hasValue(obj) ? obj.value : null;
this.type = obj.type || null;
}

hasValue(obj: TaskVariableCloud): boolean {
return typeof obj.value !== undefined && obj.value !== null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,51 @@ describe('Form Cloud service', () => {
});
});

it('should fetch result if the variable value is 0', (done) => {
oauth2Auth.callCustomApi.and.returnValue(Promise.resolve({
'list': {
'entries': [
{
'entry': {
'serviceName': 'fake-rb',
'serviceFullName': 'fake-rb',
'serviceVersion': '',
'appName': 'fake',
'appVersion': '',
'serviceType': null,
'id': 25,
'type': 'string',
'name': 'fakeProperty',
'createTime': 1556112661342,
'lastUpdatedTime': 1556112661342,
'executionId': null,
'value': 0,
'markedAsDeleted': false,
'processInstanceId': '18e16bc7-6694-11e9-9c1b-0a586460028a',
'taskId': '18e192da-6694-11e9-9c1b-0a586460028a',
'taskVariable': true
}
}
],
'pagination': {
'skipCount': 0,
'maxItems': 100,
'count': 1,
'hasMoreItems': false,
'totalItems': 1
}
}
}));

service.getTaskVariables(appName, taskId).subscribe((result) => {
expect(result).toBeDefined();
expect(result.length).toBe(1);
expect(result[0].name).toBe('fakeProperty');
expect(result[0].value).toBe(0);
done();
});
});

it('should fetch task form flattened', (done) => {
spyOn(service, 'getTask').and.returnValue(of(responseBody.entry));
spyOn(service, 'getForm').and.returnValue(of({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,28 @@ describe('StartProcessCloudComponent', () => {

beforeEach(() => {
component.name = 'My new process with form';
component.values = [{
'id': '1',
'type': 'string',
'name': 'firstName',
'value': 'FakeName',
get 'hasValue'() {
return this['value'];
},
set 'hasValue'(value) {
this['value'] = value;
}
}, {
'id': '1', 'type': 'string',
'name': 'lastName',
'value': 'FakeLastName',
get 'hasValue'() {
return this['value'];
},
set 'hasValue'(value) {
this['value'] = value;
}
}];
});

it('should be able to start a process with a valid form', fakeAsync(() => {
Expand Down Expand Up @@ -264,11 +286,6 @@ describe('StartProcessCloudComponent', () => {
it('should be able to start a process with a prefilled valid form', fakeAsync(() => {
component.processDefinitionName = 'processwithform';
getDefinitionsSpy.and.returnValue(of(fakeSingleProcessDefinition(component.processDefinitionName)));
component.values = [{'id': '1', 'type': 'string', 'name': 'firstName', 'value': 'FakeName' }, {
'id': '1', 'type': 'string',
'name': 'lastName',
'value': 'FakeLastName'
}];
fixture.detectChanges();
formDefinitionSpy = spyOn(formCloudService, 'getForm').and.returnValue(of(fakeStartForm));

Expand Down Expand Up @@ -299,11 +316,6 @@ describe('StartProcessCloudComponent', () => {
it('should NOT be able to start a process with a prefilled NOT valid form', fakeAsync(() => {
component.processDefinitionName = 'processwithform';
getDefinitionsSpy.and.returnValue(of(fakeSingleProcessDefinition(component.processDefinitionName)));
component.values = [{ 'id': '1', 'type': 'string', 'name': 'firstName', 'value': 'FakeName' }, {
'id': '1', 'type': 'string',
'name': 'lastName',
'value': 'FakeLastName'
}];
fixture.detectChanges();
formDefinitionSpy = spyOn(formCloudService, 'getForm').and.returnValue(of(fakeStartFormNotValid));

Expand Down Expand Up @@ -333,11 +345,6 @@ describe('StartProcessCloudComponent', () => {
}));

it('should create a process instance if the selection is valid', fakeAsync(() => {
component.values = [{ 'id': '1', 'type': 'string', 'name': 'firstName', 'value': 'FakeName' }, {
'id': '1', 'type': 'string',
'name': 'lastName',
'value': 'FakeLastName'
}];
component.name = 'testFormWithProcess';
component.processDefinitionName = 'processwithoutform2';
getDefinitionsSpy.and.returnValue(of(fakeSingleProcessDefinition(component.processDefinitionName)));
Expand Down Expand Up @@ -365,11 +372,6 @@ describe('StartProcessCloudComponent', () => {
}));

it('should have start button enabled when default values are set', fakeAsync(() => {
component.values = [{ 'id': '1', 'type': 'string', 'name': 'firstName', 'value': 'FakeName' }, {
'id': '1', 'type': 'string',
'name': 'lastName',
'value': 'FakeLastName'
}];
component.name = 'testFormWithProcess';
component.processDefinitionName = 'processwithoutform2';
getDefinitionsSpy.and.returnValue(of(fakeSingleProcessDefinition(component.processDefinitionName)));
Expand Down

0 comments on commit 9870510

Please sign in to comment.