Skip to content

Commit

Permalink
fix(connectRefinementList): throw error with usage (#2962)
Browse files Browse the repository at this point in the history
* test(connectRefinementList): add a pattern for the error
* fix(connectRefinementList): throw with the usage message
* style(connectRefinementList): add spaces in the usage message
  • Loading branch information
samouss authored and bobylito committed Jun 21, 2018
1 parent 7cc82f2 commit f60222d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,28 @@ describe('connectRefinementList', () => {
};

it('throws on bad usage', () => {
expect(connectRefinementList).toThrow();
expect(connectRefinementList).toThrow(/Usage:/);

expect(() =>
connectRefinementList({
operator: 'and',
})
).toThrow();
).toThrow(/Usage:/);

expect(() => connectRefinementList(() => {})()).toThrow();
expect(() => connectRefinementList(() => {})()).toThrow(/Usage:/);

expect(() =>
connectRefinementList(() => {})({
operator: 'and',
})
).toThrow();
).toThrow(/Usage:/);

expect(() =>
connectRefinementList(() => {})({
attributeName: 'company',
operator: 'YUP',
})
).toThrow();
).toThrow(/Usage:/);
});

describe('options configuring the helper', () => {
Expand Down
14 changes: 11 additions & 3 deletions src/connectors/refinement-list/connectRefinementList.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var customRefinementList = connectRefinementList(function render(params) {
// widgetParams,
// }
});
search.addWidget(
customRefinementList({
attributeName,
Expand All @@ -27,15 +28,16 @@ search.addWidget(
[ escapeFacetValues = false ]
})
);
Full documentation available at https://community.algolia.com/instantsearch.js/v2/connectors/connectRefinementList.html
`;

export const checkUsage = ({
attributeName,
operator,
usageMessage,
showMoreLimit,
limit,
message,
}) => {
const noAttributeName = attributeName === undefined;
const invalidOperator = !/^(and|or)$/.test(operator);
Expand All @@ -45,7 +47,7 @@ export const checkUsage = ({
: false;

if (noAttributeName || invalidOperator || invalidShowMoreLimit) {
throw new Error(usageMessage);
throw new Error(message);
}
};

Expand Down Expand Up @@ -159,7 +161,13 @@ export default function connectRefinementList(renderFn, unmountFn) {
escapeFacetValues = false,
} = widgetParams;

checkUsage({ attributeName, operator, usage, limit, showMoreLimit });
checkUsage({
message: usage,
attributeName,
operator,
showMoreLimit,
limit,
});

const formatItems = ({ name: label, ...item }) => ({
...item,
Expand Down

0 comments on commit f60222d

Please sign in to comment.