Skip to content

Commit 8001b6b

Browse files
committed
test: rewrite tests inline
1 parent 0987003 commit 8001b6b

17 files changed

+59
-54
lines changed

test/expected/appends-existing.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/basic.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/invalid-url.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/no-config.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/no-encode.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/no-sort.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/expected/tags.html

Lines changed: 0 additions & 5 deletions
This file was deleted.

test/expected/variable-url.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/appends-existing.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/basic.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/invalid-url.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/no-config.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/no-encode.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/no-sort.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/fixtures/tags.html

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/fixtures/variable-url.html

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/test.js

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,66 +2,94 @@ const test = require('ava')
22
const plugin = require('../lib')
33
const posthtml = require('posthtml')
44

5-
const path = require('path')
6-
const {readFileSync} = require('fs')
7-
8-
const fixture = file => readFileSync(path.join(__dirname, 'fixtures', `${file}.html`), 'utf8')
9-
const expected = file => readFileSync(path.join(__dirname, 'expected', `${file}.html`), 'utf8')
10-
115
const clean = html => html.replace(/[^\S\r\n]+$/gm, '').trim()
126

13-
const process = (t, name, options, log = false) => {
7+
const process = (html, options, log = false) => {
148
return posthtml([plugin(options)])
15-
.process(fixture(name))
9+
.process(html)
1610
.then(result => log ? console.log(result.html) : clean(result.html))
17-
.then(html => t.is(html, expected(name).trim()))
1811
}
1912

20-
test('Basic', t => {
21-
return process(t, 'basic', {
22-
parameters: {foo: 'bar', baz: 'qux'}
13+
test('Skip if config or parameters missing', async t => {
14+
const html = await process('<a href="https://example.com">Test</a>')
15+
16+
t.is(html, '<a href="https://example.com">Test</a>')
17+
})
18+
19+
test('Skip if invalid URL (`strict` enabled)', async t => {
20+
const html = await process('<a href="undefined">Test</a>', {
21+
parameters: {foo: 'bar'}
2322
})
23+
24+
t.is(html, '<a href="undefined">Test</a>')
2425
})
2526

26-
test('Skip if config or parameters missing', t => {
27-
return process(t, 'no-config')
27+
test('Apply to invalid URL (`strict` disabled)', async t => {
28+
const html = await process('<a href="undefined">Test</a>', {
29+
parameters: {foo: 'bar'},
30+
strict: false
31+
})
32+
33+
t.is(html, '<a href="undefined?foo=bar">Test</a>')
2834
})
2935

30-
test('Skip if invalid URL', t => {
31-
return process(t, 'invalid-url', {
32-
parameters: {foo: 'bar'}
36+
test('Adds parameters to a[href] attribute value', async t => {
37+
const html = await process('<a href="https://example.com">Test</a>', {
38+
parameters: {foo: 'bar', baz: 'qux'}
3339
})
40+
41+
t.is(html, '<a href="https://example.com?baz=qux&foo=bar">Test</a>')
3442
})
3543

36-
test('Does not skip variable URL', t => {
37-
return process(t, 'variable-url', {
38-
parameters: {foo: 'bar'}
44+
test('URL with special characters', async t => {
45+
const html = await process('<a href="https://example.com/{{ var }}?foo=bar">Test</a>', {
46+
parameters: {bar: 'baz'},
47+
strict: false
3948
})
49+
50+
t.is(html, '<a href="https://example.com/{{ var }}?bar=baz&foo=bar">Test</a>')
4051
})
4152

42-
test('Does not encode parameters if `encode` option is false', t => {
43-
return process(t, 'no-encode', {
53+
test('Does not encode parameters if `encode` option is false', async t => {
54+
const html = await process('<a href="https://example.com">Test</a>', {
4455
qs: {encode: false},
4556
parameters: {foo: '@Bar@'}
4657
})
58+
59+
t.is(html, '<a href="https://example.com?foo=@Bar@">Test</a>')
4760
})
4861

49-
test('Does not sort parameters if `sort` option is false', t => {
50-
return process(t, 'no-sort', {
62+
test('Does not sort parameters if `sort` option is false', async t => {
63+
const html = await process('<a href="https://example.com">Test</a>', {
5164
qs: {sort: false},
5265
parameters: {foo: 'bar', baz: 'qux'}
5366
})
67+
68+
t.is(html, '<a href="https://example.com?foo=bar&baz=qux">Test</a>')
5469
})
5570

56-
test('Appends new parameters to existing parameters', t => {
57-
return process(t, 'appends-existing', {
71+
test('Appends new parameters to existing parameters', async t => {
72+
const html = await process('<a href="https://example.com?s=test">Test</a>', {
5873
parameters: {foo: 'bar', baz: 'qux'}
5974
})
75+
76+
t.is(html, '<a href="https://example.com?baz=qux&foo=bar&s=test">Test</a>')
6077
})
6178

62-
test('Processes only tags provided in the `tags` option', t => {
63-
return process(t, 'tags', {
64-
tags: ['a[href*="example.com"]', 'link'],
65-
parameters: {foo: 'bar'}
66-
})
79+
test('Processes only tags provided in the `tags` option', async t => {
80+
const html = await process(
81+
`<a href="https://example.com">Test</a>
82+
<a href="https://skip.me">Skip</a>
83+
<link rel="stylesheet" href="https://example.com/style.css">
84+
<module href="https://example.com/header.html">`,
85+
{
86+
tags: ['a[href*="example.com"]', 'link'],
87+
parameters: {foo: 'bar'}
88+
}
89+
)
90+
91+
t.is(html, `<a href="https://example.com?foo=bar">Test</a>
92+
<a href="https://skip.me">Skip</a>
93+
<link rel="stylesheet" href="https://example.com/style.css?foo=bar">
94+
<module href="https://example.com/header.html"></module>`)
6795
})

0 commit comments

Comments
 (0)