Testing with reactiveUtils

177
2
03-14-2024 09:10 AM
Tom_Prutz
New Contributor II

I am currently working on a part of our application that will display a subset of topo data under certain circumstances. I am trying to test part of that functionality but I am currently unable to get my code to run in a test.

I am quite new to the esri stack and particularly front end testing so it is possible I'm doing something wrong or misunderstanding a concept, however a lot of trial and error and Google searching hasn't turned anything up.

At the most basic level my issue is that I can't get the watcher created by a reactiveUtils instance to trigger.

I have a MapView that is passed into my function I then have code that looks like this

reactiveUtils.when(
                () => view.stationary === true,
                () => {
                         do stuff
});
 
I am mocking the view and as such when I pass it in I can confirm that the value of stationary is true. I hoped that would cause the code inside that reactiveUtils to trigger but it does not. I have verified that the value is true at that point and that no code inside is running.
 
At this stage I'm at a loss. I don't know if it is possible to trigger my mock to start false and then switch true at the right time. I suspect not as the test would finish before that could occur. Equally I don't know how to mock out or otherwise trigger that reactiveUtils. 
 
An option is to pull the code inside, out and test that separately but that then does not encompass everything I want to test. Another option would be to feed in a real view and simulate it being moved. That is a bigger scope than what I am trying to do here.
 
Can anyone suggest an option for how I can get this to work? Happy to provide more information/code if that will help. There seems to be a general lack of documentation around using things like these in tests, does such documentation exist and if so please could someone point me towards it?
 
Tags (2)
2 Replies
Egge-Jan_Pollé
MVP Regular Contributor

Hi @Tom_Prutz, Good evening,

I don't know whether your question is still relevant, as you posted it almost 2 weeks ago...

Actually I think your code snippet should work, so we have got to find out why it is not working - as you say - in your case. You can find more information about reactiveUtils in the ArcGIS JS API Reference, and there it is suggested to put a question mark behind view. But I did test your sample without a question mark, and that did the job as well...

reactiveUtils.when(
  () => view?.stationary === true,
  () => {
    console.log("view is stationary now");
  });

 

To see a working example you have a look at this blogpost ArcGIS JavaScript with UK data - URL.searchParams. It might seem an older post, but I just updated it tonight, to use reactiveUtils (instead of the deprecated, and now even completely removed, watchUtils which I used in an earlier version of the post). As you can see, the parameters in the URL are updated every time the view becomes stationary.

So, in the end, the conclusion might be, that your issue is caused by your test setup. Can you please provide us with more information/code to explain what you are trying to do?

Cheers,

Egge-Jan

0 Kudos
Tom_Prutz
New Contributor II

Thank you for coming back to me. I had to pivot a bit as I ran out of ideas and with the help of an Esri consultant we did manage to get it working by mocking out the reactiveUtils so from a practical stand point this is now resolved.

I suspect you may have been correct about the test setup and I will bear that in mind for the future.  Many Thanks.