rwsdk
Version:
Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime
55 lines (54 loc) • 1.81 kB
JavaScript
import { describe, it, expect } from "vitest";
import { validateClickEvent } from "./navigation";
describe("clientNavigation", () => {
let mockEvent = {
button: 0, // right click
metaKey: false,
altKey: false,
shiftKey: false,
ctrlKey: false,
};
let mockTarget = {
closest: () => {
return {
getAttribute: () => "/test",
hasAttribute: () => false,
};
},
};
it("should return true", () => {
expect(validateClickEvent(mockEvent, mockTarget)).toBe(true);
});
it("should return false if the event is not a left click", () => {
expect(validateClickEvent({ ...mockEvent, button: 1 }, mockTarget)).toBe(false);
});
it("none of the modifier keys are pressed", () => {
expect(validateClickEvent({ ...mockEvent, metaKey: true }, mockTarget)).toBe(false);
});
it("the target is not an anchor tag", () => {
expect(validateClickEvent(mockEvent, {
closest: () => undefined,
})).toBe(false);
});
it("should have an href attribute", () => {
expect(validateClickEvent(mockEvent, {
closest: () => ({ getAttribute: () => undefined }),
})).toBe(false);
});
it("should not include an #hash", () => {
expect(validateClickEvent(mockEvent, {
closest: () => ({
getAttribute: () => "/test#hash",
hasAttribute: () => false,
}),
})).toBe(false);
});
it("should be a relative link", () => {
expect(validateClickEvent(mockEvent, {
closest: () => ({
getAttribute: () => "/test",
hasAttribute: () => false,
}),
})).toBe(true);
});
});