Skip to content

Commit 301c26f

Browse files
test: refactor (#218)
1 parent a80fc7e commit 301c26f

File tree

5 files changed

+142
-124
lines changed

5 files changed

+142
-124
lines changed

test/attributes-option.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import loader from '../src';
22
import { GET_URL_CODE } from '../src/constants';
33

44
describe("'attributes' option", () => {
5+
it('should work by default', () => {
6+
const result = loader.call(
7+
{ mode: 'development' },
8+
'Text <img src="image.png"><img src="~bootstrap-img"> Text <img src="">'
9+
);
10+
11+
expect(result).toBe(
12+
// eslint-disable-next-line no-useless-escape
13+
`${GET_URL_CODE}module.exports = "Text <img src=\\"" + __url__(require("./image.png")) + "\\"><img src=\\"" + __url__(require("bootstrap-img")) + "\\"> Text <img src=\\\"\\\">";`
14+
);
15+
});
16+
517
it('should work with a "string" notation', () => {
618
const result = loader.call(
719
{
@@ -99,4 +111,26 @@ describe("'attributes' option", () => {
99111
`${GET_URL_CODE}module.exports = "Text <script src=\\"script.js\\"><img src=\\"" + __url__(require("./image.png")) + "\\">";`
100112
);
101113
});
114+
115+
it('should ignore hash fragments in URLs', () => {
116+
const result = loader.call(
117+
{ mode: 'development' },
118+
'<img src="icons.svg#hash">'
119+
);
120+
121+
expect(result).toBe(
122+
`${GET_URL_CODE}module.exports = "<img src=\\"" + __url__(require("./icons.svg")) + "#hash\\">";`
123+
);
124+
});
125+
126+
it('should ignore some anchor by default in attributes', () => {
127+
const result = loader.call(
128+
{ mode: 'development' },
129+
'<a href="mailto:username@exampledomain.com"></a>'
130+
);
131+
132+
expect(result).toBe(
133+
`${GET_URL_CODE}module.exports = "<a href=\\"mailto:username@exampledomain.com\\"></a>";`
134+
);
135+
});
102136
});

test/esModule-option.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import loader from '../src';
2+
import { GET_URL_CODE } from '../src/constants';
3+
4+
describe("'esModule' option", () => {
5+
it('should use a CommonJS export by default', () => {
6+
const result = loader.call({ query: '' }, '<p>Hello world!</p>');
7+
8+
expect(result).toBe(
9+
`${GET_URL_CODE}module.exports = "<p>Hello world!</p>";`
10+
);
11+
});
12+
13+
it('should use a CommonJS export when the value is "false"', () => {
14+
const result = loader.call(
15+
{ query: '?esModule=false' },
16+
'<p>Hello world!</p>'
17+
);
18+
19+
expect(result).toBe(
20+
`${GET_URL_CODE}module.exports = "<p>Hello world!</p>";`
21+
);
22+
});
23+
24+
it('should use an ES module export when the value is "true"', () => {
25+
const result = loader.call(
26+
{ query: '?esModule=true' },
27+
'<p>Hello world!</p>'
28+
);
29+
30+
expect(result).toBe(`${GET_URL_CODE}export default "<p>Hello world!</p>";`);
31+
});
32+
});

test/interpolate-option.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import loader from '../src';
2+
import { GET_URL_CODE } from '../src/constants';
3+
4+
describe("'interpolate' option", () => {
5+
it('should disabled by default', () => {
6+
const result = loader.call(
7+
{ mode: 'development' },
8+
// eslint-disable-next-line no-template-curly-in-string
9+
'<img src="${"Hello " + (1+1)}">'
10+
);
11+
12+
expect(result).toBe(
13+
`${GET_URL_CODE}module.exports = "<img src=\\"\${\\"Hello \\" + (1+1)}\\">";`
14+
);
15+
});
16+
17+
it('should work with boolean notation', () => {
18+
const result = loader.call(
19+
{
20+
mode: 'development',
21+
query: '?interpolate',
22+
},
23+
// eslint-disable-next-line no-template-curly-in-string
24+
'<img src="${"Hello " + (1+1)}"><img src="${`Hello ` + (1+1)}"><p>Something about the \\` character</p><script>{"json": "with \\"quotes\\" in value"}</script>'
25+
);
26+
27+
expect(result).toBe(
28+
// eslint-disable-next-line no-useless-escape
29+
`${GET_URL_CODE}module.exports = "<img src=\\"" + ("Hello " + (1 + 1)) + "\\"><img src=\\"" + ("Hello " + (1 + 1)) + "\\"><p>Something about the \` character</p><script>{\\\"json\\\": \\\"with \\\\\\\"quotes\\\\\\\" in value\\\"}</script>";`
30+
);
31+
});
32+
33+
it('should work with the "require"', () => {
34+
const result = loader.call(
35+
{
36+
mode: 'development',
37+
query: '?interpolate=require',
38+
},
39+
// eslint-disable-next-line no-template-curly-in-string
40+
'<a href="${list.href}"><img src="${require("./test.jpg")}" /></a>'
41+
);
42+
43+
expect(result).toBe(
44+
`${GET_URL_CODE}module.exports = "<a href=\\"\${list.href}\\"><img src=\\"" + __url__(require("./test.jpg")) + "\\" /></a>";`
45+
);
46+
});
47+
});

test/loader.test.js

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,7 @@
1-
/* eslint-disable no-useless-escape,no-template-curly-in-string */
2-
31
import loader from '../src';
42
import { GET_URL_CODE } from '../src/constants';
53

64
describe('loader', () => {
7-
it('should convert to requires', () => {
8-
const result = loader.call(
9-
{ mode: 'development' },
10-
'Text <img src="image.png"><img src="~bootstrap-img"> Text <img src="">'
11-
);
12-
13-
expect(result).toBe(
14-
`${GET_URL_CODE}module.exports = "Text <img src=\\"" + __url__(require("./image.png")) + "\\"><img src=\\"" + __url__(require("bootstrap-img")) + "\\"> Text <img src=\\\"\\\">";`
15-
);
16-
});
17-
185
it('should not make bad things with templates', () => {
196
const result = loader.call(
207
{ mode: 'development' },
@@ -36,115 +23,4 @@ describe('loader', () => {
3623
`${GET_URL_CODE}module.exports = "<script>{\\"json\\": \\"with \\\\\\"quotes\\\\\\" in value\\"}</script>";`
3724
);
3825
});
39-
40-
it('should not translate root-relative urls (without root query)', () => {
41-
const result = loader.call(
42-
{ mode: 'development' },
43-
'Text <img src="/image.png">'
44-
);
45-
46-
expect(result).toBe(
47-
`${GET_URL_CODE}module.exports = "Text <img src=\\"/image.png\\">";`
48-
);
49-
});
50-
51-
it('should accept root from query', () => {
52-
const result = loader.call(
53-
{
54-
mode: 'development',
55-
query: '?root=/test',
56-
},
57-
'Text <img src="/image.png">'
58-
);
59-
60-
expect(result).toBe(
61-
`${GET_URL_CODE}module.exports = "Text <img src=\\"" + __url__(require("/test/image.png")) + "\\">";`
62-
);
63-
});
64-
65-
it('should ignore hash fragments in URLs', () => {
66-
const result = loader.call(
67-
{ mode: 'development' },
68-
'<img src="icons.svg#hash">'
69-
);
70-
71-
expect(result).toBe(
72-
`${GET_URL_CODE}module.exports = "<img src=\\"" + __url__(require("./icons.svg")) + "#hash\\">";`
73-
);
74-
});
75-
76-
it("should ignore anchor with 'mailto:' in the href attribute", () => {
77-
const result = loader.call(
78-
{ mode: 'development' },
79-
'<a href="mailto:username@exampledomain.com"></a>'
80-
);
81-
82-
expect(result).toBe(
83-
`${GET_URL_CODE}module.exports = "<a href=\\"mailto:username@exampledomain.com\\"></a>";`
84-
);
85-
});
86-
87-
it('should ignore interpolations by default', () => {
88-
const result = loader.call(
89-
{ mode: 'development' },
90-
'<img src="${"Hello " + (1+1)}">'
91-
);
92-
93-
expect(result).toBe(
94-
`${GET_URL_CODE}module.exports = "<img src=\\"\${\\"Hello \\" + (1+1)}\\">";`
95-
);
96-
});
97-
98-
it('should enable interpolations when using interpolate flag', () => {
99-
const result = loader.call(
100-
{
101-
mode: 'development',
102-
query: '?interpolate',
103-
},
104-
'<img src="${"Hello " + (1+1)}">'
105-
);
106-
107-
expect(result).toBe(
108-
`${GET_URL_CODE}module.exports = "<img src=\\"" + ("Hello " + (1 + 1)) + "\\">";`
109-
);
110-
});
111-
112-
it('should not change handling of quotes when interpolation is enabled', () => {
113-
const result = loader.call(
114-
{
115-
mode: 'development',
116-
query: '?interpolate',
117-
},
118-
'<script>{"json": "with \\"quotes\\" in value"}</script>'
119-
);
120-
121-
expect(result).toBe(
122-
`${GET_URL_CODE}module.exports = "<script>{\\\"json\\\": \\\"with \\\\\\\"quotes\\\\\\\" in value\\\"}</script>";`
123-
);
124-
});
125-
126-
it('should enable interpolations when using interpolate=require flag and only require function be translate', () => {
127-
const result = loader.call(
128-
{
129-
mode: 'development',
130-
query: '?interpolate=require',
131-
},
132-
'<a href="${list.href}"><img src="${require("./test.jpg")}" /></a>'
133-
);
134-
135-
expect(result).toBe(
136-
`${GET_URL_CODE}module.exports = "<a href=\\"\${list.href}\\"><img src=\\"" + __url__(require("./test.jpg")) + "\\" /></a>";`
137-
);
138-
});
139-
140-
it('should export as es6 default export', () => {
141-
const result = loader.call(
142-
{
143-
query: '?esModule',
144-
},
145-
'<p>Hello world!</p>'
146-
);
147-
148-
expect(result).toBe(`${GET_URL_CODE}export default "<p>Hello world!</p>";`);
149-
});
15026
});

test/root-options.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import loader from '../src';
2+
import { GET_URL_CODE } from '../src/constants';
3+
4+
describe("'root' option", () => {
5+
it('should not translate root-relative urls by default', () => {
6+
const result = loader.call(
7+
{ mode: 'development' },
8+
'Text <img src="/image.png">'
9+
);
10+
11+
expect(result).toBe(
12+
`${GET_URL_CODE}module.exports = "Text <img src=\\"/image.png\\">";`
13+
);
14+
});
15+
16+
it('should work', () => {
17+
const result = loader.call(
18+
{
19+
mode: 'development',
20+
query: '?root=/test',
21+
},
22+
'Text <img src="/image.png">'
23+
);
24+
25+
expect(result).toBe(
26+
`${GET_URL_CODE}module.exports = "Text <img src=\\"" + __url__(require("/test/image.png")) + "\\">";`
27+
);
28+
});
29+
});

0 commit comments

Comments
 (0)