43
Views
6
Comments
Solved
Advanced usage: How to override a system Javascript function such as CurrDate()
Question

I want a custom behavior of the system function CurrDate(). The reason is that for testing the functionality that is dependent on today's date, I should be able to change the date to anything and test that functionality.

Is there a way I can do that?

Another thing I couldn't find is how to find all usages of CurrDate() in all my modules so that I can replace them with my custom function for the same purpose stated above.

Thanks.

UserImage.jpg
Sanjay K
Solution

Found that it's not possible to override.

So I used my own solution:

  1. Defined a Client Variable to store date in Core data module.
  2. In Core data module, added a function TodaysDate that gets that date.
  3. Added another function SetTodaysDate that sets that date.
  4. Found the way to search in the UI module expressions for uses of CurrDate() and replaced it with TodaysDate() (after refreshing the reference to Core data)
  5. Added a special Tools menu to the app "Set Todays Date" that allows me to set any date as todays date and then test the App for the changed date.


2021-09-06 15-09-53
Dorine Boudry
Ā 
MVP

ok,

yes, I think a client variable can work, but will only work on device logic, if you have any business rules checked in the server, or you have any timers or processes that are date sensitive, they won't be able to reach that same client variable.  If you have several users testing, they will not have a unified date.

2024-07-05 14-16-55
Daniël Kuhlmann
Ā 
MVP

Honestly I don't think this reply should be marked as solution. It only works cliƫnt side. Also it should be a foundation module not a core data module.

UserImage.jpg
Sanjay K

I disagree. Client side allows 2 different screen developers to unit test their screens independently, using different date as today's date for their own screen's functionality. This way, they won't step on each other's toes when using different dates for unit testing their functionality.

This is, of course, assuming that only the UI side is using CurrDate() in its conditions and queries for date based functionality. If the Server functionality is also affected by today's date then server side developers will need their own server based variable and function to test their side.

In the App I'm working on, only the UI screens behave differently based on current date so the solution I gave above works well for me to unit test.

2024-07-05 14-16-55
Daniël Kuhlmann
Ā 
MVP

You can unit test server actions also. My point is that the solution only covers client side, and not server side. But I am ok to disagree ;)

2021-09-06 15-09-53
Dorine Boudry
Ā 
MVP

Hi Sanjay,

If I need this type of time travelling for testing purposes, I wrap the CurrDate() function into my own MyCurrDate() function, and don't use the CurrDate() directly.

This MyCurrDate() function can do something like looking at some setting, and give back a date based on that, or if nothing is set, ultimately by default give back the real corrDate().   You could also incorporate in that logic a safeguard for not using it in production, so there is no possibility for people to 'cook the books' by faking the timestamp of when something happened.

I think that is what you mean with 'my custom function', right ?

For the usage of CurrDate() you already have in, so you can replace them with that custom function, that will probably have to be done on a 'per module' base, by using the 'search in this module'.

Dorine

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.