-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add `exports.parseQuery` in `lib/index.js` - Separate `test/parseQuery.test.js` from `test/getOptions.test.js` - Add `parseQuery` in `README.md` - Fix: object is passed to loaderUtils.parseQuery in test - Change the title of the test and example code in README - In example code, replace `var`s with `const` #76 #77
- Loading branch information
Showing
4 changed files
with
120 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
"use strict"; | ||
|
||
const assert = require("assert"); | ||
const loaderUtils = require("../"); | ||
|
||
describe("parseQuery()", () => { | ||
describe("when passed string is a query string starting with ?", () => { | ||
[{ | ||
it: "should return an empty object by default", | ||
query: "?", | ||
expected: {} | ||
}, | ||
{ | ||
it: "should parse query params", | ||
query: "?name=cheesecake&slices=8&delicious&warm=false", | ||
expected: { | ||
delicious: true, | ||
name: "cheesecake", | ||
slices: "8", // numbers are still strings with query params | ||
warm: false | ||
} | ||
}, | ||
{ | ||
it: "should parse query params with arrays", | ||
query: "?ingredients[]=flour&ingredients[]=sugar", | ||
expected: { | ||
ingredients: ["flour", "sugar"] | ||
} | ||
}, | ||
{ | ||
it: "should parse query params in JSON format", | ||
query: "?" + JSON.stringify({ | ||
delicious: true, | ||
name: "cheesecake", | ||
slices: 8, | ||
warm: false | ||
}), | ||
expected: { | ||
delicious: true, | ||
name: "cheesecake", | ||
slices: 8, | ||
warm: false | ||
} | ||
}, | ||
{ | ||
it: "should use decodeURIComponent", | ||
query: "?%3d", | ||
expected: { "=": true } | ||
}, | ||
{ | ||
it: "should recognize params starting with + as boolean params with the value true", | ||
query: "?+%3d", | ||
expected: { "=": true } | ||
}, | ||
{ | ||
it: "should recognize params starting with - as boolean params with the value false", | ||
query: "?-%3d", | ||
expected: { "=": false } | ||
}, | ||
{ | ||
it: "should not confuse regular equal signs and encoded equal signs", | ||
query: "?%3d=%3D", | ||
expected: { "=": "=" } | ||
}].forEach(test => { | ||
it(test.it, () => { | ||
assert.deepEqual( | ||
loaderUtils.parseQuery(test.query), | ||
test.expected | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
describe("when passed string is any other string not starting with ?", () => { | ||
it("should throw an error", () => { | ||
assert.throws( | ||
() => loaderUtils.parseQuery("a"), | ||
"A valid query string passed to parseQuery should begin with '?'" | ||
); | ||
}); | ||
}); | ||
|
||
}); |