Last active
January 2, 2022 16:30
-
-
Save sodogan/833cd67f6e70e6eb0955b01e6675ede4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//learning Qunit testing in here! | |
sap.ui.define([ | |
"com/sodogan/manage_products/test/unit/helper/FakeI18nModel", | |
"sap/ui/model/resource/ResourceModel", | |
"sap/ui/base/ManagedObject", | |
"com/sodogan/manage_products/model/formatter", | |
"sap/ui/thirdparty/sinon", | |
"sap/ui/thirdparty/sinon-qunit" | |
], function (FakeI18nModel, ResourceModel,ManagedObject, formatter) { | |
"use strict"; | |
const MAIL_DELIVERY = 'Delivery By Mail'; | |
const PARCEL_DELIVERY = 'Delivery By Parcel'; | |
const FREIGHT_DELIVERY = 'Delivery By Freight'; | |
const KG = "KG"; | |
/* formatter.delivery has dependancy to getModel and getResourceBundle | |
* getResourceBundle also calls getText() | |
* So we need a way to mock the | |
* delivery: function (sMeasure, iWeight) { | |
var oModel = this.getModel("i18n"); | |
debugger; | |
var oResourceBundle = oModel.getResourceBundle(), | |
sResult = ""; | |
if (sMeasure === "G") { | |
iWeight = iWeight / 1000; | |
} | |
if (iWeight < 0.5) { | |
sResult = oResourceBundle.getText("deliveryByMail"); | |
} else if (iWeight < 5) { | |
sResult = oResourceBundle.getText("deliveryByParcel"); | |
} else { | |
sResult = oResourceBundle.getText("deliveryByFreight"); | |
} | |
return sResult; | |
}, | |
*/ | |
QUnit.module("Delivery", { | |
beforeEach: function () { | |
//Now create a fake controller which will have a getModel method | |
console.log('Inside beforeEach'); | |
var fakeResourceModel = new FakeI18nModel({ | |
deliveryByMail: "Delivery By Mail", | |
deliveryByParcel: "Delivery By Parcel", | |
deliveryByFreight: "Delivery By Freight" | |
}); | |
//When its calle with i18n args it will return the fake Resouce which is ours! | |
/* You can use this fake controller or a managedObject is fine too | |
var fakeController = { | |
getModel: function () { | |
//empty | |
} | |
}; | |
*/ | |
var fakeController = new ManagedObject({}); | |
//intercept the getModel method and return a Fake ResouceModel | |
this._stub = sinon.stub(fakeController, "getModel").withArgs("i18n").returns(fakeResourceModel); | |
//bind the function to the fakeController! | |
this._fnFakeDelivery = formatter.delivery.bind(fakeController); | |
}, | |
afterEach: function () { | |
console.log('Inside afterEach'); | |
this._fnFakeDelivery = null; | |
} | |
}); | |
/* | |
* Using the FakeModel which is created for testing as a Fake model | |
* Notice that its is required | |
*/ | |
QUnit.test("O.1 KG will be delivered by Mail", function (assert) { | |
//Given | |
//When | |
let actual = this._fnFakeDelivery("KG", 0.2); | |
//Then | |
let expected = MAIL_DELIVERY; | |
assert.strictEqual(actual, expected, "Should be Mail delivery"); | |
//Make sure that spy is called! | |
//Then-check its called once! | |
sinon.assert.calledOnce(this._stub); | |
//check that | |
assert.ok(this._stub.calledWith("i18n"), "Check the arg"); | |
}); | |
/* | |
* Using our own ResourceModel which is created for testing as a Fake model | |
* Notice that its is required | |
*/ | |
QUnit.test("O.5 KG will be delivered by Mail", function (assert) { | |
//Given | |
//When | |
var fakeResourceModel = new ResourceModel({ | |
bundleName: "com.sodogan.manage_products.i18n.i18n" | |
}); | |
//debugger; | |
var fakeController = Object.create(null); | |
//When its calle with i18n args it will return the fake Resouce which is ours! | |
fakeController.getModel = sinon.stub().withArgs("i18n").returns(fakeResourceModel); | |
//bind the function to the fakeController! | |
var fakeDelivery = formatter.delivery.bind(fakeController); | |
let actual = fakeDelivery("KG", 0.2); | |
//Then | |
let expected = MAIL_DELIVERY; | |
assert.strictEqual(actual, expected, "Should be Mail delivery"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment