sourcegraph/client/shared/src/components/utils/linkClickHandler.test.tsx
GitStart-SourceGraph da154b5a93
[SG-33302] Upgrade to the latest react version and migrate to the new root API (#36045)
* feat: react v18 and new root api with some errors fixes

* web: repo page integration test flake fix (#37592)

* web: fix `blog-viewer` integration test flake (#38069)

* fix: build-ts

* fix: new failing unit tests

* fix: unit test

* web: fix search integration test

* feat: applied request changes

* fix: new failing test because of global mockReactVisibilitySensor

* fix: unexpected onLayoutChange call on SmartInsightsViewGrid

* fix: make ForwardReferenceComponent support custom children

* fix: more new ts lint issue

* fix: remaining issue BatchSpec

* fix: SmartInsightsViewGrid issue with useLayoutEffect

Co-authored-by: gitstart-sourcegraph <gitstart@users.noreply.github.com>
Co-authored-by: Valery Bugakov <skymk1@gmail.com>
2022-07-15 10:58:08 +07:00

65 lines
2.2 KiB
TypeScript

import assert from 'assert'
import { render } from '@testing-library/react'
import { createMemoryHistory } from 'history'
import * as sinon from 'sinon'
import { Link } from '@sourcegraph/wildcard'
import { createLinkClickHandler } from './linkClickHandler'
describe('createLinkClickHandler', () => {
it('handles clicks on links that stay inside the app', () => {
jsdom.reconfigure({ url: 'https://sourcegraph.test/some/where' })
const history = createMemoryHistory({ initialEntries: [] })
expect(history).toHaveLength(0)
const { container } = render(
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div onClick={createLinkClickHandler(history)}>
<Link to="https://sourcegraph.test/else/where">Test</Link>
</div>
)
const anchor = container.querySelector('a')
assert(anchor)
const spy = sinon.spy((_event: MouseEvent) => undefined)
window.addEventListener('click', spy)
anchor.click()
sinon.assert.calledOnce(spy)
expect(spy.args[0][0].defaultPrevented).toBe(true)
expect(history).toHaveLength(1)
expect(history.entries[0].pathname).toBe('/else/where')
})
it('ignores clicks on links that go outside the app', () => {
jsdom.reconfigure({ url: 'https://sourcegraph.test/some/where' })
const history = createMemoryHistory({ initialEntries: [] })
expect(history).toHaveLength(0)
const { container } = render(
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
<div onClick={createLinkClickHandler(history)}>
<Link to="https://github.com/some/where">Test</Link>
</div>
)
const anchor = container.querySelector('a')
assert(anchor)
const spy = sinon.spy((_event: MouseEvent) => undefined)
window.addEventListener('click', spy)
anchor.click()
sinon.assert.calledOnce(spy)
expect(spy.args[0][0].defaultPrevented).toBe(false)
expect(history).toHaveLength(0)
})
})