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

chore: if target is not <a> element, don't handle onClick event #53

Merged
merged 2 commits into from
Nov 22, 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
12 changes: 12 additions & 0 deletions .changeset/cold-swans-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@crowdstrike/foundry-js': minor
---

Improve how we handle onClick for navigation. Now when adding event listener to click event:

```javascript
document.querySelector('[data-internal-links]')
.addEventListener('click', (event) => falcon.navigation.onClick(event, '_self', 'internal'));
```

we'll call preventDefault correctly and won't throw error in the console.
6 changes: 3 additions & 3 deletions src/lib/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ export class Navigation<DATA extends LocalData = LocalData> {
return;
}

event.preventDefault();

if (!(event.target instanceof HTMLAnchorElement)) {
throw Error(`event target is not an anchor element, ${event.target}`);
return;
}

event.preventDefault();

const path = event.target.getAttribute('href');
defaultTarget =
(event.target.getAttribute('target') as '_self' | '_blank') ??
Expand Down
47 changes: 43 additions & 4 deletions tests/lib/navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ test('onClick fails if MouseEvent not passed', async () => {
});

test('onClick fails target element was not an anchor element', async () => {
const spy = vi.spyOn(window.parent, 'postMessage');

const mouseEventClass = new MouseEvent('click');

// resetting the types to see what happens when types are ignored and wrong event is passed
await expect(async () =>
navigation.onClick(mouseEventClass),
).rejects.toThrowError(/event target is not an anchor element/);
navigation.onClick(mouseEventClass);

expect(spy).not.toHaveBeenCalledOnce();
});

test('onClick fails with anchor element missing href attribute', async () => {
Expand Down Expand Up @@ -122,3 +123,41 @@ test('onClick triggers navigateTo message', async () => {
}),
);
});

test('onClick triggers navigateTo message with query params for internal navigation', async () => {
const spy = vi.spyOn(window.parent, 'postMessage');

const anchorElement = document.createElement('a');

anchorElement.href = '/?param1=value1';

class SyntheticMouseEvent extends MouseEvent {
target = anchorElement;
}

const mouseEventClass = new SyntheticMouseEvent('click');

navigation.onClick(mouseEventClass);

expect(spy).toHaveBeenCalledOnce();

expect(spy.mock.lastCall?.[0]).toEqual(
expect.objectContaining({
message: {
type: 'navigateTo',
payload: {
path: '/?param1=value1',
target: '_self',
metaKey: false,
ctrlKey: false,
shiftKey: false,
type: 'falcon',
},
},
meta: {
version: 'current',
messageId: expect.stringMatching(uuidV4Regex),
},
}),
);
});