Skip to content

Commit

Permalink
fix(link): remove unnecessary navigation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Erbil Nas committed Dec 25, 2024
1 parent 412f661 commit a6a08cc
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 145 deletions.
114 changes: 0 additions & 114 deletions src/components/link/bl-link.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,120 +61,6 @@ describe("bl-link", () => {
expect(el.shadowRoot!.querySelector("bl-icon")?.getAttribute("name")).to.equal("external_link");
});

it("handles navigation and events correctly", async () => {
const el = await fixture<BlLink>(html`<bl-link target="javascript:void(0)">Home</bl-link>`);
const windowOpenStub = sinon.stub(window, "open");

// Test internal navigation
const internalClickEvent = new MouseEvent("click", { bubbles: true });
const internalClickSpy = sinon.spy(internalClickEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(internalClickEvent);
expect(internalClickSpy.called).to.be.true;
expect(windowOpenStub.calledWith("javascript:void(0)", "_self", "")).to.be.true;

// Test external navigation
windowOpenStub.resetHistory();
el.external = true;
el.target = "https://example.com";
await el.updateComplete;
const externalClickEvent = new MouseEvent("click", { bubbles: true });
const externalClickSpy = sinon.spy(externalClickEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(externalClickEvent);
expect(externalClickSpy.called).to.be.true;
expect(windowOpenStub.calledWith("https://example.com", "_blank", "noopener,noreferrer")).to.be.true;

// Test disabled state
windowOpenStub.resetHistory();
el.disabled = true;
await el.updateComplete;
const disabledClickEvent = new MouseEvent("click", { bubbles: true });
const disabledClickSpy = sinon.spy(disabledClickEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(disabledClickEvent);
expect(disabledClickSpy.called).to.be.true;
expect(windowOpenStub.notCalled).to.be.true;

// Test keyboard navigation
el.disabled = false;
await el.updateComplete;
const enterKeyEvent = new KeyboardEvent("keydown", { key: "Enter", bubbles: true });
const enterKeySpy = sinon.spy(enterKeyEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(enterKeyEvent);
expect(enterKeySpy.called).to.be.true;
expect(windowOpenStub.calledWith("https://example.com", "_blank", "noopener,noreferrer")).to.be.true;

// Test keyboard navigation when disabled
windowOpenStub.resetHistory();
el.disabled = true;
await el.updateComplete;
const disabledEnterKeyEvent = new KeyboardEvent("keydown", { key: "Enter", bubbles: true });
const disabledEnterKeySpy = sinon.spy(disabledEnterKeyEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(disabledEnterKeyEvent);
expect(disabledEnterKeySpy.notCalled).to.be.true;
expect(windowOpenStub.notCalled).to.be.true;

// Test keyboard navigation with non-Enter key
el.disabled = false;
await el.updateComplete;
const spaceKeyEvent = new KeyboardEvent("keydown", { key: "Space", bubbles: true });
const spaceKeySpy = sinon.spy(spaceKeyEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(spaceKeyEvent);
expect(spaceKeySpy.notCalled).to.be.true;
expect(windowOpenStub.notCalled).to.be.true;

// Test navigation without target
el.target = "";
await el.updateComplete;
const noTargetClickEvent = new MouseEvent("click", { bubbles: true });
const noTargetClickSpy = sinon.spy(noTargetClickEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(noTargetClickEvent);
expect(noTargetClickSpy.called).to.be.true;
expect(windowOpenStub.notCalled).to.be.true;

// Test navigation with disabled and no target
el.disabled = true;
el.target = "";
await el.updateComplete;
const disabledNoTargetClickEvent = new MouseEvent("click", { bubbles: true });
const disabledNoTargetClickSpy = sinon.spy(disabledNoTargetClickEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(disabledNoTargetClickEvent);
expect(disabledNoTargetClickSpy.called).to.be.true;
expect(windowOpenStub.notCalled).to.be.true;

// Test navigation with disabled and target
el.disabled = true;
el.target = "https://example.com";
await el.updateComplete;
const disabledWithTargetClickEvent = new MouseEvent("click", { bubbles: true });
const disabledWithTargetClickSpy = sinon.spy(disabledWithTargetClickEvent, "preventDefault");

el.shadowRoot!.querySelector("a")?.dispatchEvent(disabledWithTargetClickEvent);
expect(disabledWithTargetClickSpy.called).to.be.true;
expect(windowOpenStub.notCalled).to.be.true;

// Test direct navigation call with disabled and target
el.disabled = true;
el.target = "https://example.com";
(el as unknown as { navigate: () => void }).navigate();
expect(windowOpenStub.notCalled).to.be.true;

// Test direct navigation call with enabled and target
el.disabled = false;
el.target = "https://example.com";
(el as unknown as { navigate: () => void }).navigate();
expect(windowOpenStub.calledWith("https://example.com", "_blank", "noopener,noreferrer")).to.be.true;

// Cleanup
windowOpenStub.restore();
});

it("handles inline variant warning", async () => {
const consoleWarnSpy = sinon.spy(console, "warn");

Expand Down
31 changes: 0 additions & 31 deletions src/components/link/bl-link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,35 +79,6 @@ export default class BlLink extends LitElement {
return null;
}

private handleKeyDown(event: KeyboardEvent) {
if (this.disabled) return;

if (event.key === "Enter") {
event.preventDefault();
this.navigate();
}
}

private handleClick(event: Event) {
if (this.disabled || !this.target) {
event.preventDefault();
return;
}

event.preventDefault();
this.navigate();
}

private navigate() {
if (this.disabled || !this.target) return;

window.open(
this.target,
this.external ? "_blank" : "_self",
this.external ? "noopener,noreferrer" : ""
);
}

connectedCallback() {
super.connectedCallback();

Expand Down Expand Up @@ -149,8 +120,6 @@ export default class BlLink extends LitElement {
aria-label="${ifDefined(this.ariaLabel || undefined)}"
aria-disabled="${this.disabled}"
tabindex="${this.disabled ? "-1" : "0"}"
@click="${this.handleClick}"
@keydown="${this.handleKeyDown}"
>
${content}
${this.external ? html`<span class="visually-hidden">(opens in new tab)</span>` : null}
Expand Down

0 comments on commit a6a08cc

Please sign in to comment.