Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(ngMock): httpBackend match data with dates (1.2.2 regression) #5127

Closed
wants to merge 1 commit into from
Closed

fix(ngMock): httpBackend match data with dates (1.2.2 regression) #5127

wants to merge 1 commit into from

Conversation

bcaudan
Copy link
Contributor

@bcaudan bcaudan commented Nov 25, 2013

Expecting a request with a data object containing a date fails.

Use serialization/deserialization of data to have the same date format as the expectation.

@mary-poppins
Copy link

Thanks for the PR!

  • Contributor signed CLA now or in the past
    • If you just signed, leave a comment here with your real name
  • PR's commit messages follow the commit message format

If you need to make changes to your pull request, you can update the commit with git commit --amend.
Then, update the pull request with git push -f.

Thanks again for your help!

@bcaudan
Copy link
Contributor Author

bcaudan commented Nov 25, 2013

signed CLA as Bastien Caudan

@petebacondarwin
Copy link
Contributor

LGTM

    Expecting a request with a data object containing a date was failing.
@bcaudan
Copy link
Contributor Author

bcaudan commented Feb 23, 2014

The JSON serialization/deserialization of data could be a bit a confusing:

if (data && !angular.isString(data))
  return angular.equals(angular.fromJson(angular.toJson(data)), angular.fromJson(d));

So, I tried to work on another implementation traversing the parsed JSON object to convert date strings into date objects:

if (data && !angular.isString(data))
      return angular.equals(data, withJsDates(angular.fromJson(d)));

function withJsDates(d) {
    if (d === null) return d;
    angular.forEach(d, function (value, key) {
      if (angular.isObject(value)) {
        d[key] = withJsDates(value);
      } else if (angular.isString(value)) {
        var timestamp = Date.parse(value);
        if (!isNaN(timestamp)) {
          d[key] = new Date(timestamp);
        }
      }
    });
    return d;
  }

This implementation is faster and seems more readable to me but unfortunately does not work on IE8...
Two IE8 issues are involved:

  • Date.parse implementation is not standard
  • angular compares date by timestamp and Date.toJSON loses milliseconds...

Maybe someone would have other thoughts on this issue.

@mathieubigorne
Copy link

I need it too.
What prevents it from being merged ?

@bcaudan
Copy link
Contributor Author

bcaudan commented Oct 20, 2014

It is still not working on angular 1.3.0, plunker :(

@petebacondarwin
Copy link
Contributor

This is mocking code. I think we should go with your initial idea.

petebacondarwin pushed a commit that referenced this pull request Oct 20, 2014
…orrectly

If a response or expectation contained a date object then `$httpBackend.expect`
was not matching correctly.

This commit encodes then decodes the object being matched to ensure consistency.

Closes #5127
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants