MPDX-6916: Replace Moment - #14
Conversation
|
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/cru/mpdx-react/DJaxDwFBkPCvXLty5BSJtJ2wm18q |
| Settings.resetCaches(); | ||
| }); | ||
|
|
||
| it('default', async () => { |
There was a problem hiding this comment.
This test is failing. For the WeeklyActivityTableCellDateRange, the expected value should be "Jan 26 - Feb 1", but it is now "Jan 27 - Feb 2". Unless Luxon defines a week as being Monday to Sunday, rather than Sunday to Saturday, I'm not sure why this is happening.
There was a problem hiding this comment.
Actually I just found out, that's exactly what's happening: moment/luxon@6b145bf. Would it make more sense to update the test to the new "start of week" and "end of week" values, or make WeeklyActivity offset the values coming from Luxon?
There was a problem hiding this comment.
Hmm ya the library just always returns Monday as the first day of the week. We might need to figure this out ourselves somehow. moment/luxon#373 (comment) has some code in a Gist that would work but that's annoying...
It seems weird displaying Monday as the first day of the week. But then again it probably seems weird for someone in another locale that Sunday is the first day of the week. But maybe we could consider these events as starting over every work week. Idk how we clearly show that to users as it's just a filter on the dashboard... Maybe a rolling 7 day window would make sense but idk if we can make that decision...
For explicitly setting Sunday to make US users happy, either of these could work:
OzzieOrca
left a comment
There was a problem hiding this comment.
Code looks good :) Thanks for figuring this out!
I probably didn't make this clear enough in the ticket but Tatai was migrating to date-fns as he went along I think. I like Luxon but haven't really used date-fns. Sounds like you want to move forward with Luxon too. So we probably need to migrate from date-fns to Luxon too. Having 2 date libraries doesn't seem great. At this point it's probably better to do that second migration in another PR since this one is looking good and to keep thing cleaner.
| node_modules | ||
| package-lock.json |
There was a problem hiding this comment.
How did you get files here? Yarn shouldn't make them... Kinda looks like you ran npm.
There was a problem hiding this comment.
Yeah I ran yarn but I think there was something that wasn't installing properly until I ran npm. I can give it a try again to avoid npm altogether.
There was a problem hiding this comment.
Oh I ran into this too initially. Did you set your yarn version locally to be Yarn 2? I followed the documentation to do that: https://yarnpkg.com/getting-started/install. The main command you're looking for is yarn set version berry but I had to upgrade to the newest version first in order to use yarn set.
There was a problem hiding this comment.
Ya maybe you need a Yarn upgrade. I'm running 2.4. Yarn still uses the 1.x version even if you have 2.4 installed unless the project has v2 configured. https://github.com/CruGlobal/mpdx-react/blob/main/.yarnrc.yml#L5 should set that but maybe I have something configured wrong... As long as you have a current yarn version it should just work... Let me know if it doesn't.
| describe('MockDate', () => { | ||
| beforeEach(() => { | ||
| MockDate.set(new Date(2020, 1, 1)); | ||
| Settings.now = () => new Date(2020, 1, 1).valueOf(); |
There was a problem hiding this comment.
Ooh this is nice to have included. Mocking dates has always been finicky.
| }); | ||
| afterEach(() => { | ||
| MockDate.reset(); | ||
| Settings.resetCaches(); |
There was a problem hiding this comment.
Should we do this in a global afterEach? Actually we could setup the mock date globally too...
There was a problem hiding this comment.
Yeah we can probably do that. Where can I setup things to run globally on all jest tests?
| const todayDate = DateTime.local().toISODate(); | ||
| const date = DateTime.fromISO(contact.lateAt).toISODate(); |
There was a problem hiding this comment.
Are these variables unused?
There was a problem hiding this comment.
I guess not! I might have left that there after trying different solutions.
| Settings.resetCaches(); | ||
| }); | ||
|
|
||
| it('default', async () => { |
There was a problem hiding this comment.
Hmm ya the library just always returns Monday as the first day of the week. We might need to figure this out ourselves somehow. moment/luxon#373 (comment) has some code in a Gist that would work but that's annoying...
It seems weird displaying Monday as the first day of the week. But then again it probably seems weird for someone in another locale that Sunday is the first day of the week. But maybe we could consider these events as starting over every work week. Idk how we clearly show that to users as it's just a filter on the dashboard... Maybe a rolling 7 day window would make sense but idk if we can make that decision...
For explicitly setting Sunday to make US users happy, either of these could work:
| const [startOfWeek, setStartOfWeek] = useState(DateTime.local().startOf('week')); | ||
| const [endOfWeek, setEndOfWeek] = useState(DateTime.local().endOf('week')); |
There was a problem hiding this comment.
An Interval transformed with mapEndpoints could work here. Seems a little cleaner since it's one state variable but maybe slightly more complicated.
|
@OzzieOrca Yeah I think the ticket was clear enough, I think in the end Luxon looks nicer to me than date-fns. I also looked into DayJS since it seems like Moment with a smaller bundle size, but I think I like Luxon's API a little better. I can create another PR after this for ripping date-fns out as well. |
OzzieOrca
left a comment
There was a problem hiding this comment.
Looks good :) Couple of trivial things.
| global.beforeEach(() => { | ||
| Settings.now = () => new Date(2020, 1, 1).valueOf(); | ||
| }); | ||
|
|
||
| global.afterEach(() => { |
There was a problem hiding this comment.
I think global is just the node global object variable (like window is in a browser). I don't think you need it as referencing a global variable directly should work. But if there are type errors or something, you can keep it.
| global.beforeEach(() => { | |
| Settings.now = () => new Date(2020, 1, 1).valueOf(); | |
| }); | |
| global.afterEach(() => { | |
| beforeEach(() => { | |
| Settings.now = () => new Date(2020, 1, 1).valueOf(); | |
| }); | |
| afterEach(() => { |
| @@ -1,5 +1,5 @@ | |||
| import React from 'react'; | |||
| import MockDate from 'mockdate'; | |||
| import { Settings } from 'luxon'; | |||
There was a problem hiding this comment.
I think this is unused and is throwing a lint error.
I decided to go with Luxon as a replacement to MomentJS: https://moment.github.io/luxon/index.html