Skip to content

Commit d309898

Browse files
committed
Added unit tests for url-utils
1 parent 392d1e3 commit d309898

15 files changed

+756
-0
lines changed

packages/url-utils/test/unit/utils/absolute-to-transform-ready.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ describe('utils: absoluteToTransformReady()', function () {
3535
absoluteToTransformReady(url, root).should.eql('http://i%20don’t%20believe%20that%20our%20platform%20should%20take%20that%20down%20because%20i%20think%20there%20are%20things%20that%20different%20people%20get%20wrong');
3636
});
3737

38+
it('handles unparseable URLs that throw errors', function () {
39+
// Test URLs that cause URL constructor to throw
40+
let url = 'http://[invalid';
41+
let root = 'https://example.com';
42+
absoluteToTransformReady(url, root).should.eql('http://[invalid');
43+
44+
url = 'not a valid url at all!!!';
45+
absoluteToTransformReady(url, root).should.eql('not a valid url at all!!!');
46+
});
47+
3848
describe('with matching root', function () {
3949
it('returns relative file', function () {
4050
let url = 'https://example.com/my/file.png';
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require('../../utils');
2+
3+
const deduplicateDoubleSlashes = require('../../../lib/utils/deduplicate-double-slashes');
4+
5+
describe('utils: deduplicateDoubleSlashes()', function () {
6+
it('should deduplicate double slashes in URL', function () {
7+
deduplicateDoubleSlashes('http://example.com//path//to//file.png')
8+
.should.eql('http:/example.com/path/to/file.png');
9+
});
10+
11+
it('should deduplicate multiple consecutive slashes', function () {
12+
deduplicateDoubleSlashes('http://example.com///path////to///file.png')
13+
.should.eql('http:/example.com//path//to//file.png');
14+
});
15+
16+
it('should handle path with no double slashes', function () {
17+
deduplicateDoubleSlashes('http://example.com/path/to/file.png')
18+
.should.eql('http:/example.com/path/to/file.png');
19+
});
20+
21+
it('should handle relative paths', function () {
22+
deduplicateDoubleSlashes('/path//to//file.png')
23+
.should.eql('/path/to/file.png');
24+
});
25+
26+
it('should handle protocol slashes correctly', function () {
27+
deduplicateDoubleSlashes('http://example.com//path')
28+
.should.eql('http:/example.com/path');
29+
});
30+
31+
it('should handle empty string', function () {
32+
deduplicateDoubleSlashes('')
33+
.should.eql('');
34+
});
35+
36+
it('should handle string with only slashes', function () {
37+
deduplicateDoubleSlashes('////')
38+
.should.eql('//');
39+
});
40+
});
41+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require('../../utils');
2+
3+
const htmlToTransformReady = require('../../../lib/utils/html-to-transform-ready');
4+
5+
describe('utils: htmlToTransformReady()', function () {
6+
const siteUrl = 'http://my-ghost-blog.com';
7+
const itemPath = '/my-awesome-post';
8+
let options;
9+
10+
beforeEach(function () {
11+
options = {
12+
staticImageUrlPrefix: 'content/images'
13+
};
14+
});
15+
16+
it('converts relative HTML to transform-ready', function () {
17+
const html = '<a href="/about">Link</a><img src="/content/images/test.jpg">';
18+
const result = htmlToTransformReady(html, siteUrl, itemPath, options);
19+
20+
result.should.containEql('<a href="__GHOST_URL__/about">Link</a>');
21+
result.should.containEql('<img src="__GHOST_URL__/content/images/test.jpg">');
22+
});
23+
24+
it('converts relative HTML with page-relative URLs', function () {
25+
const html = '<a href="about">Link</a>';
26+
const result = htmlToTransformReady(html, siteUrl, itemPath, options);
27+
28+
result.should.containEql('<a href="__GHOST_URL__/my-awesome-post/about">Link</a>');
29+
});
30+
31+
it('handles options when itemPath is an object', function () {
32+
const html = '<a href="/about">Link</a>';
33+
const optionsAsItemPath = {
34+
staticImageUrlPrefix: 'content/images',
35+
assetsOnly: true
36+
};
37+
const result = htmlToTransformReady(html, siteUrl, optionsAsItemPath);
38+
39+
result.should.containEql('<a href="/about">Link</a>');
40+
});
41+
42+
it('handles options when itemPath is an object and options is null', function () {
43+
const html = '<a href="/about">Link</a>';
44+
const optionsAsItemPath = {
45+
staticImageUrlPrefix: 'content/images'
46+
};
47+
const result = htmlToTransformReady(html, siteUrl, optionsAsItemPath, null);
48+
49+
result.should.containEql('<a href="__GHOST_URL__/about">Link</a>');
50+
});
51+
52+
it('works with subdirectories', function () {
53+
const url = 'http://my-ghost-blog.com/blog';
54+
const html = '<a href="/blog/about">Link</a>';
55+
const result = htmlToTransformReady(html, url, 'blog/my-post', options);
56+
57+
result.should.containEql('<a href="__GHOST_URL__/about">Link</a>');
58+
});
59+
});
60+
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
require('../../utils');
2+
3+
const lexicalToTransformReady = require('../../../lib/utils/lexical-to-transform-ready');
4+
5+
describe('utils: lexicalToTransformReady()', function () {
6+
const siteUrl = 'http://my-ghost-blog.com';
7+
const itemPath = '/my-awesome-post';
8+
let options;
9+
10+
beforeEach(function () {
11+
options = {
12+
staticImageUrlPrefix: 'content/images'
13+
};
14+
});
15+
16+
it('converts relative lexical to transform-ready', function () {
17+
const lexical = JSON.stringify({
18+
root: {
19+
children: [
20+
{
21+
children: [
22+
{
23+
children: [
24+
{
25+
detail: 0,
26+
format: 0,
27+
mode: 'normal',
28+
style: '',
29+
text: 'Test',
30+
type: 'text',
31+
version: 1
32+
}
33+
],
34+
direction: 'ltr',
35+
format: '',
36+
indent: 0,
37+
type: 'link',
38+
version: 1,
39+
rel: null,
40+
target: null,
41+
url: '/test'
42+
}
43+
],
44+
direction: 'ltr',
45+
format: '',
46+
indent: 0,
47+
type: 'paragraph',
48+
version: 1
49+
}
50+
],
51+
direction: 'ltr',
52+
format: '',
53+
indent: 0,
54+
type: 'root',
55+
version: 1
56+
}
57+
});
58+
59+
const result = lexicalToTransformReady(lexical, siteUrl, itemPath, options);
60+
const parsed = JSON.parse(result);
61+
62+
parsed.root.children[0].children[0].url.should.equal('__GHOST_URL__/test');
63+
});
64+
65+
it('handles options when itemPath is an object', function () {
66+
const lexical = JSON.stringify({
67+
root: {
68+
children: [
69+
{
70+
children: [
71+
{
72+
children: [
73+
{
74+
detail: 0,
75+
format: 0,
76+
mode: 'normal',
77+
style: '',
78+
text: 'Test',
79+
type: 'text',
80+
version: 1
81+
}
82+
],
83+
direction: 'ltr',
84+
format: '',
85+
indent: 0,
86+
type: 'link',
87+
version: 1,
88+
rel: null,
89+
target: null,
90+
url: '/test'
91+
}
92+
],
93+
direction: 'ltr',
94+
format: '',
95+
indent: 0,
96+
type: 'paragraph',
97+
version: 1
98+
}
99+
],
100+
direction: 'ltr',
101+
format: '',
102+
indent: 0,
103+
type: 'root',
104+
version: 1
105+
}
106+
});
107+
108+
const optionsAsItemPath = {
109+
staticImageUrlPrefix: 'content/images'
110+
};
111+
const result = lexicalToTransformReady(lexical, siteUrl, optionsAsItemPath);
112+
const parsed = JSON.parse(result);
113+
114+
parsed.root.children[0].children[0].url.should.equal('__GHOST_URL__/test');
115+
});
116+
117+
it('handles options when itemPath is an object and options is null', function () {
118+
const lexical = JSON.stringify({
119+
root: {
120+
children: [
121+
{
122+
children: [
123+
{
124+
children: [
125+
{
126+
detail: 0,
127+
format: 0,
128+
mode: 'normal',
129+
style: '',
130+
text: 'Test',
131+
type: 'text',
132+
version: 1
133+
}
134+
],
135+
direction: 'ltr',
136+
format: '',
137+
indent: 0,
138+
type: 'link',
139+
version: 1,
140+
rel: null,
141+
target: null,
142+
url: '/test'
143+
}
144+
],
145+
direction: 'ltr',
146+
format: '',
147+
indent: 0,
148+
type: 'paragraph',
149+
version: 1
150+
}
151+
],
152+
direction: 'ltr',
153+
format: '',
154+
indent: 0,
155+
type: 'root',
156+
version: 1
157+
}
158+
});
159+
160+
const optionsAsItemPath = {
161+
staticImageUrlPrefix: 'content/images'
162+
};
163+
const result = lexicalToTransformReady(lexical, siteUrl, optionsAsItemPath, null);
164+
const parsed = JSON.parse(result);
165+
166+
parsed.root.children[0].children[0].url.should.equal('__GHOST_URL__/test');
167+
});
168+
169+
it('handles empty lexical', function () {
170+
const lexical = JSON.stringify({
171+
root: {
172+
children: [],
173+
direction: 'ltr',
174+
format: '',
175+
indent: 0,
176+
type: 'root',
177+
version: 1
178+
}
179+
});
180+
181+
const result = lexicalToTransformReady(lexical, siteUrl, itemPath, options);
182+
const parsed = JSON.parse(result);
183+
184+
parsed.root.children.should.be.an.Array();
185+
parsed.root.children.length.should.equal(0);
186+
});
187+
});
188+

packages/url-utils/test/unit/utils/markdown-absolute-to-transform-ready.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,57 @@ Testing <a href="__GHOST_URL__/link">Inline</a> with **markdown**
117117
result.should.equal('[![Test](__GHOST_URL__/content/images/2014/01/test.jpg)](__GHOST_URL__/content/images/2014/01/test.jpg)');
118118
});
119119

120+
it('handles default options when options is not provided', function () {
121+
const markdown = 'This is a [link](http://my-ghost-blog.com/link)';
122+
const result = markdownAbsoluteToTransformReady(markdown, siteUrl);
123+
124+
result.should.equal('This is a [link](__GHOST_URL__/link)');
125+
});
126+
127+
it('handles ignoreProtocol option', function () {
128+
const markdown = 'This is a [link](https://my-ghost-blog.com/link)';
129+
const testOptions = {
130+
ignoreProtocol: true
131+
};
132+
const result = markdownAbsoluteToTransformReady(markdown, 'http://my-ghost-blog.com', testOptions);
133+
134+
result.should.equal('This is a [link](__GHOST_URL__/link)');
135+
});
136+
137+
it('handles ignoreProtocol option set to false', function () {
138+
const markdown = 'This is a [link](https://my-ghost-blog.com/link)';
139+
const testOptions = {
140+
ignoreProtocol: false
141+
};
142+
const result = markdownAbsoluteToTransformReady(markdown, 'http://my-ghost-blog.com', testOptions);
143+
144+
result.should.equal('This is a [link](https://my-ghost-blog.com/link)');
145+
});
146+
147+
it('handles assetsOnly option', function () {
148+
const markdown = '![](http://my-ghost-blog.com/content/images/image.png) [](http://my-ghost-blog.com/not-an-asset)';
149+
const testOptions = {
150+
assetsOnly: true
151+
};
152+
const result = markdownAbsoluteToTransformReady(markdown, siteUrl, testOptions);
153+
154+
result.should.equal('![](__GHOST_URL__/content/images/image.png) [](http://my-ghost-blog.com/not-an-asset)');
155+
});
156+
157+
it('handles siteUrl with trailing slash', function () {
158+
const markdown = 'This is a [link](http://my-ghost-blog.com/link)';
159+
const result = markdownAbsoluteToTransformReady(markdown, 'http://my-ghost-blog.com/', options);
160+
161+
result.should.equal('This is a [link](__GHOST_URL__/link)');
162+
});
163+
164+
it('handles siteUrl with https protocol', function () {
165+
const markdown = 'This is a [link](https://my-ghost-blog.com/link)';
166+
const result = markdownAbsoluteToTransformReady(markdown, 'https://my-ghost-blog.com', options);
167+
168+
result.should.equal('This is a [link](__GHOST_URL__/link)');
169+
});
170+
120171
describe('AST parsing is skipped', function () {
121172
let remarkSpy, sandbox;
122173

0 commit comments

Comments
 (0)