Browse Source

fix: avoid setter conflict (#133)

main
Justin Poehnelt 4 years ago committed by GitHub
parent
commit
e27cd9ad5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 9
      src/marker.test.ts
  2. 12
      src/marker.ts
  3. 10
      src/util.ts

9
src/marker.test.ts

@ -45,6 +45,15 @@ beforeEach(() => { @@ -45,6 +45,15 @@ beforeEach(() => {
google.maps.event.addDomListener = jest.fn();
});
test("init should not pass extended options", () => {
const marker = new MarkerWithLabel({
labelContent: "foo",
labelClass: "bar",
clickable: true,
});
expect(google.maps.Marker).toBeCalledWith({ clickable: true });
});
test("should have listeners after multiple calls to setMap", () => {
const map = (jest.fn() as any) as google.maps.Map;
(google.maps.Marker.prototype.getMap as jest.Mock).mockImplementation(() => {

12
src/marker.ts

@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
* limitations under the License.
*/
import { abortEvent, stopPropagation } from "./util";
import { abortEvent, omit, stopPropagation } from "./util";
import { Label } from "./label";
import { MarkerSafe } from "./marker-safe";
@ -49,7 +49,15 @@ export class MarkerWithLabel extends MarkerSafe { @@ -49,7 +49,15 @@ export class MarkerWithLabel extends MarkerSafe {
private mouseOutTimeout: ReturnType<typeof setTimeout>;
constructor(options: MarkerWithLabelOptions) {
super({ ...options });
// need to omit extended options to Marker class that collide with setters/getters
super(
omit(options, [
"labelAnchor",
"labelZIndexOffset",
"labelClass",
"labelContent",
])
);
this.label = new Label({ ...{}, ...options });
this.propertyListeners = [

10
src/util.ts

@ -51,3 +51,13 @@ export function stopPropagation(e: Event | null) { @@ -51,3 +51,13 @@ export function stopPropagation(e: Event | null) {
e.cancelBubble = true;
}
}
export function omit(
o: { [key: string]: any },
keys: string[]
): { [key: string]: any } {
const x = { ...o };
keys.forEach((k) => delete x[k]);
return x;
}

Loading…
Cancel
Save