From 903aa7544f7c25d524e25a7dc171e9d4d6f80980 Mon Sep 17 00:00:00 2001 From: Fabian Ehrentraud Date: Wed, 12 Jun 2019 08:45:05 +0200 Subject: [PATCH] feat: added unit tests added unit tests for option "prefixPublicPathWithWebpackPublicPath" --- .../publicPath-option.test.js.snap | 54 ++++++++ test/publicPath-option.test.js | 121 ++++++++++++++++++ 2 files changed, 175 insertions(+) diff --git a/test/__snapshots__/publicPath-option.test.js.snap b/test/__snapshots__/publicPath-option.test.js.snap index 676eeba..d39e529 100644 --- a/test/__snapshots__/publicPath-option.test.js.snap +++ b/test/__snapshots__/publicPath-option.test.js.snap @@ -1,5 +1,59 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value 1`] = ` +Object { + "assets": Array [ + "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", + ], + "source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", +} +`; + +exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value and pass \`context\` 1`] = ` +Object { + "assets": Array [ + "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", + ], + "source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", +} +`; + +exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{Function}\` value and pass \`resourcePath\` 1`] = ` +Object { + "assets": Array [ + "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", + ], + "source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", +} +`; + +exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value 1`] = ` +Object { + "assets": Array [ + "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", + ], + "source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", +} +`; + +exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value as URL 1`] = ` +Object { + "assets": Array [ + "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", + ], + "source": "module.exports = __webpack_public_path__ + \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", +} +`; + +exports[`when applied with \`publicPath\` and \`prefixPublicPathWithWebpackPublicPath\` options matches snapshot for \`{String}\` value without trailing slash 1`] = ` +Object { + "assets": Array [ + "9c87cbf3ba33126ffd25ae7f2f6bbafb.png", + ], + "source": "module.exports = __webpack_public_path__ + \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";", +} +`; + exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value 1`] = ` Object { "assets": Array [ diff --git a/test/publicPath-option.test.js b/test/publicPath-option.test.js index 6980fb2..01e7616 100644 --- a/test/publicPath-option.test.js +++ b/test/publicPath-option.test.js @@ -127,3 +127,124 @@ describe('when applied with `publicPath` option', () => { expect({ assets, source }).toMatchSnapshot(); }); }); + +describe('when applied with `publicPath` and `prefixPublicPathWithWebpackPublicPath` options', () => { + it('matches snapshot for `{String}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath: 'public_path/', + prefixPublicPathWithWebpackPublicPath: true, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{String}` value without trailing slash', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath: 'public_path', + prefixPublicPathWithWebpackPublicPath: true, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + // notice that this case will produce invalid urls if __webpack_public_path__ is set to an absolute url + it('matches snapshot for `{String}` value as URL', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath: 'https://cdn.com/', + prefixPublicPathWithWebpackPublicPath: true, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath(url) { + return `public_path/${url}`; + }, + prefixPublicPathWithWebpackPublicPath: true, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value and pass `resourcePath`', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath(url, resourcePath) { + expect(resourcePath).toMatch('file.png'); + + return `public_path/${url}`; + }, + prefixPublicPathWithWebpackPublicPath: true, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); + + it('matches snapshot for `{Function}` value and pass `context`', async () => { + const config = { + loader: { + test: /(png|jpg|svg)/, + options: { + publicPath(url, resourcePath, context) { + expect(context).toMatch('fixtures'); + + return `public_path/${url}`; + }, + prefixPublicPathWithWebpackPublicPath: true, + }, + }, + }; + + const stats = await webpack('fixture.js', config); + const [module] = stats.toJson().modules; + const { assets, source } = module; + + expect({ assets, source }).toMatchSnapshot(); + }); +});