Something went wrong. Try again.
Synapse is a study workspace. Organize courses read and annotate the material, train with generated exercises and watch your progress forked from denishovart.com/synapse
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { interceptExternalLinks, openExternal } from './openExternal';
// isTauri() is false under jsdom, so these exercise the plain-browser paths:// openExternal opens a new tab, and the delegated listener routes absolute// http(s) anchors through it.
describe('openExternal (browser)', () => { afterEach(() => vi.restoreAllMocks());
it('opens a new noopener tab, lowercasing only the scheme', async () => { const open = vi.spyOn(window, 'open').mockReturnValue(null); await openExternal('HTTPS://Example.com/Path'); expect(open).toHaveBeenCalledWith('https://Example.com/Path', '_blank', 'noopener'); });});
describe('interceptExternalLinks', () => { beforeEach(() => { document.body.innerHTML = ''; }); afterEach(() => vi.restoreAllMocks());
it('intercepts a left click on an absolute http(s) anchor', () => { const open = vi.spyOn(window, 'open').mockReturnValue(null); interceptExternalLinks(); document.body.innerHTML = '<a href="https://site.test/x">link</a>';
const anchor = document.querySelector('a')!; const event = new MouseEvent('click', { bubbles: true, button: 0, cancelable: true }); anchor.dispatchEvent(event);
expect(event.defaultPrevented).toBe(true); expect(open).toHaveBeenCalledWith('https://site.test/x', '_blank', 'noopener'); });
it('ignores relative (in-app) links', () => { const open = vi.spyOn(window, 'open').mockReturnValue(null); interceptExternalLinks(); document.body.innerHTML = '<a href="/courses/1">route</a>';
document .querySelector('a')! .dispatchEvent(new MouseEvent('click', { bubbles: true, button: 0, cancelable: true })); expect(open).not.toHaveBeenCalled(); });
it('ignores non-primary buttons', () => { const open = vi.spyOn(window, 'open').mockReturnValue(null); interceptExternalLinks(); document.body.innerHTML = '<a href="https://site.test">link</a>';
document .querySelector('a')! .dispatchEvent(new MouseEvent('click', { bubbles: true, button: 1, cancelable: true })); expect(open).not.toHaveBeenCalled(); });
it('ignores a click that is not on an anchor', () => { const open = vi.spyOn(window, 'open').mockReturnValue(null); interceptExternalLinks(); document.body.innerHTML = '<button>nope</button>';
document .querySelector('button')! .dispatchEvent(new MouseEvent('click', { bubbles: true, button: 0, cancelable: true })); expect(open).not.toHaveBeenCalled(); });});