Testing Cubbles components
Purpose
To demonstrate how to test a Cubbles component in a browser.
Prerequisites
To be able to test a component, you should have:
Created a compound or elementary component. In the case of this tutorial, we will test the
currency-converter
elementary, whose webpackage can be cloned from its GitHub repository.Installed the CDT dependencies running the
npm install
command from its root folder (usually called devtools)
You can check this tutorial to know how to create elementary components and to read more details about the currency-converter` elementary. Also, a demo of this component is available online.
Testing the currency-converter component
To be able to test our component, we need to have a test environment to write and run unit tests. Run the _generateArtifactWctScaffold task to generate that environment.
The created environment will allow you to write tests using Chai 2.3.0 assertions and Sinon 1.17.1.
Preparing the tests
As you may know, you should write the component tests in the currency-converter-test.js
file inside the test
folder. You should follow the following steps:
For our purposes, you can remove the sample tests and leave only the outer
describe
of the scriptSet a high timeout number (e.g. 70000) because loading components locally can take too long depending on the internet connection and the number of components
Create a variable to access the component
Create a variable for a stub to wrap the
makeRequest
function that makes the AJAX requests in the component, so that our tests do not rely on an external API. For more information about Sinon Stubs check thisCreate a variable to maintain the expected values for the assertions
Init those values within a
before
functionWait until the component is ready to run the tests
Restore the created stub within an
after
function
The previous process is illustrated below:
Testing initial status of the currency-converter component
Note that the currency-converter
slots should have the initial values defined in its manifest.webpackage
file. These values are presented below:
Slot
Value
base
'EUR'
foreignCurrency
'USD'
date
'2018-01-01'
As you may noticed, the makeRequestStub
Stub that we defined will return an object in the format in which the conversion API would do it (i.e. { [base_foreignCurrency]: { val: { [date]: conversion } }
). It will use the values of expectedVals.init
if the value of the slot is equal to expectedVals.init.base
, which corresponds to the value defined in the manifest.
Therefore, we will test that:
The initial value of the
foreignCurrency
slot is equal to the one defined in themanifest.webpackage
fileThe initial value of the
date
slot is equal to the one defined in themanifest.webpackage
fileThe initial value of the
base
slot is equal to the one defined in themanifest.webpackage
fileThe
makeRequest
method is called (An API request is done)The first value of the
conversion
slot is equal to the value ofexpectedVals.init.conversion
. That is, that the API answer is processed correctly
The above tests can be done with the following code:
Testing that slots of the currency-converter component are set correctly
Now, we will test that the component behaves correctly when the slots are set using the setSlotName
methods offer by Cubbles. We should:
Set all the input slots and call the method that sends the query to eh API
Check that the
foreignCurrency
slot was set correctlyCheck that the
date
slot was set correctlyCheck that the
base
slot was set correctlyCheck that the
conversion
slot was calculated correctly
The code below illustrates the previous steps:
Result
T o run the tests in the browser, start a local webserver running the following command:
If everything goes well, the bash will display the following message:
Then, navigate to http://localhost:8282/my-first-webpackage/currency-converter/test/index.html. Wait until the tests are executed. If everything goes well, you should get the following messages:
Check it online
You can run the tests online. Also, the source code of the tests is available at GitHub.
Last updated