-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Adding resetOnSelect behavior to created item #4108
Adding resetOnSelect behavior to created item #4108
Conversation
Thanks for your interest in palantir/blueprint, @alexisvisco! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
This seems like a reasonable change, would you mind adding a unit test @alexisvisco? FYSA @cmslewis |
@alexisvisco Are you seeing this bug in any of the higher-order components that use |
This bug is in all components that use @cmslewis instead of a repo I have made a video that demonstrate the change between the actual production blueprint and my change https://youtu.be/C2m8WvYxpqw Can you give me some hint to create tests please ? I don't known how to trigger a item selection / creation. I have looked your tests in the Thanks you for your reviewing. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RE: Testing: You can add tests in the "query"
describe block here. I'll let you commit them yourself so you can tinker, but I might try something like this:
it("resets the query after creating new item if resetOnSelect=true", () => {
const onQueryChangeSpy = runResetOnSelectTest(true);
assert.isTrue(onQueryChangeSpy.calledWith(""));
});
it("does not reset the query after creating new item if resetOnSelect=false", () => {
const onQueryChangeSpy = runResetOnSelectTest(false);
assert.isTrue(onQueryChangeSpy.notCalled);
});
function runResetOnSelectTest(resetOnSelect: boolean): sinon.SinonSpy {
let triggerItemCreate: ((e: any) => void) | undefined;
const onQueryChangeSpy = sinon.spy();
// supply a custom renderer so we can hook into handleClick and invoke it ourselves later
const createNewItemRenderer = sinon.spy(
(_query: string, _active: boolean, handleClick: React.MouseEventHandler<HTMLElement>) => {
triggerItemCreate = handleClick;
return <div />;
},
);
const queryList = shallow(
<FilmQueryList
{...testProps}
// Must return something in order for item creation to work.
createNewItemFromQuery={() => ({ title: "irrelevant", rank: 0, year: 0 })}
createNewItemRenderer={createNewItemRenderer}
onQueryChange={onQueryChangeSpy}
resetOnSelect={resetOnSelect}
/>,
);
// Change the query to something non-empty so we can ensure it wasn't cleared.
// Ignore this change in the spy.
(queryList.instance() as QueryList<IFilm>).setQuery("some query");
onQueryChangeSpy.resetHistory();
assert.isDefined(triggerItemCreate, "query list should pass click handler to createNewItemRenderer");
triggerItemCreate!({});
return onQueryChangeSpy;
}
Let me know if that's not testing what you're trying to fix. To run these, you can cd
into packages/select
, slap an it.only
on the tests you care about in order to run those only, and then run yarn test:karma:debug
from your terminal to re-run tests every time you save.
@@ -407,7 +407,9 @@ export class QueryList<T> extends AbstractComponent2<IQueryListProps<T>, IQueryL | |||
const item = Utils.safeInvoke(this.props.createNewItemFromQuery, query); | |||
if (item != null) { | |||
Utils.safeInvoke(this.props.onItemSelect, item, evt); | |||
this.setQuery("", true); | |||
if (this.props.resetOnSelect) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Since this appears on L419 as well, it might be nice to put this in a maybeResetQuery
helper (after all the handle-
function definitions, e.g. around L500).
private maybeResetQuery() {
if (this.props.resetOnSelect) {
this.setQuery("", true);
}
}
That'd help avoid something like this happening again.
@alexisvisco sorry I didn't mention the new |
friendly bump on this @alexisvisco |
I don't understand, the yarn format that I am running don't indicate any errors ... Can you help me ? |
…m-create # Conflicts: # packages/select/test/queryListTests.tsx
merged develop into your branch. here's the latest docs preview build |
Fixes #4103
After investigating the source code I found an issue with the
resetOnSelect
when there is an item creation. The current behavior when an item is created is by default reset the query event ifresetOnSelect
is set to falseChanges proposed in this pull request:
This PR change the behavior when an item is created and if
resetOnSelect
is true.If
resetOnSelect
is false the query is not reset.The code change is minimal so the review should be fast :)