Skip to content

Commit

Permalink
Add tests for the Url class in handy-url
Browse files Browse the repository at this point in the history
  • Loading branch information
negativetwelve committed Sep 2, 2017
1 parent 45d828c commit 9127c1a
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/handy-url/src/Url.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class Url {
}

get domain() {
return _.first(_.last(this.hostname.split('.'), 2));
return _.first(_.lastN(this.hostname.split('.'), 2));
}

get tld() {
Expand Down
162 changes: 162 additions & 0 deletions packages/handy-url/src/__tests__/Url.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
* @jest-environment jsdom
*
* TODO(mark): Unsure why this is necessary but in a node environment, tests
* fail. Definitely has to do with the Url class.
*/

// Imports
import Url from '../Url';


describe('Url', () => {

// --------------------------------------------------
// Protocol
// --------------------------------------------------
describe('#protocol', () => {
const httpProtocols = [
'http://google.com',
'http://a.b.com',
];

httpProtocols.forEach(url => {
it(`returns 'http' for ${url}`, () => {
expect(new Url(url).protocol).toBe('http');
});
});

const httpsProtocols = [
'https://google.com',
'https://www.google.com',
];

httpsProtocols.forEach(url => {
it(`returns 'https' for ${url}`, () => {
expect(new Url(url).protocol).toBe('https');
});
});

const invalidProtocols = [
'www.google.com',
'google.com',
'http//google.com',
'ftp//google.com',
'ftp/google.com',
];

invalidProtocols.forEach(url => {
it(`returns null for ${url}`, () => {
expect(new Url(url).protocol).toBeNull();
});
});
});

describe('#isAbsolute', () => {
const absoluteUrls = [
'http://google.com',
'https://google.com',
'http://www.google.com',
'https://www.google.com',
];

absoluteUrls.forEach(url => {
it(`returns true for ${url}`, () => {
expect(new Url(url).isAbsolute).toBe(true);
});
});

const relativeUrls = [
'www.google.com',
'google.com',
];

relativeUrls.forEach(url => {
it(`returns false for ${url}`, () => {
expect(new Url(url).isAbsolute).toBe(false);
});
});
});

describe('#isSecure', () => {
const secureUrls = [
'https://www.google.com',
'https://google.com',
'https://facebook.com',
];

secureUrls.forEach(url => {
it(`returns true for ${url}`, () => {
expect(new Url(url).isSecure).toBe(true);
});
});

const insecureUrls = [
'http://www.google.com',
'blahblah',
'ftp://google.com',
'blob://google.com',
];

insecureUrls.forEach(url => {
it(`return false for ${url}`, () => {
expect(new Url(url).isSecure).toBe(false);
});
});
});

// --------------------------------------------------
// Host / Hostname
// --------------------------------------------------
describe('#hostname', () => {
it('returns subdomains in the hostname', () => {
expect(new Url('http://www.google.com').hostname).toBe('www.google.com');
});

it('returns all subdomains in the hostname', () => {
expect(new Url('https://something.www.google.com').hostname).toBe('something.www.google.com');
});
});

describe('#domain', () => {
it('does not return any subdomains', () => {
expect(new Url('https://www.google.com').domain).toBe('google');
});

it('does not return the tld', () => {
expect(new Url('https://google.com').domain).toBe('google');
});

it('does not require a protocol', () => {
expect(new Url('www.google.com').domain).toBe('google');
});
});

describe('#tld', () => {
it('does not return the domain name', () => {
expect(new Url('https://www.google.com').tld).toBe('com');
});
});

// --------------------------------------------------
// Filename
// --------------------------------------------------
describe('#filename', () => {
it('parses the filename from a path', () => {
const url = new Url('http://markmiyashita.com/downloads/mark_miyashita_resume.pdf');

expect(url.filename).toBe('mark_miyashita_resume.pdf');
});
});

describe('#fileBasename', () => {
it('parses out the extension', () => {
expect(new Url('my_file.pdf').fileBasename).toBe('my_file');
});

it('splits by period and rejoins properly', () => {
expect(new Url('a.b.c.xls').fileBasename).toBe('a.b.c');
});
});

});

0 comments on commit 9127c1a

Please sign in to comment.