Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remap click to gmp-click to avoid error-messages #21

Merged
merged 2 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/__tests__/marker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,56 @@ describe('html attributes', () => {
});
});

describe('events', () => {
const position = [10, 53] as [number, number];

let map: google.maps.Map;
let marker: Marker;
let markerElement: google.maps.marker.AdvancedMarkerElement;
let addListenerSpy: jest.SpyInstance;
let addDomListenerSpy: jest.SpyInstance;

beforeEach(async () => {
initialize();

map = new google.maps.Map(document.createElement('div'));
marker = new Marker({position, map});

markerElement = mockInstances.get(AdvancedMarkerElement).at(0)!;
// eslint-disable-next-line @typescript-eslint/unbound-method
addListenerSpy = markerElement.addListener as jest.MockedFunction<
typeof markerElement.addListener
>;
addDomListenerSpy = jest
.spyOn(markerElement.element, 'addEventListener')
.mockImplementation(() => {});
});

test('events are added to the marker instance', () => {
const fn = jest.fn();

marker.addListener('drag', fn);

expect(addListenerSpy).toHaveBeenCalledWith('drag', fn);
});

test('click event is rerouted to gmp-click', () => {
const fn = jest.fn();

marker.addListener('click', fn);

expect(addListenerSpy).toHaveBeenCalledWith('gmp-click', fn);
});

test('dom-events are forwarded to the html-element', () => {
const fn = jest.fn();
marker.addListener('touchstart', fn);
expect(addDomListenerSpy).toHaveBeenCalledWith('touchstart', fn);
});

test.todo('dom-events can be removed');
});

describe('dynamic attributes', () => {
test.todo('dynamic attribute parameters');
test.todo('marker state');
Expand Down
5 changes: 4 additions & 1 deletion src/marker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,10 @@ export class Marker<TUserData = unknown> {
handler: ((ev: google.maps.MapMouseEvent) => void) | ((ev: Event) => void)
): google.maps.MapsEventListener {
if (eventName in MarkerEvents) {
return this.markerView_.addListener(eventName, handler);
return this.markerView_.addListener(
eventName === 'click' ? 'gmp-click' : eventName,
handler
);
}

const element = this.markerView_.element;
Expand Down