-
Notifications
You must be signed in to change notification settings - Fork 8
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
yield setHook function #186
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import { render } from '@ember/test-helpers'; | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { modifier } from 'ember-modifier'; | ||
|
||
import { findElement, resetTestingContainerDimensions, styleFor } from '../velcro-test-helpers'; | ||
|
||
|
@@ -37,6 +38,35 @@ module('Integration | Component | velcro', function (hooks) { | |
}); | ||
}); | ||
|
||
test('it renders with setHook', async function (assert) { | ||
let hookModifier = modifier((element, [setHook]) => { | ||
setHook(element); | ||
}); | ||
|
||
|
||
await render(<template> | ||
<Velcro as |velcro|> | ||
<div id="hook" {{hookModifier velcro.setHook}} style="width: 200px; height: 40px"> | ||
{{velcro.data.rects.reference.width}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Can you update the readme as to when you'd want to use setHook? While the PR description is helpful, I can't quite see how another modifier would make use of this. Also the test doesn't show much other than it renders. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The test shows that it renders and that the content was correctly positioned and sized. Which means that the
It would be a contrived example, but I'll explain my use case: Imagine you're writing a dropdown component with ember-velcro. You want to yield a
Without The dropdown component might look something like this: let myModifier = modifier((element, [setHook, handler]) => {
// call ember-velcro's setHook
setHook(element);
// other custom logic
element.addEventListener('click', handler);
return () => {
element.removeEventListener('click', handler);
};
});
<template>
<Velcro as |velcro|>
{{yield (hash
trigger=(modifier myModifier velcro.setHook onClick)
)}}
</Velcro>
</template> This is needed because there's no way in ember (that I know of) to combine two existing modifiers into a single one. If there was, this PR wouldn't be needed. Being this such an edge case, I'm not sure if we should document it with this detail. It might distract users from the main usage. Maybe adding this in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't consider this an edge case -- in design systems, this sort of problem is IMMENSELY common -- it's a composibilitiy design challenge There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nicolechung documentation was added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think (hope?) that should be enough for the tests. What I meant is that a user can't tell why they'd need to use The documentation looks terrific, thanks for adding that! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like when tests self-document the usage of something. This test passes the setHook into a custom modifier like it is intended to be used. |
||
{{velcro.data.rects.reference.height}} | ||
</div> | ||
<div id="loop" {{velcro.loop}} style="width: 200px; height: 400px"> | ||
{{velcro.data.rects.floating.width}} | ||
{{velcro.data.rects.floating.height}} | ||
</div> | ||
</Velcro> | ||
</template>); | ||
|
||
assert.dom('#hook').hasText('200 40', 'reference element has expected dimensions'); | ||
assert.dom('#loop').hasText('200 400', 'floating element has expected dimensions'); | ||
assert.dom('#loop').hasAttribute('style'); | ||
assert.dom('#loop').hasStyle({ | ||
position: 'fixed', | ||
top: '40px', | ||
left: '0px', | ||
}); | ||
}); | ||
|
||
module('@middleware', function () { | ||
test('it yields the MiddlewareArguments', async function (assert) { | ||
await render(<template> | ||
|
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.
This is just to make vscode glint extension happy and correctly detect gts files in sub packages.