I'm working on an enterprise application that is being consumed as an angular element by another angular application.
Our application receives some input from the parent application which kicks off a bunch of api calls which then display data. One issue we are starting to run into is when we get quick, repeated inputs from the parent application.
For example, the parent application might input {"name": "Fred", "id": "abc"}, our application then starts to kick off a bunch of API calls using angular services which we subscribe to in our components. However, we are sometimes running into trouble if the parent component immediately were to input {"name": "Bob", "id": "xyz"} very shortly after the {"name": "Fred", "id": "abc"} input, before some of our API calls have completed. In these instances, we will sometimes be displaying data that belongs to the wrong person.
I can elaborate more if it would be helpful or post some modified code, don't think I'm allowed to share our actual code. I'm hoping to get some general advice or tips on how to deal with this type of situation.
One thing I have started to try is doing is checking to see if the subscription already exists each time we receive an input, resetting all of our display data and unsubscribing to the service, and then proceeding to start the subscription again. I'm not sure at this point if this has fixed the issue.
One other thing I was hoping to get some advice on is how to test for this situation. Is this something that could be tested using jasmine/karma (and somehow intentionally manipulating the response time in a mocked service), or is this something that would be more suited for Protractor/Cypress tests?
Thank you!
Edit -- Here is an imperfect stackblitz example, where the home component would be the focus, which accepts input from the app component
https://stackblitz.com/edit/angular-q4bv9y?file=src%2Fapp%2Fapp.module.ts
switchMapoperator can help a lot. - BizzyBob