Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: the getOptions method returns empty object on empty query #167

Merged
merged 1 commit into from Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/getOptions.js
Expand Up @@ -11,7 +11,7 @@ function getOptions(loaderContext) {

if (!query || typeof query !== 'object') {
// Not object-like queries are not supported.
return null;
return {};
}

return query;
Expand Down
132 changes: 75 additions & 57 deletions test/getOptions.test.js
Expand Up @@ -3,62 +3,80 @@
const loaderUtils = require('../lib');

describe('getOptions()', () => {
describe('when loaderContext.query is a string with length > 0', () => {
it('should call parseQuery() and return its result', () => {
expect(
loaderUtils.getOptions({
query: '?something=getOptions_cannot_parse',
})
).toEqual({ something: 'getOptions_cannot_parse' });
});
});
describe('when loaderContext.query is an empty string', () => {
it('should return null', () => {
expect(
loaderUtils.getOptions({
query: '',
})
).toEqual(null);
});
});
describe('when loaderContext.query is an object', () => {
it('should just return it', () => {
const query = {};
expect(
loaderUtils.getOptions({
query,
})
).toEqual(query);
});
});
describe('when loaderContext.query is an array', () => {
it('should just return it', () => {
const query = [];
expect(loaderUtils.getOptions({ query })).toEqual(query);
});
});
describe('when loaderContext.query is anything else', () => {
it('should return null', () => {
expect(
loaderUtils.getOptions({
query: undefined,
})
).toEqual(null);
expect(
loaderUtils.getOptions({
query: null,
})
).toEqual(null);
expect(
loaderUtils.getOptions({
query: 1,
})
).toEqual(null);
expect(
loaderUtils.getOptions({
query: 0,
})
).toEqual(null);
});
it('should work', () => {
expect(
loaderUtils.getOptions({
query: true,
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: false,
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: null,
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: undefined,
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: 1,
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: 0,
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: -0,
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: -1,
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: '',
})
).toEqual({});
expect(
loaderUtils.getOptions({
query: '?something=getOptions_cannot_parse',
})
).toEqual({ something: 'getOptions_cannot_parse' });

const query1 = {};

expect(
loaderUtils.getOptions({
query: query1,
})
).toEqual(query1);

const query2 = { foo: { bar: 'baz' } };

expect(
loaderUtils.getOptions({
query: query2,
})
).toEqual(query2);

const query3 = [];

expect(loaderUtils.getOptions({ query: query3 })).toEqual(query3);

const query4 = [1, true, 'foobar'];

expect(loaderUtils.getOptions({ query: query4 })).toEqual(query4);
});
});