test/alias_test.js
'use strict';
import {expect} from 'chai';
import {describe, it} from 'mocha';
import {getAlias, setAlias} from '../src/index';
/**
* @test {getAlias}
*/
describe('getAlias()', () => {
it('should return an empty string if the alias is not resolvable', () => {
expect(getAlias('@fooBarBaz123456', false)).to.be.empty;
});
it('should eventually throw an error if the alias is not resolvable', () => {
expect(() => getAlias('@fooBarBaz123456', true)).to.throw(TypeError);
});
it('should return the path or URL of the specified alias', () => {
setAlias('@test', '/home/cedx');
expect(getAlias('@test')).to.equal('/home/cedx');
expect(getAlias('@test/foo/bar.js')).to.equal('/home/cedx/foo/bar.js');
setAlias('@foobar', '@test/foobar');
expect(getAlias('@foobar/baz.js')).to.equal('/home/cedx/foobar/baz.js');
});
it('should return the specified value if it is not a path alias', () => {
expect(getAlias('')).to.be.empty;
expect(getAlias('fooBar')).to.equal('fooBar');
});
});
/**
* @test {setAlias}
*/
describe('setAlias()', () => {
it('should throw an error if the specified name is empty', () => {
expect(() => setAlias('', '/home/cedx')).to.throw(TypeError);
});
it('should normalize the directory separator', () => {
setAlias('@foo', '\\home\\cedx\\');
expect(getAlias('@foo')).to.equal('/home/cedx');
});
it('should add the required `@` character to the specified name', () => {
setAlias('bar', '/home/cedx');
expect(getAlias('@bar')).to.equal('/home/cedx');
});
it('should remove an alias if the specified value is empty', () => {
setAlias('@baz', '/home/cedx');
expect(getAlias('@baz')).to.equal('/home/cedx');
setAlias('@baz', '');
expect(() => getAlias('@baz')).to.throw;
});
});