pulsar/spec/uri-handler-registry-spec.js

76 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-09-19 03:33:51 +03:00
/** @babel */
2017-09-27 22:40:21 +03:00
import url from 'url'
2017-09-19 03:33:51 +03:00
import {it} from './async-spec-helpers'
2017-10-18 01:23:10 +03:00
import URIHandlerRegistry from '../src/uri-handler-registry'
2017-09-19 03:33:51 +03:00
2017-10-18 01:23:10 +03:00
describe('URIHandlerRegistry', () => {
2017-09-27 23:00:42 +03:00
let registry
beforeEach(() => {
2017-10-18 01:23:10 +03:00
registry = new URIHandlerRegistry(5)
2017-09-27 23:00:42 +03:00
})
2017-09-19 03:33:51 +03:00
2017-10-18 01:23:10 +03:00
it('handles URIs on a per-host basis', () => {
2017-09-19 03:33:51 +03:00
const testPackageSpy = jasmine.createSpy()
const otherPackageSpy = jasmine.createSpy()
registry.registerHostHandler('test-package', testPackageSpy)
registry.registerHostHandler('other-package', otherPackageSpy)
2017-10-18 01:23:10 +03:00
registry.handleURI('atom://yet-another-package/path')
2017-09-19 03:33:51 +03:00
expect(testPackageSpy).not.toHaveBeenCalled()
expect(otherPackageSpy).not.toHaveBeenCalled()
2017-10-18 01:23:10 +03:00
registry.handleURI('atom://test-package/path')
2017-09-27 22:40:21 +03:00
expect(testPackageSpy).toHaveBeenCalledWith(url.parse('atom://test-package/path', true), 'atom://test-package/path')
2017-09-19 03:33:51 +03:00
expect(otherPackageSpy).not.toHaveBeenCalled()
2017-10-18 01:23:10 +03:00
registry.handleURI('atom://other-package/path')
2017-09-27 22:40:21 +03:00
expect(otherPackageSpy).toHaveBeenCalledWith(url.parse('atom://other-package/path', true), 'atom://other-package/path')
2017-09-19 03:33:51 +03:00
})
2017-09-27 23:00:42 +03:00
it('keeps track of the most recent URIs', () => {
const spy1 = jasmine.createSpy()
const spy2 = jasmine.createSpy()
const changeSpy = jasmine.createSpy()
registry.registerHostHandler('one', spy1)
registry.registerHostHandler('two', spy2)
registry.onHistoryChange(changeSpy)
2017-10-18 01:23:10 +03:00
const uris = [
2017-09-27 23:00:42 +03:00
'atom://one/something?asdf=1',
'atom://fake/nothing',
'atom://two/other/stuff',
'atom://one/more/thing',
'atom://two/more/stuff'
]
2017-10-18 01:23:10 +03:00
uris.forEach(u => registry.handleURI(u))
2017-09-27 23:00:42 +03:00
expect(changeSpy.callCount).toBe(5)
2017-10-18 01:23:10 +03:00
expect(registry.getRecentlyHandledURIs()).toEqual(uris.map((u, idx) => {
return {id: idx + 1, uri: u, handled: !u.match(/fake/), host: url.parse(u).host}
2017-09-27 23:00:42 +03:00
}).reverse())
2017-10-18 01:23:10 +03:00
registry.handleURI('atom://another/url')
2017-09-27 23:00:42 +03:00
expect(changeSpy.callCount).toBe(6)
2017-10-18 01:23:10 +03:00
const history = registry.getRecentlyHandledURIs()
2017-09-27 23:00:42 +03:00
expect(history.length).toBe(5)
2017-10-18 01:23:10 +03:00
expect(history[0].uri).toBe('atom://another/url')
expect(history[4].uri).toBe(uris[1])
2017-09-27 23:00:42 +03:00
})
2017-09-19 03:33:51 +03:00
it('refuses to handle bad URLs', () => {
[
'atom:package/path',
'atom:8080://package/path',
'user:pass@atom://package/path',
'smth://package/path'
].forEach(uri => {
2017-10-18 01:23:10 +03:00
expect(() => registry.handleURI(uri)).toThrow()
2017-09-19 03:33:51 +03:00
})
})
})