so now let me close all the files and let's implement the state management for the email password recovery for the login page first i need access to the store so let's get it on the constructor now our tests for the login page are failing because the tests don't have access to the store so let's add it to the imports i'll need the store module i'll need the loading reducer and i also need the login reducer after i save this now our tests will pass so let's create a function called forgot email password that will be
called when the user clicks on the forgot email password button and now let's create a test for that forgot email password functionality it should recover email password on forgot email password let's start by expecting that true is false as we have a failing test let's start in this test i'll simulate the whole flow meaning that i'll simulate that the logging page is started the user set a valid email on the input field and the user clicked on the recover email password button then i would expect that the property is recovering password of the logging state
is equal to true with this the page will know that the user is trying to recover the password and then the page will be able to respond to it so let's code this test first i have to start the page that's pretty easy i just need to tell the fixture to detect the changes now i have to simulate the user input a valid email remember that i have access to the component and that the component has a form i can then get the email and set as the value a valid email now for the click
on the button i need access to the elements of the page so i'll create a new property to the test called page before each test this page will receive from the fixture debug element the native element the page has a function called query selector which will search in the page for some css like selector so let's just query for the selector with the id equal to recover password button and then i can click on it and finally i have to ask the store if the is recovering password property is set to true to do this
i first need access to the store in my test so let's create a new property for the test which is the store and before each test i will initialize that store using testbed now on the store i can select the login state and subscribe to it meaning that now i am observing the login state with that logging state i can expect that on the login state the is recovering password property is equal to true after i save this our task will keep failing the first thing that's making it fail is that we didn't define this
id on our login page so let's do this i'll copy the recov password button string go to the login page and define here the id of my forward email password button i'll save it again and now our test will keep failing but because we didn't do anything to change the state of the store on the forgot email password function of our page let's tell the store to dispatch the recover password action after i save this our test will pass so let's take a look at the app itself and the state i'll come to the redux
tab and here you can see that our store has the initial state i will input an email at the email field and now click on the forgot email password button after this we see that now the is recovering password property of the logging state is equal to true cool but we are actually not doing anything with that information by now what we will do now is to make the login page observe the login state when the is recovering password property is equal to true will show the loading component when it becomes false we'll hide the
loading component so let's go back to the code let's create a new test for this scenario it should show the loading when is recovering password to achieve this we need a few things first we need to start the page then we need to change the is recovering password to true and then we need to verify if on the loading state the show property is equal to true so let's start by telling the fixture to detect the changes to change the is recovering password to true i simply need to tell the store to dispatch the recover
password action and now i just need to go to the store select the loading state and subscribe to it now i have access to the loading state and i can expect that on the loading state the show property is equal to true after i save this our test will fail it fails because i'm not informing anywhere that the loading should be shown when the user is trying to recover the password so let's do this remember that the login page has to observe the logging state in order to know what to do so when the page
is initialized i'll get the store and select the logging state then i'll subscribe to it and i'll get access to the logging state now what i can do is just check if the logging state dot is recovering password property is equal to true if it is then i'll get start and dispatch an action to show the loading component after i save this our test will pass so let's go to the app and see what's happening on the redux so you can understand it better first we are in the initial state so nothing is happening i
will input a valid email and when i click on the forgot email password button i will call the recover password action this will set that as recovering password to true and will make the subscription code run again and by having this recovering password as true the show action is called and now we are showing the loading component but the loading is never disappearing what we need to do now is to create a service that will be responsible for calling some backend or something like this and that will actually recover the password if everything was fine
then we hide the loading component and show a success message if it had an error then we should hide the loading component and show an error message let's do the first one then i'll create a new test that it should hide the loading component and show a success message when has recovered the password i will start with a failing test what i need to do here is to start the page then set the login state as recovering password then i inform that the logging state has recovered the password correctly and i'll verify that on the
loading state the show property is false and that a success message was shown first i'll start the page now i'll tell the store to dispatch the recover password action now i'll tell the store to dispatch the recover password success action so i'm telling that the new state of the login page is the state where the password recovery worked now i have to get the store and select the loading state i'll subscribe to it to get access to the loading state and i'll expect that on the loading state the show property is false and finally i'll
verify if a success message was shown to do that i will use the toast component of the ionic framework so i need to create a new property for the test called dose controller of the type dosed controller i will initialize before each test this property using the test bed the toast controller has a function called create what i need to do is to spy on the toast controller function create and then i would expect that the toast controller with the create function was called once when i save it the test will fail and it fails
because i'm actually not doing anything that this test is expecting me to do so let's make it pass what i need to do is to come to the subscription of the login state and add another condition here i'll check if the logging state dot is recovered password is true in case it's true then i need to hide the loading component to do this i just need to tell the store to dispatch the hide action now i need to show the success message so i need access to the toast controller i get access to it from
the constructor now i can create a constant called toaster and then i will await that the toast controller will create a toaster i'll put it to be positioned at the bottom of the page i'll set the message of the toaster as recovery email sent and output its color as primary i needed to put that await there because the create function is asynchronous so i also need to tell that my subscription is asynchronous and finally i'll tell the toaster to present the message after i save this our test will pass but if we go to the
actual web set a valid email and click on the forgot email password button we see that the loading component still never disappears that happens obviously because we are never calling the recover password success action to make it disappear what should happen is that we should call a service that service would call the back end the back end would return a success response and we would then call the recover password success action so let's do this but first let's do some refactor here because one function is getting too long so i'll create two new functions one
for each state and now just copy and paste the code of each state this function has to be a synchronous i can remove this asynchronous call here and now i'll just call the true functions now yeah much better all right so now let's create a service that will be responsible in the future for calling the backend to do that i create a new command line and i'll tell ionik to generate a service on the services slash alf folder with the name alf we can see that two new files were created which is our service and
the test for it so let's go to our service and make a fake call to a fake backend i'll create a function called recover email password which receives an email as a string this function returns an observable without any value as this is a fixed service for now i mean in the future we'll create a real one calling the back end don't worry i will return a new observable and i will set a timeout of three seconds for it to respond inside of the timeout i will create a condition if the email is equal to
error at email.com then i'll tell the observer to return an error object with the message equal to email not found if not then i'll just tell the observer to send the answer and complete its job by doing this we can test both scenarios so let's go back to the login page and inform that now we need to use that of service that i just created then what i need to do is when the user is trying to recover the password i will call the service to recover the password and pass to it the email value
now i'll subscribe to the service and when it answers successfully i'll tell the store to dispatch the recover password success action after i save this our tests keep passing now let's go to the actual app and see what's happening on the redux first we are in the initial state so nothing is happening i will input a valid email and when i click on the forgot email password button i will call the recover password action this will make the subscription run again and as the is recovering password is true we show the loading component and call
the fake service three seconds later it responds with success and we dispatch the recover password success action the state changes again the subscription runs again and now as the is recovered password is true we hide the loading component and show the success message now let's do the error case and show the error message let's create a new test that it should hide the loading component and show an error message when there is an error on the recover password first let's make this test fail what i need to do here is to start the page then
try to recover the password then change the stage to inform that it failed then expect that the loading component is not showing and an error message was shown so i will start the page now i will recover the password the recovery password failed now i'll just copy this part from the last task as they are basically the same when i save it our test will fail so let's change our code to make it path here on the login page i need a new function for the case when there is an error on the password recovery
i'll just check if there is an error on the logging state if the error is present then i'll tell the store to hide the loading component and i'll tell the toaster to show a message let me just copy the instruction from here below and paste it now i need to set this function as a synchronous to change the color from primary to danger and to actually use the error message from the login state after i save this our test will pass oops sorry i forgot to actually call this function here after i save this our
task will pass now let's go to the actual app i will put the error email and click on the forgot email password button the loading component shows up but it never disappears this happens because we are actually not doing anything when there is an error on the off service so let's do it the subscription also can have a narrow structure that we declare after the success instruction so i'll just tell the store to dispatch the recover password fail action with the arrow returned by the service after i save this our tests keep passing now let's
go back to the actual app and see what's happening on the redux first we are in the initial stage so nothing is happening i will input the error email and when i click on the forgot email password button i'll call the recover password action this will make the subscription run and as the is recovering password is true we show the loading component and call the fake service three seconds later it responds with an error and we dispatch the recover password fail action with the error the state changes the subscription runs again and now as the
error is not now we hide the loading component and show the error message and it's done we just did the password recovery state management on the logging screen actually only one detail is missing the subscription to the store keeps alive forever and this code will run every time the login state changes but after the user leaves the login page we don't actually need the login screen to keep an eye on the logging state anymore so we have to unsubscribe that subscription when the page is destroyed to do this i'll inform that the login page implements
the ondestroy interface from ionic now i need to declare a ng on destroy function which will be executed when we destroy this page now i need to create a new property to our page which is the login state subscription of the type subscription now let's make this property receive the response from our subscription and finally i can just come to the destroy function and verify if the subscription exists if it does then i can just unsubscribe from it and that's it now let's do the state management for the actual login functionality of the login page