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

Commit

Permalink
refactor: tests (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi authored Dec 19, 2018
1 parent 3981acc commit e5fe4e8
Show file tree
Hide file tree
Showing 28 changed files with 503 additions and 534 deletions.
3 changes: 1 addition & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ export default function loader(content) {
publicPath = JSON.stringify(publicPath);
}

// eslint-disable-next-line no-undefined
if (options.emitFile === undefined || options.emitFile) {
if (typeof options.emitFile === 'undefined' || options.emitFile) {
this.emitFile(outputPath, content);
}

Expand Down
21 changes: 0 additions & 21 deletions test/Errors.test.js

This file was deleted.

14 changes: 0 additions & 14 deletions test/__snapshots__/Errors.test.js.snap

This file was deleted.

3 changes: 3 additions & 0 deletions test/__snapshots__/context-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`when applied with \`context\` option matches snapshot for \`{String}\` value 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
3 changes: 3 additions & 0 deletions test/__snapshots__/emitFile-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`when applied with \`emitFile\` option matches snapshot for \`true\` (\`{Boolean}\`) (default) 1`] = `"64eababb117f90535085779cc0325003.svg"`;
8 changes: 8 additions & 0 deletions test/__snapshots__/errors.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`errors validation errors 1`] = `
"File Loader Invalid Options
options.useRelativePath should be boolean
"
`;
2 changes: 1 addition & 1 deletion test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Loader Defaults 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
exports[`loader should works without options 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
5 changes: 5 additions & 0 deletions test/__snapshots__/name-option.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`when applied with \`name\` option matches snapshot for \`{Function}\` value 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;

exports[`when applied with \`name\` option matches snapshot for \`{String}\` value 1`] = `"module.exports = __webpack_public_path__ + \\"9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";"`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options outputPath {Function} 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value 1`] = `
Object {
"assets": Array [
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -9,7 +9,7 @@ Object {
}
`;

exports[`Options outputPath {Function} with \`options.name\` 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value with \`options.name\` 1`] = `
Object {
"assets": Array [
"output_path_func/file.png",
Expand All @@ -18,7 +18,7 @@ Object {
}
`;

exports[`Options outputPath {Function} with \`publicPath\` {Function} 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value with \`publicPath\` (\`{Function}\`) 1`] = `
Object {
"assets": Array [
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -27,7 +27,7 @@ Object {
}
`;

exports[`Options outputPath {Function} with \`publicPath\` {String} 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{Function}\` value with \`publicPath\` (\`{String}\`) 1`] = `
Object {
"assets": Array [
"output_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -36,7 +36,7 @@ Object {
}
`;

exports[`Options outputPath {String} 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -45,25 +45,25 @@ Object {
}
`;

exports[`Options outputPath {String} with \`options.name\` 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value with \`publicPath\` (\`{Function}\`) 1`] = `
Object {
"assets": Array [
"output_path/file.png",
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = __webpack_public_path__ + \\"output_path/file.png\\";",
"source": "module.exports = \\"public_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} with \`publicPath\` {Function} 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value with \`publicPath\` (\`{String}\`) 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path_func/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} with \`publicPath\` {String} 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value with \`publicPath\` (\`{String}\`) without trailing slash 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -72,20 +72,20 @@ Object {
}
`;

exports[`Options outputPath {String} with \`publicPath\` {String} without trailing slash 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` value without trailing slash 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = __webpack_public_path__ + \\"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options outputPath {String} without trailing slash 1`] = `
exports[`when applied with \`outputPath\` option matches snapshot for \`{String}\` with \`name\` option 1`] = `
Object {
"assets": Array [
"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
"output_path/file.png",
],
"source": "module.exports = __webpack_public_path__ + \\"output_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = __webpack_public_path__ + \\"output_path/file.png\\";",
}
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options publicPath {Function} 1`] = `
exports[`when applied with \`publicPath\` option matches snapshot for \`{Function}\` value 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -9,25 +9,25 @@ Object {
}
`;

exports[`Options publicPath {String} - URL 1`] = `
exports[`when applied with \`publicPath\` option matches snapshot for \`{String}\` value 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options publicPath {String} - Without trailing slash 1`] = `
exports[`when applied with \`publicPath\` option matches snapshot for \`{String}\` value as URL 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
],
"source": "module.exports = \\"public_path/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
"source": "module.exports = \\"https://cdn.com/9c87cbf3ba33126ffd25ae7f2f6bbafb.png\\";",
}
`;

exports[`Options publicPath {String} 1`] = `
exports[`when applied with \`publicPath\` option matches snapshot for \`{String}\` value without trailing slash 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Options useRelativePath {Boolean} - \`false\` 1`] = `
exports[`when applied with \`useRelativePath\` option matches snapshot for \`false\` value (\`{Boolean}\`) 1`] = `
Object {
"assets": Array [
"9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -9,7 +9,7 @@ Object {
}
`;

exports[`Options useRelativePath {Boolean} - \`true\` 1`] = `
exports[`when applied with \`useRelativePath\` option matches snapshot for \`true\` value (\`{Boolean}\`) 1`] = `
Object {
"assets": Array [
"assets/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -18,7 +18,7 @@ Object {
}
`;

exports[`Options useRelativePath {Boolean} - \`true\` with absolute \`context\` 1`] = `
exports[`when applied with \`useRelativePath\` option matches snapshot for \`true\` value (\`{Boolean}\`) and with absolute \`context\` 1`] = `
Object {
"assets": Array [
"file-loader/test/fixtures/nested/assets/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand All @@ -27,7 +27,7 @@ Object {
}
`;

exports[`Options useRelativePath {Boolean} - \`true\` with relative \`context\` 1`] = `
exports[`when applied with \`useRelativePath\` option matches snapshot for \`true\` value (\`{Boolean}\`) and with relative \`context\` 1`] = `
Object {
"assets": Array [
"assets/9c87cbf3ba33126ffd25ae7f2f6bbafb.png",
Expand Down
20 changes: 20 additions & 0 deletions test/context-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import webpack from './helpers/compiler';

describe('when applied with `context` option', () => {
it('matches snapshot for `{String}` value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
context: `${__dirname}`,
},
},
};

const stats = await webpack('fixture.js', config);
const [module] = stats.toJson().modules;
const { source } = module;

expect(source).toMatchSnapshot();
});
});
38 changes: 38 additions & 0 deletions test/emitFile-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import webpack from './helpers/compiler';

describe('when applied with `emitFile` option', () => {
it('matches snapshot for `true` (`{Boolean}`) (default)', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
emitFile: true,
},
},
};

const stats = await webpack('emitFile/fixture.js', config);
const [, module] = stats.toJson().modules;
const { assets } = module;

expect(assets[0]).toMatchSnapshot();
});

it('matches snapshot for `false` value (`{Boolean}`)', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
emitFile: false,
},
},
};

const stats = await webpack('emitFile/fixture.js', config);
const [module] = stats.toJson().modules;
const { assets } = module;

// eslint-disable-next-line no-undefined
expect(assets[0]).toBe(undefined);
});
});
14 changes: 14 additions & 0 deletions test/errors.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import loader from '../src';

describe('errors', () => {
it('validation errors', () => {
const err = () =>
loader.call({
query: { useRelativePath: 1 },
emitFile: true,
});

expect(err).toThrow();
expect(err).toThrowErrorMatchingSnapshot();
});
});
4 changes: 2 additions & 2 deletions test/loader.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import webpack from './helpers/compiler';

describe('Loader', () => {
test('Defaults', async () => {
describe('loader', () => {
it('should works without options', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
Expand Down
39 changes: 39 additions & 0 deletions test/name-option.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import webpack from './helpers/compiler';

describe('when applied with `name` option', () => {
it('matches snapshot for `{String}` value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
name: '[hash].[ext]',
},
},
};

const stats = await webpack('fixture.js', config);
const [module] = stats.toJson().modules;
const { source } = module;

expect(source).toMatchSnapshot();
});

it('matches snapshot for `{Function}` value', async () => {
const config = {
loader: {
test: /(png|jpg|svg)/,
options: {
name() {
return '[hash].[ext]';
},
},
},
};

const stats = await webpack('fixture.js', config);
const [module] = stats.toJson().modules;
const { source } = module;

expect(source).toMatchSnapshot();
});
});
3 changes: 0 additions & 3 deletions test/options/__snapshots__/context.test.js.snap

This file was deleted.

3 changes: 0 additions & 3 deletions test/options/__snapshots__/emitFile.test.js.snap

This file was deleted.

5 changes: 0 additions & 5 deletions test/options/__snapshots__/name.test.js.snap

This file was deleted.

Loading

0 comments on commit e5fe4e8

Please sign in to comment.