Business Example
In this exercise, you will write a unit test for a formatter.
SAP Business Technology Platform and its tools are cloud services. Due to the nature of cloud software, step procedures and naming of fields and buttons may differ from the exercise solution.
Task 1: Create an SAP Fiori Freestyle Project
Steps
Log into your SAP Business Application Studio.
Perform this step as shown in a previous exercise.
Create a new SAP Fiori Freestyle project using the following information:
SAP Fiori Freestyle Project Information
Field Value Template SAP Fiori Application Template Type SAP Fiori Template Basic Data source None View name Main Module name ux402_qunit Application title Unit Tests Application namespace student##.com.sap.training.ux402.qunit Description A Unit-test Application. Project folder path /home/user/projects Leave other fields and radio buttons on default value.
Perform this step as shown in a previous exercise.
Task 2: Create the Function you will Test
You want to create a function to display dates in a pretty format.
Steps
Add a file with the name
Formatter.js
into the webapp/model folder and implement theFormatter.js
file, as shown in the following figure. Then save your work.Note
if you prefer, you can upload theFormatter.js
file located under Templates folder in the files you got from Github.Create or upload the
formatter.js
file as shown in previous exercises.Write or Explore the code. You can use the following:
Code snippetExpandsap.ui.define([], function() { "use strict"; return { prettyDate : function (now, time) { var date = new Date(time || ""), diff = (((new Date(now)).getTime() - date.getTime()) / 1000), dayDiff = Math.floor(diff / 86400); if (isNaN(dayDiff) || dayDiff < 0 || dayDiff >= 3) { return; } var oResult = dayDiff === 0 && ( diff < 60 && "just now" || diff < 120 && "1 minute ago" || diff < 3600 && Math.floor(diff / 600) + " minutes ago" || diff < 7200 && "1 hour ago" || diff < 86400 && Math.floor(diff / 3600) + "hours ago") || dayDiff === 1 && "Yesterday" || dayDiff < 7 && dayDiff + " days ago" || dayDiff < 31 && Math.ceil(dayDiff / 7) + "weeks ago"; return oResult; } } });
Task 3: Create the Unit Test
Steps
Add a folder with the name model to the webapp/test/unit folder.
Perform this step as shown in a previous exercise.
Add a file with the name Formatter.js to the webapp/test/unit/model folder.
Perform this step as shown in a previous exercise.
Define a SAPUI5 module and add the dependency to the Formatter that you created in step 6.
Add the following lines of code:
Code snippetExpandsap.ui.define([ "student##/com/sap/training/ux402/qunit/ux402_qunit/model/Formatter" ], function(Formatter) { "use strict"; });
Add a QUnit module to the newly created SAPUI5 module. Use the
QUnit.module
function. As the module description, useFormatter
and use thebeforeEach
hook to create a_formatter
member variable with reference to theFormatter
.Add the following lines of code after
"use strict";
statement:Code snippetExpandQUnit.module("Formatter", { beforeEach: function () { this._formatter = Formatter; } });
In the
Formatter.js
file, which is located in the webapp/test/unit/model folder, define a test case with theQUnit.test
function. UseTest the pretty date implementation
as the test case description. In the assigned function, create a variable with the name now. Assign the value2008/01/28 22:25:00
to thenow
variable. Use theequal
function of the assertion object to check if the date in thenow
variable is equal to the expected values in the following table. Print out the mentioned assertion messages.Hint
You will use theprettyDate
function of theFormatter
as first argument of theequal
function and the listed assertion messages as the second argument of theequal
function.Expected Values and Corresponding Assertion Messages
Expected Value Assertion Message 2008/01/28 22:25:00 just now 2008/01/28 22:24:00 1 minute ago 2008/01/28 22:22:00 3 minute ago 2008/01/28 21:25:00 1 hour ago 2008/01/27 22:25:00 Yesterday 2008/01/26 22:25:00 2 days ago 2007/01/26 22:25:00 undefined Add the following lines of code after the
QUnit.module
function call:Code snippetExpandQUnit.test("Test the pretty date implementation", function(assert) { var now= "2008/01/28 22:25:00"; assert.equal(this._formatter.prettyDate(now,"2008/01/28 22:25:00" ), "just now"); assert.equal(this._formatter.prettyDate(now,"2008/01/28 22:24:00" ), "1 minute ago"); assert.equal(this._formatter.prettyDate(now,"2008/01/28 22:22:00" ), "3 minutes ago"); assert.equal(this._formatter.prettyDate(now,"2008/01/28 21:25:00" ), "1 hour ago"); assert.equal(this._formatter.prettyDate(now,"2008/01/27 22:25:00" ), "Yesterday"); assert.equal(this._formatter.prettyDate(now,"2008/01/26 22:25:00" ), "2 days ago"); assert.equal(this._formatter.prettyDate(now,"2007/01/26 22:25:00" ), undefined); });
Save your work. Your
Formatter.js
file should now look like the following:
Task 4: Adjust namespaces
Steps
Open the
AllTests.js
file and replace the given namespace withstudent##/com/sap/training/ux402/qunit/ux402_qunit/test/unit/controller/Main.controller
andstudent##/com/sap/training/ux402/qunit/ux402_qunit/test/unit/model/Formatter
.Open the
AllTests.js
file located in the unit folder of your project.Replace the given code with the following lines of code:
Code snippetExpandsap.ui.define([ "student##/com/sap/training/ux402/qunit/ux402_qunit/test/unit/controller/Main.controller", "student##/com/sap/training/ux402/qunit/ux402_qunit/test/unit/model/Formatter" ], function () { "use strict"; });
Open the
webapp/test/unit/controller/Main.controller.js
file and adjust the namespaces. Replace the given namespace withstudent##/com/sap/training/ux402/qunit/ux402_qunit/controller/Main.controller
.Open the
webapp/test/unit/unitTests.qunit.js
file and adjust the actual namespace. Replace the given namespace withstudent##/com/sap/training/ux402/qunit/ux402_qunit/test/unit/AllTests
.Open the
webapp/test/unit/unitTests.qunit.html
file and adjust the resource roots configuration.Locate the
data-sap-ui-resourceroots
value allocation (probably at around line 9 – 15). Replace the value allocation with following:'{"student##.com.sap.training.ux402.qunit/ux402_qunit": "../../"}'
Task 5: Run the Test
Steps
Run your unit-tests and check the result.
Open the context menu on your project folder ux402_qunit.
Select Preview Application.
Choose the option unit-tests fiori run –open text/unit/unitTests.qunit.html.