social-links
Version:
Validate & sanitize social links
226 lines • 12.4 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var main_1 = __importDefault(require("./main"));
var main_2 = require("./main");
describe('SocialLinks', function () {
var sl;
beforeEach(function () {
sl = new main_1.default();
});
describe('constants', function () {
it('should be defined TYPE_DESKTOP', function () {
expect(main_2.TYPE_DESKTOP).toBeDefined();
});
it('should be defined TYPE_MOBILE', function () {
expect(main_2.TYPE_MOBILE).toBeDefined();
});
it('should be distnict', function () {
expect(main_2.TYPE_MOBILE).not.toEqual(main_2.TYPE_DESKTOP);
});
});
describe('isValid', function () {
it('should be valid http', function () {
expect(sl.isValid('linkedin', 'http://www.linkedin.com/in/gkucmierz')).toBe(true);
});
it('should be valid https', function () {
expect(sl.isValid('linkedin', 'https://www.linkedin.com/in/gkucmierz')).toBe(true);
});
it('should be valid http, no-www', function () {
expect(sl.isValid('linkedin', 'http://linkedin.com/in/gkucmierz')).toBe(true);
});
it('should be valid https, no-www', function () {
expect(sl.isValid('linkedin', 'https://linkedin.com/in/gkucmierz')).toBe(true);
});
it('should be valid no-protocol', function () {
expect(sl.isValid('linkedin', 'www.linkedin.com/in/gkucmierz')).toBe(true);
});
it('should be valid no-protocol, no-www', function () {
expect(sl.isValid('linkedin', 'linkedin.com/in/gkucmierz')).toBe(true);
});
it('should be valid only id', function () {
expect(sl.isValid('linkedin', 'gkucmierz')).toBe(true);
});
});
describe('getProfileId', function () {
it('should work with http', function () {
expect(sl.getProfileId('linkedin', 'http://www.linkedin.com/in/gkucmierz')).toBe('gkucmierz');
});
it('should work with https', function () {
expect(sl.getProfileId('linkedin', 'https://www.linkedin.com/in/gkucmierz')).toBe('gkucmierz');
});
it('should work with http, no-www', function () {
expect(sl.getProfileId('linkedin', 'http://linkedin.com/in/gkucmierz')).toBe('gkucmierz');
});
it('should work with https, no-www', function () {
expect(sl.getProfileId('linkedin', 'https://linkedin.com/in/gkucmierz')).toBe('gkucmierz');
});
it('should work with no-protocol', function () {
expect(sl.getProfileId('linkedin', 'www.linkedin.com/in/gkucmierz')).toBe('gkucmierz');
});
it('should work with only id', function () {
expect(sl.getProfileId('linkedin', 'gkucmierz')).toBe('gkucmierz');
});
});
describe('getLink', function () {
it('should create default TYPE_DESKTOP link', function () {
expect(sl.getLink('linkedin', 'gkucmierz')).toBe('https://linkedin.com/in/gkucmierz');
});
it('should create TYPE_DESKTOP link', function () {
expect(sl.getLink('linkedin', 'gkucmierz', main_2.TYPE_DESKTOP)).toBe('https://linkedin.com/in/gkucmierz');
});
it('should create TYPE_MOBILE link', function () {
expect(sl.getLink('linkedin', 'gkucmierz', main_2.TYPE_MOBILE)).toBe('https://linkedin.com/mwlite/in/gkucmierz');
});
});
describe('sanitize', function () {
it('should sanitize link https, www', function () {
var sanitized = sl.sanitize('linkedin', 'https://www.linkedin.com/in/gkucmierz');
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should sanitize link https', function () {
var sanitized = sl.sanitize('linkedin', 'https://linkedin.com/in/gkucmierz');
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should sanitize link http', function () {
var sanitized = sl.sanitize('linkedin', 'http://linkedin.com/in/gkucmierz');
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should sanitize link www', function () {
var sanitized = sl.sanitize('linkedin', 'www.linkedin.com/in/gkucmierz');
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should sanitize link', function () {
var sanitized = sl.sanitize('linkedin', 'linkedin.com/in/gkucmierz');
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should sanitize link as mobile', function () {
var sanitized = sl.sanitize('linkedin', 'linkedin.com/in/gkucmierz', main_2.TYPE_MOBILE);
expect(sanitized).toBe('https://linkedin.com/mwlite/in/gkucmierz');
});
it('should sanitize mobile link as mobile', function () {
var sanitized = sl.sanitize('linkedin', 'linkedin.com/mwlite/in/gkucmierz');
expect(sanitized).toBe('https://linkedin.com/mwlite/in/gkucmierz');
});
it('should sanitize mobile link as desktop', function () {
var sanitized = sl.sanitize('linkedin', 'linkedin.com/mwlite/in/gkucmierz', main_2.TYPE_DESKTOP);
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should sanitize link with slash at end', function () {
var sanitized = sl.sanitize('linkedin', 'linkedin.com/in/gkucmierz/');
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should sanitize only profile id', function () {
var sanitized = sl.sanitize('linkedin', 'gkucmierz');
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should sanitize only profile id as mobile', function () {
var sanitized = sl.sanitize('linkedin', 'gkucmierz', main_2.TYPE_MOBILE);
expect(sanitized).toBe('https://linkedin.com/mwlite/in/gkucmierz');
});
});
describe('getProfileNames', function () {
it('should correctly return profiles names', function () {
sl = new main_1.default({ usePredefinedProfiles: false });
var testProfile = 'test_profile';
expect(sl.getProfileNames()).toStrictEqual([]);
sl.addProfile(testProfile, []);
expect(sl.getProfileNames()).toStrictEqual([testProfile]);
});
});
describe('config', function () {
describe('usePredefinedProfiles', function () {
it('should set usePredefinedProfiles = true', function () {
sl = new main_1.default({ usePredefinedProfiles: true });
expect(sl.getLink('linkedin', 'gkucmierz')).toBe('https://linkedin.com/in/gkucmierz');
});
it('should set usePredefinedProfiles = false', function () {
sl = new main_1.default({ usePredefinedProfiles: false });
expect(function () { return sl.getLink('linkedin', 'gkucmierz'); }).toThrowError();
});
});
describe('trimInput', function () {
it('should set trimInput as default', function () {
sl = new main_1.default();
var whitespace = [' ', '\t', '\n'].join('');
expect(sl.isValid('linkedin', "".concat(whitespace, "http://www.linkedin.com/in/gkucmierz").concat(whitespace))).toBeTruthy();
});
it('should trim isValid', function () {
sl = new main_1.default({ trimInput: true });
var whitespace = [' ', '\t', '\n'].join('');
expect(sl.isValid('linkedin', "".concat(whitespace, "http://www.linkedin.com/in/gkucmierz").concat(whitespace))).toBeTruthy();
});
it('should not trim isValid', function () {
sl = new main_1.default({ trimInput: false });
var whitespace = [' ', '\t', '\n'].join('');
expect(sl.isValid('linkedin', "".concat(whitespace, "http://www.linkedin.com/in/gkucmierz").concat(whitespace))).toBeFalsy();
});
});
describe('allowQueryParams', function () {
it('should not allowQueryParams as default', function () {
var params = '?param=123¶m2=abc';
expect(sl.isValid('linkedin', "http://www.linkedin.com/in/gkucmierz".concat(params))).toBeFalsy();
});
it('should allowQueryParams in link', function () {
sl = new main_1.default({ allowQueryParams: true });
var params = '?param=123¶m2=abc';
expect(sl.isValid('linkedin', "http://www.linkedin.com/in/gkucmierz".concat(params))).toBeTruthy();
});
it('should not allowQueryParams in link', function () {
sl = new main_1.default({ allowQueryParams: false });
var params = '?param=123¶m2=abc';
expect(sl.isValid('linkedin', "http://www.linkedin.com/in/gkucmierz".concat(params))).toBeFalsy();
});
it('should sanitize query params with allowQueryParams = true', function () {
sl = new main_1.default({ allowQueryParams: true });
var params = '?param=123¶m2=abc';
var sanitized = sl.sanitize('linkedin', "http://www.linkedin.com/in/gkucmierz".concat(params));
expect(sanitized).toBe('https://linkedin.com/in/gkucmierz');
});
it('should not sanitize query params with allowQueryParams = false', function () {
sl = new main_1.default({ allowQueryParams: false });
var params = '?param=123¶m2=abc';
expect(function () { return sl.sanitize('linkedin', "http://www.linkedin.com/in/gkucmierz".concat(params)); }).toThrowError();
});
it('should not use allowQueryParams when only profileId is provided', function () {
sl = new main_1.default({ allowQueryParams: true });
var params = '?param=123¶m2=abc';
expect(function () { return sl.sanitize('linkedin', "gkucmierz".concat(params)); }).toThrowError();
});
});
});
describe('profiles', function () {
it('should clean profiles', function () {
sl = new main_1.default({ usePredefinedProfiles: false });
expect(function () { return sl.getLink('linkedin', 'gkucmierz'); }).toThrowError();
});
it('should add profile', function () {
var name = 'test';
sl.addProfile(name, [{ match: '(.{3})', group: 1, pattern: '-{PROFILE_ID}-' }]);
expect(sl.isValid(name, '123')).toBeTruthy();
expect(sl.sanitize(name, '123')).toBe('-123-');
});
});
describe('detectProfile', function () {
it('should detect github profiles', function () {
expect(sl.detectProfile('https://github.com/gkucmierz')).toEqual('github');
expect(sl.detectProfile('http://github.com/abc')).toEqual('github');
expect(sl.detectProfile('github.com/abc')).toEqual('github');
expect(sl.detectProfile('www.github.com/gkucmierz')).toEqual('github');
});
it('should detect different profiles', function () {
expect(sl.detectProfile('https://exercism.io/profiles/gkucmierz')).toEqual('exercism');
expect(sl.detectProfile('https://dev.to/gkucmierz')).toEqual('dev_to');
expect(sl.detectProfile('https://youtube.com/channel/gkucmierz')).toEqual('youtube');
expect(sl.detectProfile('https://linkedin.com/in/gkucmierz')).toEqual('linkedin');
expect(sl.detectProfile('https://medium.com/@gkucmierz')).toEqual('medium');
});
it('should return empty string if no match', function () {
expect(sl.detectProfile('https://www.codewars.com/kata/my-languages')).toEqual('');
expect(sl.detectProfile('')).toEqual('');
});
});
});
//# sourceMappingURL=main.spec.js.map