use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testSubFormInput.
@Test
public void testSubFormInput() {
FormInputViewModelBuilder builder = builderFor(setup.getCatalog().getIncidentForm());
// Start with empty input
FormInputModel inputModel = new FormInputModel(new RecordRef(IncidentForm.FORM_ID, ResourceId.generateId()));
// Should see one (empty) sub form record
FormInputViewModel viewModel = builder.build(inputModel);
SubFormViewModel referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(1));
// We can update this sub record
FormInputViewModel subRecord = referralSubForm.getSubRecords().get(0);
inputModel = inputModel.update(subRecord.getRecordRef(), ReferralSubForm.ORGANIZATION_FIELD_ID, new FieldInput(TextValue.valueOf("CRS")));
viewModel = builder.build(inputModel);
referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(1));
// Now add a second record
inputModel = inputModel.addSubRecord(new RecordRef(ReferralSubForm.FORM_ID, ResourceId.generateId()));
viewModel = builder.build(inputModel);
referralSubForm = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(referralSubForm.getSubRecords(), hasSize(2));
// Verify that the transaction is built is correctly
RecordTransaction tx = viewModel.buildTransaction();
RecordUpdate[] changes = tx.getChangeArray();
assertThat(changes.length, equalTo(3));
RecordUpdate parentChange = changes[0];
RecordUpdate subFormChange = changes[1];
assertThat(parentChange.getRecordRef(), equalTo(inputModel.getRecordRef()));
assertThat(subFormChange.getParentRecordId(), equalTo(parentChange.getRecordId().asString()));
}
use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.
the class FormInputViewModelTest method editRecordWithSubRecords.
@Test
public void editRecordWithSubRecords() {
IncidentForm incidentForm = setup.getCatalog().getIncidentForm();
RecordRef rootRecordRef = incidentForm.getRecordRef(0);
FormStructure structure = fetchStructure(rootRecordRef);
FormInputViewModelBuilder builder = builderFor(structure);
FormInputModel inputModel = new FormInputModel(rootRecordRef);
FormInputViewModel viewModel = builder.build(inputModel, structure.getExistingRecord());
SubFormViewModel subFormField = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(subFormField.getSubRecords(), hasSize(4));
// Try deleting the first one...
RecordRef deletedRef = subFormField.getSubRecords().get(0).getRecordRef();
inputModel = inputModel.deleteSubRecord(deletedRef);
viewModel = builder.build(inputModel, structure.getExistingRecord());
subFormField = viewModel.getSubForm(IncidentForm.REFERRAL_FIELD_ID);
assertThat(subFormField.getSubRecords(), hasSize(3));
// Should show up in transaction
RecordTransaction tx = viewModel.buildTransaction();
assertThat(tx.getChanges(), hasItem(hasProperty("deleted", equalTo(true))));
}
use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.
the class FormInputViewModelTest method hiddenSubForm.
/**
* Test the ViewModel for when the user does not have access to a referenced sub form.
*/
@Test
public void hiddenSubForm() {
FormClass parentForm = new FormClass(ResourceId.valueOf("PARENT_FORM"));
parentForm.addField(ResourceId.valueOf("F1")).setLabel("What is your name?").setType(TextType.SIMPLE);
parentForm.addField(ResourceId.valueOf("F2")).setLabel("What are your secrets?").setType(new SubFormReferenceType(ResourceId.valueOf("SECRET_FORM")));
FormTreeBuilder treeBuilder = new FormTreeBuilder(new FormMetadataProvider() {
@Override
public FormMetadata getFormMetadata(ResourceId formId) {
if (formId.equals(parentForm.getId())) {
return FormMetadata.of(1, parentForm, FormPermissions.owner());
} else {
return FormMetadata.forbidden(formId);
}
}
});
FormTree formTree = treeBuilder.queryTree(parentForm.getId());
FormStore formStore = EasyMock.createMock(FormStore.class);
EasyMock.replay(formStore);
FormInputViewModelBuilder viewModelBuilder = new FormInputViewModelBuilder(formStore, formTree, new TestingActivePeriodMemory());
FormInputViewModel viewModel = viewModelBuilder.build(new FormInputModel(new RecordRef(parentForm.getId(), ResourceId.valueOf("R1"))));
}
use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.
the class FormInputViewModelTest method monthlySubForms.
@Test
public void monthlySubForms() {
ClinicForm clinicForm = setup.getCatalog().getClinicForm();
ResourceId consultCountFieldId = clinicForm.getSubForm().getConsultsField().getId();
FormInputViewModelBuilder builder = builderFor(clinicForm);
ResourceId parentRecordId = ResourceId.generateId();
FormInputModel inputModel = new FormInputModel(new RecordRef(clinicForm.getFormId(), parentRecordId));
FormInputViewModel viewModel = builder.build(inputModel);
// Get the sub form view model.
// The active ref should be set to 2017-10 by default.
ResourceId subFormFieldId = clinicForm.getSubFormField().getId();
SubFormViewModel subForm = viewModel.getSubForm(subFormFieldId);
assertThat(subForm, notNullValue());
RecordRef octoberId = new RecordRef(clinicForm.getSubForm().getFormId(), ResourceId.generatedPeriodSubmissionId(parentRecordId, "2017-10"));
RecordRef novemberId = new RecordRef(clinicForm.getSubForm().getFormId(), ResourceId.generatedPeriodSubmissionId(parentRecordId, "2017-11"));
assertThat(subForm.getActiveRecordRef(), equalTo(octoberId));
// Update a field in the active form
inputModel = inputModel.updateSubForm(subForm.update(consultCountFieldId, new FieldInput(new Quantity(33))));
viewModel = builder.build(inputModel);
FormInputViewModel subFormViewModel = viewModel.getSubForm(subFormFieldId).getActiveSubViewModel();
assertThat(subFormViewModel.getField(consultCountFieldId), equalTo(new Quantity(33)));
assertThat(subFormViewModel.isValid(), equalTo(true));
// Now change the active record to another month
inputModel = inputModel.updateActiveSubRecord(subFormFieldId, novemberId);
viewModel = builder.build(inputModel);
subForm = viewModel.getSubForm(subFormFieldId);
// At this point, the subform is not valid because required fields are not filled in,
// but it is also not dirty because we have not changed anything
assertThat(subForm.getActiveSubViewModel().isValid(), equalTo(false));
assertThat(subForm.getActiveSubViewModel().isDirty(), equalTo(false));
assertThat(subForm.getActiveSubViewModel().isPlaceholder(), equalTo(true));
// Now fill in the required field
inputModel = inputModel.updateSubForm(subForm.update(consultCountFieldId, new FieldInput(new Quantity(44))));
viewModel = builder.build(inputModel);
assertThat(viewModel.getSubForm(subFormFieldId).getActiveRecordRef(), equalTo(novemberId));
assertThat(viewModel.getSubForm(subFormFieldId).isValid(), equalTo(true));
}
use of org.activityinfo.ui.client.input.model.FormInputModel in project activityinfo by bedatadriven.
the class FormInputViewModelTest method testPersistence.
@Test
public void testPersistence() {
Survey survey = setup.getCatalog().getSurvey();
FormInputViewModelBuilder builder = builderFor(survey);
// Start with no input
FormInputModel inputModel = new FormInputModel(new RecordRef(survey.getFormId(), ResourceId.generateId())).update(survey.getGenderFieldId(), new FieldInput(new EnumValue(survey.getMaleId()))).update(survey.getNameFieldId(), new FieldInput(TextValue.valueOf("BOB"))).update(survey.getDobFieldId(), new FieldInput(new LocalDate(1982, 1, 16))).update(survey.getAgeFieldId(), new FieldInput(new Quantity(35)));
// Verify that it's valid
FormInputViewModel viewModel = builder.build(inputModel);
assertThat(viewModel.isValid(), equalTo(true));
// Now build the update transaction and save!
Promise<Void> completed = setup.getFormStore().updateRecords(viewModel.buildTransaction());
assertThat(completed.getState(), equalTo(Promise.State.FULFILLED));
}
Aggregations