navigator_geolocation_spec.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* global google, jasmine */
  2. describe('NavigatorGeolocation', function () {
  3. var scope, navGeo;
  4. //don't do this, it will interfere others, i.e navigator.userAgent
  5. //navigator = jasmine.createSpy('navigator');
  6. navigator.geolocation = jasmine.createSpy('geolocation');
  7. beforeEach(module('ngMap'));
  8. beforeEach(inject(function ($rootScope, NavigatorGeolocation) {
  9. scope = $rootScope, navGeo = NavigatorGeolocation;
  10. }));
  11. describe('getCurrentPosition function', function () {
  12. beforeEach(function() {
  13. var GoodResponse = function (successCallback, errorCallback) { successCallback('GOOD'); };
  14. navigator.geolocation.getCurrentPosition = jasmine.createSpy().andCallFake(GoodResponse);
  15. });
  16. it('Should return a promise', function () {
  17. var promise = navGeo.getCurrentPosition();
  18. expect(typeof promise.then).toBe('function');
  19. });
  20. it('Should call getCurrentPosition to retrieve good results', function () {
  21. var jasmineSuccess = jasmine.createSpy('success');
  22. var jasmineError = jasmine.createSpy('error');
  23. navGeo.getCurrentPosition().then(jasmineSuccess, jasmineError);
  24. scope.$apply();
  25. expect(jasmineSuccess).toHaveBeenCalled();
  26. expect(jasmineError).not.toHaveBeenCalled();
  27. expect(jasmineSuccess).toHaveBeenCalledWith('GOOD');
  28. });
  29. });
  30. describe('getCurrentPosition function', function () {
  31. beforeEach(function() {
  32. var BadResponse = function (successCallback, errorCallback) { errorCallback('BAD'); };
  33. navigator.geolocation.getCurrentPosition = jasmine.createSpy().andCallFake(BadResponse);
  34. });
  35. it('Should call getCurrentPosition to retrieve bad results', function () {
  36. var jasmineSuccess = jasmine.createSpy('success');
  37. var jasmineError = jasmine.createSpy('error');
  38. navGeo.getCurrentPosition().then(jasmineSuccess, jasmineError);
  39. scope.$apply();
  40. expect(jasmineSuccess).not.toHaveBeenCalled();
  41. expect(jasmineError).toHaveBeenCalled();
  42. expect(jasmineSuccess).not.toHaveBeenCalled();
  43. });
  44. });
  45. });