0

I am converting JavaScript date objects to local time, then formatting using a special format.

I would like to create a test to confirm that the conversion is correct but depending on the location of the build server, the local time varies and so the generated date string varies.

Is there a best practice for testing these things?

Victor Grazi
  • 15,563
  • 14
  • 61
  • 94
  • What have you tried so far? Typically this is where I have used spyOn and returnValue to mock the data I need for testing. –  Dec 01 '16 at 18:05
  • I was hoping to force JavaScript to use an injected timezone. Not sure if there is any way to do that – Victor Grazi Dec 01 '16 at 19:00
  • it would be useful if you posted what you have tried so far. –  Dec 02 '16 at 15:21

1 Answers1

0
  • Though this question is relatively incomplete, I'd like to take a stab at it. Please reach my fiddle here
  • From the question and the comments, here is what I assumed and my solution is based on this assumption.

Assumptions:

  1. You are trying to inject a timezone since you believe the server actually responds with a specific timezone.
  2. Assuming 1 is true, I've written a couple tests creating a dummy date: each with a specific timezone.
  3. Though there is no clarity about what methods you'd like to test, I'm testing a couple viz. toLocaleDateString & getUTCDate
  4. There was no information about where the user might be from, so I'd imaged he is from US(EST) and then mocked the methods accordingly.

  5. Also, please note that though I've centered my tests around the US user, he could theoretically be anywhere, therefore I've used a spy & return so that my tests don't fail regardless of where you run the tests from.

  6. But I've not used a spy on the UTC methods since they are UTC regardless where you call it from.

    /** I've run these tests from US(EST) **/
    /** Testing from the observers location which is US **/
    describe('js date testing', function() {
      it('build server from UK', function() {
        var ukDate = new Date("Mar 25 2015 01:56:24 UTC+0000");
        // If I print ukDate as is- I see Tue Mar 24 2015 20:56:24 GMT-0400 (Eastern Daylight Time)
        // But lets mock it since we would like the tests to pass as I assume the user is in US.
        spyOn(ukDate, 'toLocaleDateString').and.returnValue("3/24/2015");
        var val = ukDate.toLocaleDateString();
        expect(val).toEqual("3/24/2015");
        expect(ukDate.getUTCDate()).toEqual(25);
      });
    
      it('build server from another planet that is 22 hours east of UK', function() {
        var aliensWatch = new Date("Mar 25 2015 01:56:24 UTC+2200");
        // If I print aliensWatch as is- I see Mon Mar 23 2015 23:56:24 GMT-0400 (Eastern Daylight Time)
        // But lets mock it since we would like the tests to pass as I assume the user is in US.
        spyOn(aliensWatch, 'toLocaleDateString').and.returnValue("3/23/2015");
        var val = aliensWatch.toLocaleDateString();
        expect(val).toEqual("3/23/2015");
        expect(aliensWatch.getUTCDate()).toEqual(24);
      });
    
    });
    

Additional sources

Community
  • 1
  • 1
Winter Soldier
  • 2,607
  • 3
  • 14
  • 18