on the last videos of our rio app with the ionic framework we implemented the state management for the login page using ngrx we also implemented enough service that's called when the user is recovering the password or when the user is trying to log in if we start thinking about architectural responsibilities and isolation we'll come to a place where our page should have two main responsibilities the first one is that it should know how to present itself to the users this we achieve by the html the css and the state of the page the second one
is that it should know how to respond to the user interaction this we achieve by handling the button clicks for example but we can actually see that our login page component is also responsible for accessing external resources by making the calls to the off service here's how our app is working now we have our component which is the login page when the user clicks on the login button for example we tell the store to dispatch the login action this logging action was captured by the reducer which knows how to handle the logging action the reducer
changes the logging state based on that action and as the login page is observing the logging state the login page then calls the out service logging after the of service login responds of success the login page calls the login success action which is captured by the reducer the reducer knows how to handle that action and changes the logging state of the store the logging page is observing the new state of the logging sees that the logging was successful and takes the user to the home page by days we can see that our page is doing
too much our page doesn't need to know how the logging is going to be made or what service to call in order to do the password recovery so what actually should happen is that this code that's responsible for handling the logging shouldn't be here also the code that's responsible for handling the password recovery shouldn't be here i can also remove the calls to those two functions that i just deleted on my code by doing this we shouldn't need the of service anymore and also we can remove all those imports that are not being used in
our page notice that now all that our page is doing is responding to the state i mean presenting itself according to what the state is saying and also responding to the user interaction so now our code for the login page is much cleaner as it's only responsible for doing what it needs to do and also it's isolated you could say okay cool but how do we do now the password recovery and the login the of service has not being called anywhere in order to make this work we are going to implement the ngrx side effects
which provide us a way to interact with those services and isolate them from our components before implement the ngrx effect let me explain to you how it's gonna work using the same example as i used before we have our component which is the login page when the user clicks on the login button for example we tell the store to dispatch the logging action this logging action is captured by the reducer which knows how to handle the logging action the reducer changes the logging state of the store based on that action but now we have side
effects what's gonna happen is that those side effects will keep watching every action that comes to the reducer and will do something based on those actions so when the logging action comes to the reducer the effects will realize that the user is trying to log in so the effects are gonna call the house service login function which will then try to log in by calling the backend or on our case the firebase authentication signing at some point the firebase authentication signing will respond with success or error to our service let's imagine it responded with success
the service will then take that success response back to the effect and the effect we'll call the login success action the reducer knows how to handle that action and changes the logging state of the store the login page is observing the new state of the logging sees that the logging state is successful and takes the user to the home page can you see how different pieces of our app will now have single responsibilities and now are isolated from each other notice that this part here of the diagram is responsible for letting the logging page know
how it should present itself by observing the current state of the app the logging component only tells the store that it should do the logging but it has no idea how the logging is actually being done this other part here the effects part is responsible for calling the external resources i mean the services based on some action that was handled by the reducer the effects have no idea who is calling them or when that call is being made it only knows what to do with that call this also will make our tests much simpler let's
go back to our code and tell npm to run the tests we can see now that two of our tests are failing for the login page so let me open the login page tasks and let me find this error the first error you have is on the should hide loading and show error when user couldn't log in notice that now we are only interested in testing that the page is presenting itself correctly based on the current state sdl service doesn't matter here anymore this state we are testing here is the login fail state i mean
the user set a valid email and a password clicked on the login button we marked the elf service so the response would be an error and then we test if the page responded accordingly which is to hide the loading component and to show an error message so as we don't need the of service anymore i will start by removing this mock i also don't need to set an email a password and click on the login button to get to the login fail state i removed this code and just change the state according to what it
needs to do so after the page has started here on the fixtures.detect changes i'll just tell the store to dispatch the logging action then i'll tell the store to dispatch the login fail action and i'll send to it as parameter the error object with a message that is the error message after i save this our test will pass notice that here i'm just asking if the page is responding accordingly to the logging state on the store i don't care about calling services anymore i'm just testing the behavior of the login page so let me find
out which other test is failing now so i'll copy it here and find it on our code should hide the loading and send user to home page when user has logged in so here we are testing the logging success case again i can get rid of the of service spy let me also just remove this code that's doing the logging now i can just tell the store to dispatch the login action and finally i'll just tell the store to dispatch the login success action and accentuate as parameter a new user and now my test is
checking that when the login success action happens the loading component is hidden the user is logged in and we send the user to the home page after i save this our task will pass remember that on the last video we had to create some tricks on some tasks to pass like this one here which should show the loading component and start the login when the user is logging in i have to spy on the login function and make it return an observable that would never complete so the logging state wouldn't change well i don't need
to spy on this anymore a few lines up we did the same thing spying on a function so it wouldn't respond we also don't need that and after those changes we can see that the house service is not being called anywhere else on the tests now so i can remove it from the tests after i save this our tests keep passing notice that now we have two kinds of tests one that's making the page respond to user interaction like this one here where we are starting the password recovery and another one that's testing if the
page is behaving accordingly to the state that doesn't come from user interaction like this one here where we are telling the test that the password recovery was successful well i think that for the second kind of test we should use some different structure for the name of our tests so they explain themselves better instead of the shoot structure i mean it should hide the loading and show a success message when has recovered password let's use the given when then structure so let me change it to given the user is recovering password when success then hide
loading and shows success message the next test is doing the same but for the error case so let me copy and paste it change from success to fail and from send user to home page to show error message the next test is making the page respond to the user interaction so let's keep it like this the next one isn't so let's change it to given user is logging in when success then hide loading and send user to home page the next test is doing the same but for the error case so let me copy and
paste it change from success to fail and from send user to home page to show error message after i save this our tests keep passing cool now all of our tests are passing and they have good explanatory names on the next video we will actually implement the ngrx effects on our app so subscribe to the channel to follow this development hit the like button if you enjoyed the video share it for deaf friends and see you on the next video