Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a testable version of ago() and re-enable the disabled test #4364

Merged

Conversation

janaagaard75
Copy link
Contributor

@janaagaard75 janaagaard75 commented Jun 4, 2024

The current tests for ago() function are not deterministic, so they are currently disabled.

Create a testable version of ago(), dateDiff(), that has two parameter, allowing for deterministic tests. ago() is then implemented as a special case of dateDiff(), where one of the dates is new Date().

The implementation is changed so that months are now considered to a fixed 30 days instead of the current 30.41675 days. Using 30 days is arguably less precise, but it is a lot easier to reason about because the cut off from month to month is always a full day. I think this simpler solution was preferable. The root cause of the mess is that "a month ago" means a different number of days depending the current month.

This follows up on #4295.

This puts the input and the expected values next to each other.
This is a copy of ago(), but with the ability to specify the second date instead of using Date.now().
This makes it possible to test the dates without being forced to rely on what the current date is.

The commented out tests do not yet pass. This is fixed in later commits.
@janaagaard75 janaagaard75 changed the title Testable version of ago() Create a testable version of ago() and re-enable the disabled test Jun 11, 2024
@estrattonbailey estrattonbailey changed the base branch from main to date-utils June 17, 2024 22:34
Copy link
Member

@estrattonbailey estrattonbailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much! Really appreciate this.

I'm going to merge into another branch to make some edits to the tests and add a formatting option we've been kicking around. Will get this integrated soon.

@estrattonbailey estrattonbailey merged commit 69a9b2e into bluesky-social:date-utils Jun 17, 2024
@janaagaard75 janaagaard75 deleted the testable-version-of-ago branch June 18, 2024 11:22
estrattonbailey added a commit that referenced this pull request Jun 18, 2024
* Create a testable version of ago() and re-enable the disabled test (#4364)

* Enable the test of ago()

* Use test cases

This puts the input and the expected values next to each other.

* Create dateDiff function

This is a copy of ago(), but with the ability to specify the second date instead of using Date.now().

* Let ago() use dateDiff()

* Move constants close to usage

* Test dateDiff instead of ago

This makes it possible to test the dates without being forced to rely on what the current date is.

The commented out tests do not yet pass. This is fixed in later commits.

* Update dateDiff and enable the remaining tests

* Split up tests, use date-fns as helpers

* Remove old test

* Add long format

* Add hook

* Migrate to hooks

* Delete old code

* Or equal to

* Update comment

---------

Co-authored-by: Jan Aagaard <jan@aagaard.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants