|
| 1 | +/* eslint-env mocha */ |
| 2 | +const expect = require('chai').expect |
| 3 | +const prefixes = require('../lib/prefixes') |
| 4 | + |
| 5 | +describe('prefix utilities', () => { |
| 6 | + describe('toKey', () => { |
| 7 | + it('converts a prefix to a key', () => { |
| 8 | + expect(prefixes.toKey('this')).to.eql('@prefix/this') |
| 9 | + }) |
| 10 | + }) |
| 11 | + |
| 12 | + describe('fromKey', () => { |
| 13 | + it('converts a key to its prefix', () => { |
| 14 | + expect(prefixes.fromKey('@prefix/this')).to.eql('this') |
| 15 | + }) |
| 16 | + }) |
| 17 | + |
| 18 | + describe('fromNodes', () => { |
| 19 | + it('converts hyperdb nodes to prefix/uri object', () => { |
| 20 | + const dummyNodes = [{ key: '@prefix/this', value: 'http://this.example.com' }] |
| 21 | + expect(prefixes.fromNodes(dummyNodes)).to.eql({ uri: 'http://this.example.com', prefix: 'this' }) |
| 22 | + }) |
| 23 | + it('ignores conflicts', () => { |
| 24 | + const dummyNodes = [ |
| 25 | + { key: '@prefix/this', value: 'http://this.example.com' }, |
| 26 | + { key: '@prefix/this', value: 'http://conflict.example.com' } |
| 27 | + ] |
| 28 | + expect(prefixes.fromNodes(dummyNodes)).to.eql({ uri: 'http://this.example.com', prefix: 'this' }) |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + describe('toPrefixed', () => { |
| 33 | + it('returns unmodified string if prefix is undefined', () => { |
| 34 | + const str = 'http://test.com/this/ok#wow' |
| 35 | + const prefixed = prefixes.toPrefixed(str) |
| 36 | + expect(prefixed).to.eql(str) |
| 37 | + }) |
| 38 | + it('returns unmodified string if prefix is not set', () => { |
| 39 | + const str = 'http://test.com/this/ok#wow' |
| 40 | + const prefixed = prefixes.toPrefixed(str, { 'wow': 'http://wow.com/' }) |
| 41 | + expect(prefixed).to.eql(str) |
| 42 | + }) |
| 43 | + it('returns prefix string if prefix is present', () => { |
| 44 | + const str = 'http://wow.com/now/this#and_hashed' |
| 45 | + const prefixed = prefixes.toPrefixed(str, { 'wow': 'http://wow.com/' }) |
| 46 | + expect(prefixed).to.eql('wow:now/this#and_hashed') |
| 47 | + }) |
| 48 | + }) |
| 49 | + |
| 50 | + describe('fromPrefixed', () => { |
| 51 | + it('returns unmodified string if prefix is undefined', () => { |
| 52 | + const str = 'some:thing' |
| 53 | + const prefixed = prefixes.fromPrefixed(str) |
| 54 | + expect(prefixed).to.eql(str) |
| 55 | + }) |
| 56 | + it('returns unmodified string if prefix is not set', () => { |
| 57 | + const str = 'some:thing' |
| 58 | + const prefixed = prefixes.fromPrefixed(str, { 'wow': 'http://wow.com/' }) |
| 59 | + expect(prefixed).to.eql(str) |
| 60 | + }) |
| 61 | + it('returns prefix string if prefix is present', () => { |
| 62 | + const str = 'wow:now' |
| 63 | + const prefixed = prefixes.fromPrefixed(str, { 'wow': 'http://wow.com/' }) |
| 64 | + expect(prefixed).to.eql('http://wow.com/now') |
| 65 | + }) |
| 66 | + }) |
| 67 | +}) |
0 commit comments