1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-19 07:35:48 +02:00
bitwarden-browser/src/services/utils.service.spec.ts
Oscar Hinton 4531846ff8 Add support for running unit tests (#381)
* Add test runner.

* Fix tests. Add tests for UtilsService.getDomain

* Test getHostname.

* Add two missing test cases and fix getDomain.
2017-11-17 16:33:58 -05:00

55 lines
2.8 KiB
TypeScript

import UtilsService from './utils.service';
describe('Utils Service', () => {
describe('getDomain', () => {
it('should fail for invalid urls', () => {
expect(UtilsService.getDomain(null)).toBeNull();
expect(UtilsService.getDomain(undefined)).toBeNull();
expect(UtilsService.getDomain(' ')).toBeNull();
expect(UtilsService.getDomain('https://bit!:"_&ward.com')).toBeNull();
expect(UtilsService.getDomain('bitwarden')).toBeNull();
});
it('should handle urls without protocol', () => {
expect(UtilsService.getDomain('bitwarden.com')).toBe('bitwarden.com');
expect(UtilsService.getDomain('wrong://bitwarden.com')).toBe('bitwarden.com');
});
it('should handle valid urls', () => {
expect(UtilsService.getDomain('https://bitwarden')).toBe('bitwarden');
expect(UtilsService.getDomain('https://bitwarden.com')).toBe('bitwarden.com');
expect(UtilsService.getDomain('http://bitwarden.com')).toBe('bitwarden.com');
expect(UtilsService.getDomain('http://vault.bitwarden.com')).toBe('bitwarden.com');
expect(UtilsService.getDomain('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
expect(UtilsService.getDomain('https://bitwarden.unknown')).toBe('bitwarden.unknown');
});
it('should support localhost and IP', () => {
expect(UtilsService.getDomain('https://localhost')).toBe('localhost');
expect(UtilsService.getDomain('https://192.168.1.1')).toBe('192.168.1.1');
});
});
describe('getHostname', () => {
it('should fail for invalid urls', () => {
expect(UtilsService.getHostname(null)).toBeNull();
expect(UtilsService.getHostname(undefined)).toBeNull();
expect(UtilsService.getHostname(' ')).toBeNull();
expect(UtilsService.getHostname('https://bit!:"_&ward.com')).toBeNull();
expect(UtilsService.getHostname('bitwarden')).toBeNull();
});
it('should handle valid urls', () => {
expect(UtilsService.getHostname('https://bitwarden.com')).toBe('bitwarden.com');
expect(UtilsService.getHostname('http://bitwarden.com')).toBe('bitwarden.com');
expect(UtilsService.getHostname('http://vault.bitwarden.com')).toBe('vault.bitwarden.com');
expect(UtilsService.getHostname('https://user:password@bitwarden.com:8080/password/sites?and&query#hash')).toBe('bitwarden.com');
});
it('should support localhost and IP', () => {
expect(UtilsService.getHostname('https://localhost')).toBe('localhost');
expect(UtilsService.getHostname('https://192.168.1.1')).toBe('192.168.1.1');
});
});
});