Skip to content

Commit

Permalink
fix: total binding issue by binding initialization (#53)
Browse files Browse the repository at this point in the history
* fix(FHIRContextBinding): Fixes an issue when elements are bound to the '%total%' property

* test: add OPA5 tests for total binding on tile

* fix: enablement of refresh of total value

* docs: updating the lint
  • Loading branch information
flovogt authored Aug 26, 2019
1 parent 7ce41c1 commit 75a41f3
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/sap/fhir/model/r4/FHIRContextBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ sap.ui.define([
this.sGroupId = mParameters && mParameters.groupId || oContext && oContext.sGroupId;
this.bUnique = mParameters && mParameters.unique;
this.oElementContext = Context.create(this.oModel, this, this.sPath, this.sGroupId);
this.oElementContext._loadContext();
var sChangeReason = FHIRUtils.getNumberOfLevelsByPath(this.sPath) === 1 ? ChangeReason.Refresh : undefined;
this.oElementContext._loadContext(sChangeReason);
}
});

/**
* Checks if the context binding needs to be updated
* Checks if the context binding needs to be updated.
*
* @param {boolean} bForceUpdate To force the update of the binding
* @protected
* @since 1.0.0
*/
FHIRContextBinding.prototype.checkUpdate = function() {
FHIRContextBinding.prototype.checkUpdate = function(bForceUpdate) {
if (this.isRelative() || this.bIsCreatedResource || this.bIsLoaded){
this.oElementContext._markAsReady();
this.oElementContext._markAsReady(this.oElementContext.iTotal);
}
};

Expand Down
12 changes: 7 additions & 5 deletions src/sap/fhir/model/r4/FHIRPropertyBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,22 @@ sap.ui.define([
* Initializes the binding, will force an update of the property binding
*
* @see sap.ui.model.Binding.prototype.initialize
* @returns {sap.fhir.model.r4.FHIRPropertyBinding} <code>this</code> to allow method chaining
* @protected
* @since 1.0.0
*/
FHIRPropertyBinding.prototype.initialize = function() {
this.checkUpdate(false);
return this;
};

/**
* Updates the binding value and sends a change event if necessary. A change event is sent if the <code>bForceUpdate</code> parameter is set to <code>true</code> or the current value of the
* binding isn't equal with the value stored in the model.
* @param {boolean} bForceUpdate Force update of binding
* Updates the binding value and sends a change event
*
* @param {boolean} [bForceUpdate] Force update of binding
* @param {object} [mChangedResources] The object containing the changed resources
* @param {sap.fhir.model.r4.HTTPMethod} [sMethod] The http method which triggered the checkupdate()
* @param {string} sChangeReason The reason for the fireChange event
* @param {string} [sMethod] The http method which triggered the checkupdate()
* @param {string} [sChangeReason] The reason for the fireChange event
* @protected
* @since 1.0.0
*/
Expand Down
37 changes: 37 additions & 0 deletions test/qunit/model/FHIRContextBinding.integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
sap.ui.define(["../utils/TestUtilsIntegration", "../utils/TestUtils"], function (TestUtilsIntegration, TestUtils) {
"use strict";

var oModel, oContextBinding;

function createModel(mParameters) {
oModel = TestUtils.createFHIRModel("http://localhost:8080/fhir/R4/", mParameters);
}

function createContextBinding(sPath, oContext, mParameters) {
oContextBinding = oModel.bindContext(sPath, oContext, mParameters).initialize();
}

QUnit.module("Integration-Tests: FHIRContextBinding", {
beforeEach: function () {
createModel();
}
});

QUnit.test("Check if contextbinding is initialized correctly", function (assert) {
var done = assert.async();
var fnChangeHandler1 = function(oEvent){
oContextBinding.detachChange(fnChangeHandler1);
var aPatientKeys = Object.keys(oModel.oData.Patient);
assert.strictEqual(oContextBinding.isInitial(), false);
assert.strictEqual(oContextBinding.bPendingRequest, false);
assert.strictEqual(oContextBinding.bUnique, undefined);
assert.strictEqual(aPatientKeys.length, 4);
assert.strictEqual(oContextBinding.getBoundContext().iTotal, 4);
assert.strictEqual(oContextBinding.getContext(), undefined);
assert.strictEqual(oContextBinding.getPath(), "/Patient");
done();
};
createContextBinding("/Patient", undefined, undefined);
oContextBinding.attachChange(fnChangeHandler1);
});
});
2 changes: 1 addition & 1 deletion test/qunit/model/FHIRModel.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ sap.ui.define([
var oRequestHandlePatient = mRequestHandles.patientDetails3;
var oRequestHandlePatient2 = mRequestHandles.patientDetails4;

assert.ok(2, Object.keys(mRequestHandles));
assert.ok(Object.keys(mRequestHandles), 2);

// check requesthandle for group id patientDetails
assert.equal(oRequestHandlePatient.getUrl(), "https://example.com/fhir", "The request has the correct url");
Expand Down
64 changes: 64 additions & 0 deletions test/qunit/model/FHIRPropertyBinding.integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
sap.ui.define(["../utils/TestUtilsIntegration", "../utils/TestUtils"], function (TestUtilsIntegration, TestUtils) {
"use strict";

var oModel, oContextBinding, oPropertyBinding;

function createModel(mParameters) {
oModel = TestUtils.createFHIRModel("http://localhost:8080/fhir/R4/", mParameters);
}

function createContextBinding(sPath, oContext, mParameters) {
oContextBinding = oModel.bindContext(sPath, oContext, mParameters).initialize();
}

function createPropertyBinding(sPath, oContext, mParameters) {
oPropertyBinding = oModel.bindProperty(sPath, oContext, mParameters).initialize();
}

QUnit.module("Integration-Tests: FHIRPropertyBinding", {
beforeEach: function () {
createModel();
}
});

QUnit.test("Check if propertybinding is initialized correctly if context is a aggregation of resources", function (assert) {
var done = assert.async();
var fnChangeHandler1 = function(oEvent){
oContextBinding.detachChange(fnChangeHandler1);
createPropertyBinding("%total%", oContextBinding.getBoundContext());
assert.strictEqual(oPropertyBinding.getValue(), 4, "The value of the propertybinding contains the correct 'total' value.");
done();
};
createContextBinding("/Patient", undefined, undefined);
oContextBinding.attachChange(fnChangeHandler1);
});

QUnit.test("Check if propertybinding is initialized correctly if context is a single resource", function (assert) {
var done = assert.async();
var fnChangeHandler1 = function(oEvent){
oContextBinding.detachChange(fnChangeHandler1);
createPropertyBinding("name/0/given/0", oContextBinding.getBoundContext());
assert.strictEqual(oPropertyBinding.getValue(), "Hans", "The value of the propertybinding contains the correct given name.");
done();
};
createContextBinding("/Patient/a2519", undefined, undefined);
oContextBinding.attachChange(fnChangeHandler1);
});

QUnit.test("Check if propertybinding is initialized correctly if propertybinding is created before context is loaded", function (assert) {
var done = assert.async();
createContextBinding("/Patient", undefined, undefined);
createPropertyBinding("%total%", undefined);
var fnChangeHandler1 = function(oEvent){
var fnChangeHandler2 = function(oEvent){
oPropertyBinding.detachChange(fnChangeHandler2);
assert.strictEqual(oPropertyBinding.getValue(), 4, "The value of the propertybinding contains the correct 'total' value.");
done();
};
oContextBinding.detachChange(fnChangeHandler1);
oPropertyBinding.attachChange(fnChangeHandler2);
oPropertyBinding.setContext(oContextBinding.getBoundContext());
};
oContextBinding.attachChange(fnChangeHandler1);
});
});
28 changes: 28 additions & 0 deletions test/qunit/model/FHIRPropertyBinding.unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
sap.ui.define(["../utils/TestUtils"], function (TestUtils) {
"use strict";

var oModel, oPropertyBinding;

function createModel(mParameters) {
oModel = TestUtils.createFHIRModel("https://example.com/fhir", mParameters);
}

function createPropertyBinding(sPath, oContext, mParameters) {
oPropertyBinding = oModel.bindProperty(sPath, oContext, mParameters);
}

QUnit.module("Unit-Tests: FHIRPropertyBinding", {
beforeEach: function () {
createModel();
}
});

QUnit.test("create and initialize", function (assert) {
var sPath = "/Patient/123/name/0/given";
createPropertyBinding(sPath, undefined);
assert.strictEqual(oPropertyBinding.isInitial(), false, "The binding is after creation not in 'initial' mode.");
assert.deepEqual(oPropertyBinding.initialize(), oPropertyBinding, "The initialize method returns the binding itself.");
assert.strictEqual(oPropertyBinding.isInitial(), false, "The binding is after initialization not in 'initial' mode.");
});

});
5 changes: 4 additions & 1 deletion test/qunit/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ sap.ui.define([
"./model/FHIRModel.integration",
"./model/FHIRTreeBinding.unit",
"./model/FHIRTreeBinding.integration",
"./model/FHIRListBinding.integration"
"./model/FHIRListBinding.integration",
"./model/FHIRContextBinding.integration",
"./model/FHIRPropertyBinding.unit",
"./model/FHIRPropertyBinding.integration"
], function() {
"use strict";
});
3 changes: 2 additions & 1 deletion test/sap-fhir-test-app/webapp/test/opa5/journeys/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ sap.ui.require([
"sap-fhir-test-app/test/opa5/journeys/patient/PatientTable",
"sap-fhir-test-app/test/opa5/journeys/patient/PatientDetails",
"sap-fhir-test-app/test/opa5/journeys/other/SlicingIncludesChainingReference",
"sap-fhir-test-app/test/opa5/journeys/history/History"
"sap-fhir-test-app/test/opa5/journeys/history/History",
"sap-fhir-test-app/test/opa5/journeys/other/NavigationJourney"
], function(Opa5, Arrangements, Actions, Assertions) {
"use strict";

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sap.ui.require(["sap/ui/test/opaQunit"], function(opaTest) {
"use strict";

var sHash = "";
var sPatientTableHash = "patients";
var sView = "sap-fhir-test-app.view.Home";
var sPatientTableViewName = "sap-fhir-test-app.view.patient.PatientTable";

QUnit.module("Navigation Example");

opaTest("Check total binding on start screen", function(Given, When, Then) {
Given.iStartMyApp(sHash);
Then.theGenericTileBindingShouldDeliver(sView, "allPatients", {value : "4"});
});

opaTest("Check total binding after navigation", function(Given, When, Then) {
Given.iStartMyApp(sPatientTableHash);
When.iPressNavBackButton(sPatientTableViewName, "patientPage");
Then.theGenericTileBindingShouldDeliver(sView, "allPatients", {value : "4"});
});
});
1 change: 0 additions & 1 deletion test/sap-fhir-test-app/webapp/view/Home.view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
id="patientTile"
binding="{path : '/Patient', parameters : {request: {_summary : 'count'}}}">
<TileContent footer="resource">
<ImageContent src="sap-icon://wounds-doc" />
<NumericContent id="allPatients"
scale="Patients"
value="{%total%}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
xmlns:l="sap.ui.layout"
controllerName="sap-fhir-test-app.controller.patient.PatientTable"
viewName="sap-fhir-test-app.view.patient.PatientTable">
<Page title="Patient Search"
<Page id="patientPage" title="Patient Search"
showNavButton="true"
navButtonPress="onNavBack">
<content>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:l="sap.ui.layout">
<l:HorizontalLayout class="sapUiSmallMarginBegin sapUiSmallMarginEnd">
<Table id="tblVers"
noData="{i18n>noData}"
noDataText="{i18n>noData}"
items="{/Coverage}">
<headerToolbar>
<Toolbar class="sapUiMediumMarginTop"
Expand Down

0 comments on commit 75a41f3

Please sign in to comment.