-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
202 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
import { dedupLinks, getSocialLinks } from '../src/pptr-utils/get-links'; | ||
import { SOCIAL_URLS } from '../src/pptr-utils/default'; | ||
import { LinkObject } from '../src/types'; | ||
|
||
describe('get-links', () => { | ||
describe('dedupLinks', () => { | ||
it('removes duplicates from an array of links', () => { | ||
const links:LinkObject[] = [ | ||
{ | ||
href: "www.url1.com", | ||
innerHtml: "html", | ||
innerText: "Hello, world" | ||
}, | ||
{ | ||
href: "www.url1.com", | ||
innerHtml: "html", | ||
innerText: "Hello, world" | ||
}, | ||
{ | ||
href: "www.url2.com", | ||
innerHtml: "html2", | ||
innerText: "bah bah black sheep" | ||
} | ||
]; | ||
const result = dedupLinks(links); | ||
expect(result.length).toBe(2); | ||
expect(result).toStrictEqual([ | ||
{ | ||
href: "www.url1.com", | ||
innerHtml: "html", | ||
innerText: "Hello, world" | ||
}, | ||
{ | ||
href: "www.url2.com", | ||
innerHtml: "html2", | ||
innerText: "bah bah black sheep" | ||
} | ||
]); | ||
}); | ||
it('returns original array of links if no duplicates', () => { | ||
const links: LinkObject[] = [ | ||
{ | ||
href: "www.url1.com", | ||
innerHtml: "html", | ||
innerText: "Hello, world" | ||
}, | ||
{ | ||
href: "www.url2.com", | ||
innerHtml: "html2", | ||
innerText: "bah bah black sheep" | ||
} | ||
]; | ||
const result = dedupLinks(links); | ||
expect(result).toHaveLength(2); | ||
expect(result).toStrictEqual(links); | ||
}); | ||
}); | ||
|
||
describe('getSocialLinks', () => { | ||
it('filters out only social links from a list of LinkObjects', () => { | ||
const links:LinkObject[] = [ | ||
{ | ||
href: 'url1.com', | ||
innerHtml: 'html', | ||
innerText: 'hello world!' | ||
}, | ||
{ | ||
href: 'www.facebook.com', | ||
innerHtml: 'facebook', | ||
innerText: 'fb' | ||
}, | ||
{ | ||
href: 'www.x.com', | ||
innerHtml: 'x', | ||
innerText: 'x' | ||
} | ||
]; | ||
const results = getSocialLinks(links); | ||
expect(results).toHaveLength(2); | ||
expect(results).toStrictEqual([ | ||
{ | ||
href: 'www.facebook.com', | ||
innerHtml: 'facebook', | ||
innerText: 'fb' | ||
} , | ||
{ | ||
href: 'www.x.com', | ||
innerHtml: 'x', | ||
innerText: 'x' | ||
} | ||
]); | ||
}); | ||
it('doesn\'t recognize urls ending in a social url', () => { | ||
const links = [ | ||
{ | ||
href: 'www.x.com', | ||
innerHtml: 'x', | ||
innerText: 'x' | ||
}, | ||
{ | ||
href: 'x.com', | ||
innerHtml: 'x', | ||
innerText: 'x' | ||
}, | ||
{ | ||
href: 'www.fix.com', | ||
innerHtml: 'fix', | ||
innerText: 'fix' | ||
}, | ||
{ | ||
href: 'fix.com', | ||
innerHtml: 'fix', | ||
innerText: 'fix' | ||
} | ||
]; | ||
const results = getSocialLinks(links); | ||
expect(results).toHaveLength(2); | ||
expect(results).toStrictEqual([ | ||
{ | ||
href: 'www.x.com', | ||
innerHtml: 'x', | ||
innerText: 'x' | ||
}, | ||
{ | ||
href: 'x.com', | ||
innerHtml: 'x', | ||
innerText: 'x' | ||
}, | ||
]); | ||
}); | ||
it('returns original array if it contains only social links', () => { | ||
const links = [ | ||
{ | ||
href: 'www.evernote.com', | ||
innerHtml: 'evernote', | ||
innerText: 'evernote' | ||
}, | ||
{ | ||
href: 'www.tiktok.com', | ||
innerHtml: 'tiktok', | ||
innerText: 'tiktok' | ||
} | ||
]; | ||
const result = getSocialLinks(links); | ||
expect(result).toHaveLength(links.length); | ||
expect(result).toStrictEqual(links); | ||
}); | ||
it('recognizes every social link', () => { | ||
const links:LinkObject[] = SOCIAL_URLS.map(url => ({ href: `www.${url}`, innerText: 'text', innerHtml: 'html'})); | ||
const result = getSocialLinks(links); | ||
expect(result).toHaveLength(links.length); | ||
expect(result).toStrictEqual(links); | ||
}); | ||
it('recognizes different versions of a social link', () => { | ||
const links = [ | ||
{ | ||
href: 'snapchat.com', | ||
innerHtml: 'snapchat', | ||
innerText: 'snapchat' | ||
}, | ||
{ | ||
href: 'www.snapchat.com', | ||
innerHtml: 'snapchat', | ||
innerText: 'snapchat' | ||
}, | ||
{ | ||
href: 'http://snapchat.com', | ||
innerHtml: 'snapchat', | ||
innerText: 'snapchat' | ||
}, | ||
{ | ||
href: 'https://snapchat.com', | ||
innerHtml: 'snapchat', | ||
innerText: 'snapchat' | ||
}, | ||
{ | ||
href: 'http://www.snapchat.com', | ||
innerHtml: 'snapchat', | ||
innerText: 'snapchat' | ||
}, | ||
{ | ||
href: 'snapchat.com/page', | ||
innerHtml: 'snapchat', | ||
innerText: 'snapchat' | ||
}, | ||
{ | ||
href: 'www.snapchat.com/page', | ||
innerHtml: 'snapchat', | ||
innerText: 'snapchat' | ||
}, | ||
{ | ||
href: 'subdomain.snapchat.com/page', | ||
innerHtml: 'snapchat', | ||
innerText: 'snapchat' | ||
} | ||
]; | ||
const results = getSocialLinks(links); | ||
expect(results).toHaveLength(links.length); | ||
expect(results).toStrictEqual(links); | ||
}); | ||
}); | ||
}); |