JavaScript Angular 5 unit test using new Date to check date angular unit test service,angular unit test click event,angular

I'm trying to test a service function in Angular where it receives a date and check if the date is a date in the future. If it is, it returns true.

// The 'check_date' will always be in the format `dd/mm/yyyy`
public checkDate(check_date: string): boolean {
    const today: any = new Date();
    const dateParts: any = check_date.split('/');
    const dateObject: any = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);

    if (dateObject.getTime() > today.getTime()) {
        return true;
    }

    return false;
}

How can I test this? Because if I do something like this:

it('should return true if date is in the future', () => {
    const date = '04/02/2018';
    const result = service.checkDate(date);
    expect(result).toBeTruthy();
});

Today it will pass, because new Date() will be 01/02/2018. But if I run this test next month, it will not pass.

I could set the date to be test to be way further in the future, like 01/01/3018. But I'd like to know if there is another method to test this case.

Answer:1

Date can be mocked to definitely test values it is supposed to return:

const UnmockedDate = Date;

spyOn(<any>window, 'Date').and.returnValues(
  new UnmockedDate('2018-01-01'),
  new UnmockedDate('2018-02-04')
);

const result = service.checkDate('04/02/2018');

expect(Date).toHaveBeenCalledTimes(2);

expect(Date.calls.all()[0].object instanceof UnmockedDate).toBe(true); // called with new
expect(Date.calls.argsFor(0)).toEqual([]);

expect(Date.calls.all()[1].object instanceof UnmockedDate).toBe(true);
expect(Date.calls.argsFor(1)).toEqual([...]);
...

Alternatively, Jasmine Clock API can be used to mock date:

jasmine.clock().install();
jasmine.clock().mockDate('2018-01-01');

const result = service.checkDate('04/02/2018');

...

jasmine.clock().uninstall(); // better be performed in afterEach

Since Date is not a spy, the test won't be as strict as the one where Date calls can be asserted.

Answer:2

Take a look at sinon fake timer:
http://sinonjs.org/releases/v4.2.2/fake-timers/

describe('Date.now dependent test', () => {
   let clock; 

   before(() => {
       clock = sinon.useFakeTimers({
          now: 1483228800000
       });
   });

   after(() => {
       clock.restore();
   });

   it('test', () => {
       const now = Date.now();
       //now should always be 1483228800000 here
   });
})

It is based on https://github.com/sinonjs/lolex, which overrides native Date is a special way.

Answer:3

I've some problems with my Edit function for editing my groups table. the fields are "group_id(primary key), cat_id(foreign key), group_name" I used the group_id as the identifier of which group ...

I've some problems with my Edit function for editing my groups table. the fields are "group_id(primary key), cat_id(foreign key), group_name" I used the group_id as the identifier of which group ...

I'm trying to use the jquery.data method to retrieve a bunch of item's IDs from a node server and save it for each item as an attr or data and use it later for updating the item. But the attribute ...

I'm trying to use the jquery.data method to retrieve a bunch of item's IDs from a node server and save it for each item as an attr or data and use it later for updating the item. But the attribute ...

  1. jquery data returning undefined
  2. jquery ajax return data
  3. jquery post not returning data
  4. jquery get not returning data
  5. jquery ajax not returning data
  6. jquery getjson not returning data

In Polymer 1.0+, how do you pass in a template from the light dom to be used in the dom-module? I'd like the template in the light dom to have bind variables, then the custom element use that template ...

In Polymer 1.0+, how do you pass in a template from the light dom to be used in the dom-module? I'd like the template in the light dom to have bind variables, then the custom element use that template ...

  1. polymer candy clay
  2. polymer cane
  3. polymer cane patterns
  4. polymer cane slicer
  5. polymer canes for sale
  6. polymer cane tutorial
  7. polymer candy clay liquid
  8. polymer candy clay amazon
  9. polymer candy clay for phone case
  10. polymer cane tutorials free
  11. polymer canada
  12. polymer canoe
  13. polymer cancer treatment
  14. polymer canada notes
  15. polymer canadian money
  16. polymer candy
  17. polymer canvas
  18. polymer cancer
  19. polymer cane extruder
  20. polymer candle filter

It is hard to explain, you can see a DEMO HERE I have a products table that dynamically creates/deletes new lines of products. I also have a totals table that totals up the totals of each line ...

It is hard to explain, you can see a DEMO HERE I have a products table that dynamically creates/deletes new lines of products. I also have a totals table that totals up the totals of each line ...