Skip to content

Commit b595cfb

Browse files
refactor: the getOptions method returns empty object on empty query (#167)
BREAKING CHANGE: the `getOptions` method returns empty object on empty query
1 parent c937e8c commit b595cfb

File tree

2 files changed

+76
-58
lines changed

2 files changed

+76
-58
lines changed

lib/getOptions.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function getOptions(loaderContext) {
1111

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

1717
return query;

test/getOptions.test.js

+75-57
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,80 @@
33
const loaderUtils = require('../lib');
44

55
describe('getOptions()', () => {
6-
describe('when loaderContext.query is a string with length > 0', () => {
7-
it('should call parseQuery() and return its result', () => {
8-
expect(
9-
loaderUtils.getOptions({
10-
query: '?something=getOptions_cannot_parse',
11-
})
12-
).toEqual({ something: 'getOptions_cannot_parse' });
13-
});
14-
});
15-
describe('when loaderContext.query is an empty string', () => {
16-
it('should return null', () => {
17-
expect(
18-
loaderUtils.getOptions({
19-
query: '',
20-
})
21-
).toEqual(null);
22-
});
23-
});
24-
describe('when loaderContext.query is an object', () => {
25-
it('should just return it', () => {
26-
const query = {};
27-
expect(
28-
loaderUtils.getOptions({
29-
query,
30-
})
31-
).toEqual(query);
32-
});
33-
});
34-
describe('when loaderContext.query is an array', () => {
35-
it('should just return it', () => {
36-
const query = [];
37-
expect(loaderUtils.getOptions({ query })).toEqual(query);
38-
});
39-
});
40-
describe('when loaderContext.query is anything else', () => {
41-
it('should return null', () => {
42-
expect(
43-
loaderUtils.getOptions({
44-
query: undefined,
45-
})
46-
).toEqual(null);
47-
expect(
48-
loaderUtils.getOptions({
49-
query: null,
50-
})
51-
).toEqual(null);
52-
expect(
53-
loaderUtils.getOptions({
54-
query: 1,
55-
})
56-
).toEqual(null);
57-
expect(
58-
loaderUtils.getOptions({
59-
query: 0,
60-
})
61-
).toEqual(null);
62-
});
6+
it('should work', () => {
7+
expect(
8+
loaderUtils.getOptions({
9+
query: true,
10+
})
11+
).toEqual({});
12+
expect(
13+
loaderUtils.getOptions({
14+
query: false,
15+
})
16+
).toEqual({});
17+
expect(
18+
loaderUtils.getOptions({
19+
query: null,
20+
})
21+
).toEqual({});
22+
expect(
23+
loaderUtils.getOptions({
24+
query: undefined,
25+
})
26+
).toEqual({});
27+
expect(
28+
loaderUtils.getOptions({
29+
query: 1,
30+
})
31+
).toEqual({});
32+
expect(
33+
loaderUtils.getOptions({
34+
query: 0,
35+
})
36+
).toEqual({});
37+
expect(
38+
loaderUtils.getOptions({
39+
query: -0,
40+
})
41+
).toEqual({});
42+
expect(
43+
loaderUtils.getOptions({
44+
query: -1,
45+
})
46+
).toEqual({});
47+
expect(
48+
loaderUtils.getOptions({
49+
query: '',
50+
})
51+
).toEqual({});
52+
expect(
53+
loaderUtils.getOptions({
54+
query: '?something=getOptions_cannot_parse',
55+
})
56+
).toEqual({ something: 'getOptions_cannot_parse' });
57+
58+
const query1 = {};
59+
60+
expect(
61+
loaderUtils.getOptions({
62+
query: query1,
63+
})
64+
).toEqual(query1);
65+
66+
const query2 = { foo: { bar: 'baz' } };
67+
68+
expect(
69+
loaderUtils.getOptions({
70+
query: query2,
71+
})
72+
).toEqual(query2);
73+
74+
const query3 = [];
75+
76+
expect(loaderUtils.getOptions({ query: query3 })).toEqual(query3);
77+
78+
const query4 = [1, true, 'foobar'];
79+
80+
expect(loaderUtils.getOptions({ query: query4 })).toEqual(query4);
6381
});
6482
});

0 commit comments

Comments
 (0)