Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 903aa75

Browse files
committed
feat: added unit tests
added unit tests for option "prefixPublicPathWithWebpackPublicPath"
1 parent c6a68c7 commit 903aa75

File tree

2 files changed

+175
-0
lines changed

2 files changed

+175
-0
lines changed

test/__snapshots__/publicPath-option.test.js.snap

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value 1`] = `
4+
Object {
5+
"assets": Array [
6+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
7+
],
8+
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
9+
}
10+
`;
11+
12+
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value and pass \`context\` 1`] = `
13+
Object {
14+
"assets": Array [
15+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
16+
],
17+
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
18+
}
19+
`;
20+
21+
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value and pass \`resourcePath\` 1`] = `
22+
Object {
23+
"assets": Array [
24+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
25+
],
26+
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
27+
}
28+
`;
29+
30+
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value 1`] = `
31+
Object {
32+
"assets": Array [
33+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
34+
],
35+
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
36+
}
37+
`;
38+
39+
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value as URL 1`] = `
40+
Object {
41+
"assets": Array [
42+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
43+
],
44+
"source": "module.exports = __webpack_public_path__ + \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
45+
}
46+
`;
47+
48+
exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value without trailing slash 1`] = `
49+
Object {
50+
"assets": Array [
51+
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
52+
],
53+
"source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
54+
}
55+
`;
56+
357
exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value 1`] = `
458
Object {
559
"assets": Array [

test/publicPath-option.test.js

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,124 @@ describe('when applied with `publicPath` option', () => {
127127
expect({ assets, source }).toMatchSnapshot();
128128
});
129129
});
130+
131+
describe('when applied with `publicPath` and `prefixPublicPathWithWebpackPublicPath` options', () => {
132+
it('matches snapshot for `{String}` value', async () => {
133+
const config = {
134+
loader: {
135+
test: /(png|jpg|svg)/,
136+
options: {
137+
publicPath: 'public_path/',
138+
prefixPublicPathWithWebpackPublicPath: true,
139+
},
140+
},
141+
};
142+
143+
const stats = await webpack('fixture.js', config);
144+
const [module] = stats.toJson().modules;
145+
const { assets, source } = module;
146+
147+
expect({ assets, source }).toMatchSnapshot();
148+
});
149+
150+
it('matches snapshot for `{String}` value without trailing slash', async () => {
151+
const config = {
152+
loader: {
153+
test: /(png|jpg|svg)/,
154+
options: {
155+
publicPath: 'public_path',
156+
prefixPublicPathWithWebpackPublicPath: true,
157+
},
158+
},
159+
};
160+
161+
const stats = await webpack('fixture.js', config);
162+
const [module] = stats.toJson().modules;
163+
const { assets, source } = module;
164+
165+
expect({ assets, source }).toMatchSnapshot();
166+
});
167+
168+
// notice that this case will produce invalid urls if __webpack_public_path__ is set to an absolute url
169+
it('matches snapshot for `{String}` value as URL', async () => {
170+
const config = {
171+
loader: {
172+
test: /(png|jpg|svg)/,
173+
options: {
174+
publicPath: 'https://cdn.com/',
175+
prefixPublicPathWithWebpackPublicPath: true,
176+
},
177+
},
178+
};
179+
180+
const stats = await webpack('fixture.js', config);
181+
const [module] = stats.toJson().modules;
182+
const { assets, source } = module;
183+
184+
expect({ assets, source }).toMatchSnapshot();
185+
});
186+
187+
it('matches snapshot for `{Function}` value', async () => {
188+
const config = {
189+
loader: {
190+
test: /(png|jpg|svg)/,
191+
options: {
192+
publicPath(url) {
193+
return `public_path/${url}`;
194+
},
195+
prefixPublicPathWithWebpackPublicPath: true,
196+
},
197+
},
198+
};
199+
200+
const stats = await webpack('fixture.js', config);
201+
const [module] = stats.toJson().modules;
202+
const { assets, source } = module;
203+
204+
expect({ assets, source }).toMatchSnapshot();
205+
});
206+
207+
it('matches snapshot for `{Function}` value and pass `resourcePath`', async () => {
208+
const config = {
209+
loader: {
210+
test: /(png|jpg|svg)/,
211+
options: {
212+
publicPath(url, resourcePath) {
213+
expect(resourcePath).toMatch('file.png');
214+
215+
return `public_path/${url}`;
216+
},
217+
prefixPublicPathWithWebpackPublicPath: true,
218+
},
219+
},
220+
};
221+
222+
const stats = await webpack('fixture.js', config);
223+
const [module] = stats.toJson().modules;
224+
const { assets, source } = module;
225+
226+
expect({ assets, source }).toMatchSnapshot();
227+
});
228+
229+
it('matches snapshot for `{Function}` value and pass `context`', async () => {
230+
const config = {
231+
loader: {
232+
test: /(png|jpg|svg)/,
233+
options: {
234+
publicPath(url, resourcePath, context) {
235+
expect(context).toMatch('fixtures');
236+
237+
return `public_path/${url}`;
238+
},
239+
prefixPublicPathWithWebpackPublicPath: true,
240+
},
241+
},
242+
};
243+
244+
const stats = await webpack('fixture.js', config);
245+
const [module] = stats.toJson().modules;
246+
const { assets, source } = module;
247+
248+
expect({ assets, source }).toMatchSnapshot();
249+
});
250+
});

0 commit comments

Comments
 (0)