attr2_options_spec.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* global google */
  2. describe('Attr2Options', function() {
  3. var scope, parser;
  4. // load the tabs code
  5. beforeEach(function() {
  6. module('ngMap');
  7. inject(function($rootScope, $injector) {
  8. scope = $rootScope;
  9. scope.google = { maps: {
  10. Marker: function() {},
  11. MapTypeId: {HYBRID:'hybrid'}
  12. }
  13. };
  14. scope.$apply();
  15. parser = $injector.get('Attr2Options');
  16. });
  17. });
  18. describe("#filter", function() {
  19. it('should filter all angularjs methods', function() {
  20. var attrs ={a:1, $a:1, $$a:1};
  21. expect(parser.filter(attrs).a).toEqual(1);
  22. expect(parser.filter(attrs).$a).toEqual(undefined);
  23. expect(parser.filter(attrs).$$a).toEqual(undefined);
  24. });
  25. });
  26. describe("#getOptions", function() {
  27. it('should filter out ControlOptions', function() {
  28. var attrs ={a:1, aControlOptions:1};
  29. expect(parser.getOptions(attrs).aControlOptions).toEqual(undefined);
  30. });
  31. it('should filter out events', function() {
  32. var attrs ={a:1, onClick:'func'};
  33. expect(parser.getOptions(attrs).onClick).toEqual(undefined);
  34. });
  35. it('should convert string to number', function() {
  36. var attrs ={a:'100.99'};
  37. expect(parser.getOptions(attrs).a).toEqual(100.99);
  38. });
  39. it('should convert JSON to an object', function() {
  40. var attrs = {a:'{"foo":123}'};
  41. expect(parser.getOptions(attrs).a.foo).toEqual(123);
  42. });
  43. it('should convert object-like JSON string to an object', function() {
  44. var attrs = {a:"{ hello: 'world',foo:1, bar : '2', foo1: 1, _bar : 2, $2: 3,"+
  45. " 'xxx': 5, \"fuz\": 4, places: ['Africa', 'America', 'Asia', 'Australia'] }"
  46. };
  47. expect(parser.getOptions(attrs).a.hello).toEqual("world");
  48. });
  49. it('should convert Class name to google object', function() {
  50. var attrs = {a:'Marker()'};
  51. expect(typeof parser.getOptions(attrs, scope).a).toEqual('object');
  52. });
  53. it('should convert constant to google constant', function() {
  54. var attrs = {a:'MapTypeId.HYBRID'};
  55. expect(parser.getOptions(attrs, scope).a).toEqual(google.maps.MapTypeId.HYBRID);
  56. attrs = {MapTypeId:'HYBRID'};
  57. expect(parser.getOptions(attrs, scope).MapTypeId).toEqual(google.maps.MapTypeId.HYBRID);
  58. });
  59. });
  60. describe("#getControlOptions", function() {
  61. it('should filter out non control options', function() {
  62. var attrs ={a:1};
  63. expect(parser.getControlOptions(attrs).a).toEqual(undefined);
  64. });
  65. it('should accept object notation, i.e {foo:1}', function() {
  66. var attrs ={aControlOptions: '{foo:1}'};
  67. expect(parser.getControlOptions(attrs).aControlOptions.foo).toEqual(1);
  68. });
  69. it('should convert string to uppercase. i.e {"a":"foo"}', function() {
  70. var attrs ={aControlOptions: '{"foo":"bar"}'};
  71. expect(parser.getControlOptions(attrs).aControlOptions.foo).toEqual("BAR");
  72. });
  73. it('should convert mapTypeIds to google MapTypeIds', function() {
  74. var attrs ={aControlOptions: '{"mapTypeIds":["HYBRID"]}'};
  75. expect(parser.getControlOptions(attrs).aControlOptions.mapTypeIds).toEqual(["hybrid"]);
  76. });
  77. it('should convert style to matching google ones, i.e. ZoomControlStyle', function() {
  78. var attrs ={zoomControlOptions: '{"style":"SMALL"}'};
  79. expect(parser.getControlOptions(attrs).zoomControlOptions.style).toEqual(1);
  80. });
  81. it('should convert position to matching google ones, i.e. google.maps.ControlPosition', function() {
  82. var attrs ={zoomControlOptions: '{"position":"TOP_LEFT"}'};
  83. expect(parser.getControlOptions(attrs).zoomControlOptions.position).toEqual(1);
  84. });
  85. });
  86. describe("#getEvents", function() {
  87. it('should filter out non events', function() {
  88. var attrs ={a:1};
  89. expect(parser.getEvents(scope, attrs).a).toEqual(undefined);
  90. });
  91. it('should set scope function as events', function() {
  92. scope.scopeFunc = function() {}
  93. var attrs ={onClick:'scopeFunc()'};
  94. var events = parser.getEvents(scope, attrs);
  95. expect(typeof events.click).toEqual('function');
  96. });
  97. });
  98. describe("#getAttrsToObserve", function() {
  99. it('should return no attributes to observe with ng-repeat', function() {
  100. var attrs ={a:"1", b:"{{foo}}", 'ng-repeat': "bar"};
  101. expect([]).toEqual(parser.getAttrsToObserve(attrs));
  102. });
  103. it('should return attributes to observe', function() {
  104. var attrs ={a:"1", b:"{{foo}}", c:"{{bar}}"};
  105. expect(['b', 'c']).toEqual(parser.getAttrsToObserve(attrs));
  106. });
  107. });
  108. describe("#camelCase", function() {
  109. it('should return camelcase', function() {
  110. expect("thisIsCamelCase").toEqual(parser.camelCase("this:is_camel-case"));
  111. expect("MozCamelCase").toEqual(parser.camelCase("moz-camel-case"));
  112. });
  113. });
  114. describe("#observeAndSet", function() {
  115. //TODO
  116. // var observeAndSet = function(attrs, attrName, object) {
  117. // check object[setMethod] is called when attribute value is changed
  118. });
  119. describe("#observeAttrSetSet", function() {
  120. //TODO
  121. // var observeAttrSetObj = function(orgAttrs, attrs, obj) {}
  122. // check if observeAndSet is called
  123. });
  124. describe("#orgAttributes", function() {
  125. //TODO
  126. //var orgAttributes = function(el) { return orgAttributes }
  127. });
  128. describe("#setDelayedGeoLocation", function() {
  129. //TODO: need some mock jobs for object, NavigatorGeolocation and GeoCoder
  130. });
  131. describe("#observeAndSet", function() {
  132. //TODO: needs some mock jobs for object and attrs
  133. });
  134. });