Get link specs passing

This commit is contained in:
Andrew Dupont 2024-01-06 19:40:26 -08:00
parent e71e56bdad
commit b46fe68ae2
2 changed files with 56 additions and 40 deletions

View File

@ -0,0 +1,9 @@
module.exports = {
env: { jasmine: true },
rules: {
"node/no-unpublished-require": "off",
"node/no-extraneous-require": "off",
"no-unused-vars": "off",
"no-empty": "off"
}
};

View File

@ -2,8 +2,8 @@ const { shell } = require('electron');
describe('link package', () => { describe('link package', () => {
beforeEach(async () => { beforeEach(async () => {
await atom.packages.activatePackage('language-gfm');
await atom.packages.activatePackage('language-hyperlink'); await atom.packages.activatePackage('language-hyperlink');
await atom.packages.activatePackage('language-gfm');
const activationPromise = atom.packages.activatePackage('link'); const activationPromise = atom.packages.activatePackage('link');
atom.commands.dispatch(atom.views.getView(atom.workspace), 'link:open'); atom.commands.dispatch(atom.views.getView(atom.workspace), 'link:open');
@ -15,7 +15,10 @@ describe('link package', () => {
await atom.workspace.open('sample.md'); await atom.workspace.open('sample.md');
const editor = atom.workspace.getActiveTextEditor(); const editor = atom.workspace.getActiveTextEditor();
editor.setText('// "http://github.com"'); let languageMode = editor.getBuffer().getLanguageMode();
await languageMode.ready;
editor.setText('// http://github.com ');
await languageMode.atTransactionEnd();
spyOn(shell, 'openExternal'); spyOn(shell, 'openExternal');
atom.commands.dispatch(atom.views.getView(editor), 'link:open'); atom.commands.dispatch(atom.views.getView(editor), 'link:open');
@ -35,76 +38,80 @@ describe('link package', () => {
expect(shell.openExternal.argsForCall[0][0]).toBe('http://github.com'); expect(shell.openExternal.argsForCall[0][0]).toBe('http://github.com');
shell.openExternal.reset(); shell.openExternal.reset();
editor.setCursorBufferPosition([0, 21]); editor.setCursorBufferPosition([0, 20]);
atom.commands.dispatch(atom.views.getView(editor), 'link:open'); atom.commands.dispatch(atom.views.getView(editor), 'link:open');
expect(shell.openExternal).toHaveBeenCalled(); expect(shell.openExternal).toHaveBeenCalled();
expect(shell.openExternal.argsForCall[0][0]).toBe('http://github.com'); expect(shell.openExternal.argsForCall[0][0]).toBe('http://github.com');
}); });
// only works in Atom >= 1.33.0 // NOTE: I don't think anyone realized that this was a feature. Our
// https://github.com/atom/link/pull/33#issuecomment-419643655 // `tree-sitter-markdown` parser doesn't recognize these as URLs. We'd need
const atomVersion = atom.getVersion().split('.'); // our custom `tree-sitter-hyperlink` to support this, and it doesn't right
console.error('atomVersion', atomVersion); // now, but I'll keep it in mind for the future.
if (+atomVersion[0] > 1 || +atomVersion[1] >= 33) { xit("opens an 'atom:' link", async () => {
it("opens an 'atom:' link", async () => { await atom.workspace.open('sample.md');
await atom.workspace.open('sample.md');
const editor = atom.workspace.getActiveTextEditor(); const editor = atom.workspace.getActiveTextEditor();
editor.setText( editor.setText(
'// "atom://core/open/file?filename=sample.js&line=1&column=2"' '// atom://core/open/file?filename=sample.js&line=1&column=2 '
); );
spyOn(shell, 'openExternal'); spyOn(shell, 'openExternal');
atom.commands.dispatch(atom.views.getView(editor), 'link:open'); atom.commands.dispatch(atom.views.getView(editor), 'link:open');
expect(shell.openExternal).not.toHaveBeenCalled(); expect(shell.openExternal).not.toHaveBeenCalled();
editor.setCursorBufferPosition([0, 4]); editor.setCursorBufferPosition([0, 4]);
atom.commands.dispatch(atom.views.getView(editor), 'link:open'); atom.commands.dispatch(atom.views.getView(editor), 'link:open');
expect(shell.openExternal).toHaveBeenCalled(); expect(shell.openExternal).toHaveBeenCalled();
expect(shell.openExternal.argsForCall[0][0]).toBe( expect(shell.openExternal.argsForCall[0][0]).toBe(
'atom://core/open/file?filename=sample.js&line=1&column=2' 'atom://core/open/file?filename=sample.js&line=1&column=2'
); );
shell.openExternal.reset(); shell.openExternal.reset();
editor.setCursorBufferPosition([0, 8]); editor.setCursorBufferPosition([0, 8]);
atom.commands.dispatch(atom.views.getView(editor), 'link:open'); atom.commands.dispatch(atom.views.getView(editor), 'link:open');
expect(shell.openExternal).toHaveBeenCalled(); expect(shell.openExternal).toHaveBeenCalled();
expect(shell.openExternal.argsForCall[0][0]).toBe( expect(shell.openExternal.argsForCall[0][0]).toBe(
'atom://core/open/file?filename=sample.js&line=1&column=2' 'atom://core/open/file?filename=sample.js&line=1&column=2'
); );
shell.openExternal.reset(); shell.openExternal.reset();
editor.setCursorBufferPosition([0, 60]); editor.setCursorBufferPosition([0, 59]);
atom.commands.dispatch(atom.views.getView(editor), 'link:open'); atom.commands.dispatch(atom.views.getView(editor), 'link:open');
expect(shell.openExternal).toHaveBeenCalled(); expect(shell.openExternal).toHaveBeenCalled();
expect(shell.openExternal.argsForCall[0][0]).toBe( expect(shell.openExternal.argsForCall[0][0]).toBe(
'atom://core/open/file?filename=sample.js&line=1&column=2' 'atom://core/open/file?filename=sample.js&line=1&column=2'
); );
}); });
}
describe('when the cursor is on a [name][url-name] style markdown link', () => describe('when the cursor is on a [name][url-name] style markdown link', () =>
it('opens the named url', async () => { it('opens the named url', async () => {
jasmine.useRealClock();
await atom.workspace.open('README.md'); await atom.workspace.open('README.md');
const editor = atom.workspace.getActiveTextEditor(); const editor = atom.workspace.getActiveTextEditor();
let languageMode = editor.getBuffer().getLanguageMode();
await languageMode.ready;
editor.setText(`\ editor.setText(`\
you should [click][here] you should [click][here]
you should not [click][her] you should not [click][her]
[here]: http://github.com\ [here]: http://github.com\
`); `);
// Allow for time for injections to populate
await languageMode.atTransactionEnd();
spyOn(shell, 'openExternal'); spyOn(shell, 'openExternal');
editor.setCursorBufferPosition([0, 0]); editor.setCursorBufferPosition([0, 0]);
atom.commands.dispatch(atom.views.getView(editor), 'link:open'); atom.commands.dispatch(atom.views.getView(editor), 'link:open');
expect(shell.openExternal).not.toHaveBeenCalled(); expect(shell.openExternal).not.toHaveBeenCalled();
editor.setCursorBufferPosition([0, 20]); editor.setCursorBufferPosition([0, 19]);
atom.commands.dispatch(atom.views.getView(editor), 'link:open'); atom.commands.dispatch(atom.views.getView(editor), 'link:open');
expect(shell.openExternal).toHaveBeenCalled(); expect(shell.openExternal).toHaveBeenCalled();