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

Commit

Permalink
feat: added unit tests
Browse files Browse the repository at this point in the history
added unit tests for option "prefixPublicPathWithWebpackPublicPath"
  • Loading branch information
fabb committed Jun 12, 2019
1 parent c6a68c7 commit 903aa75
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 0 deletions.
54 changes: 54 additions & 0 deletions test/__snapshots__/publicPath-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -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 [
Expand Down
121 changes: 121 additions & 0 deletions test/publicPath-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 903aa75

Please sign in to comment.