chore(button): fix failing tests

This commit is contained in:
Joren Broekema 2019-09-23 12:55:34 +02:00 committed by erik
parent a5752fcb6d
commit aa8bfc626d
3 changed files with 46 additions and 48 deletions

View file

@ -71,6 +71,12 @@ export class LionButton extends DisabledWithTabIndexMixin(SlotMixin(LitElement))
:host .btn ::slotted(button) { :host .btn ::slotted(button) {
position: absolute; position: absolute;
clip: rect(0 0 0 0);
clip-path: inset(50%);
overflow: hidden;
white-space: nowrap;
height: 1px;
width: 1px;
} }
.click-area { .click-area {
@ -110,22 +116,6 @@ export class LionButton extends DisabledWithTabIndexMixin(SlotMixin(LitElement))
fill: #adadad; fill: #adadad;
} }
`, `,
...(this.__isIE11() /* visibility hidden in IE11 breaks native button functionality */
? [
/* TODO: Make SR-only css mixin? */
css`
:host .btn ::slotted(button) {
clip: rect(0, 0, 0, 0);
}
`,
]
: [
css`
:host .btn ::slotted(button) {
visibility: hidden;
}
`,
]),
]; ];
} }
@ -181,33 +171,16 @@ export class LionButton extends DisabledWithTabIndexMixin(SlotMixin(LitElement))
} }
} }
_redispatchClickEvent(oldEvent) {
// replacing `MouseEvent` with `oldEvent.constructor` breaks IE
const newEvent = new MouseEvent(oldEvent.type, oldEvent);
newEvent.__isRedispatchedOnNativeButton = true;
this.__enforceHostEventTarget(newEvent);
this._nativeButtonNode.dispatchEvent(newEvent);
}
/** /**
* Prevent normal click and redispatch submit on the native button unless already redispatched. * Dispatch submit event and invoke submit on the native form when clicked
*/ */
__clickDelegationHandler() { __clickDelegationHandler() {
if (this.type === 'submit' && this._nativeButtonNode.form) { if (this.type === 'submit' && this._nativeButtonNode.form) {
this._nativeButtonNode.form.dispatchEvent(new Event('submit'));
this._nativeButtonNode.form.submit(); this._nativeButtonNode.form.submit();
} }
} }
__enforceHostEventTarget(event) {
try {
// this is for IE11 (and works in others), because `Object.defineProperty` does not give any effect there
event.__defineGetter__('target', () => this); // eslint-disable-line no-restricted-properties
} catch (error) {
// in case `__defineGetter__` is removed from the platform
Object.defineProperty(event, 'target', { writable: false, value: this });
}
}
__setupDelegationInConstructor() { __setupDelegationInConstructor() {
// do not move to connectedCallback, otherwise IE11 breaks // do not move to connectedCallback, otherwise IE11 breaks
// more info: https://github.com/ing-bank/lion/issues/179#issuecomment-511763835 // more info: https://github.com/ing-bank/lion/issues/179#issuecomment-511763835
@ -252,7 +225,7 @@ export class LionButton extends DisabledWithTabIndexMixin(SlotMixin(LitElement))
__keyupHandler(e) { __keyupHandler(e) {
if (this.__isKeyboardClickEvent(e)) { if (this.__isKeyboardClickEvent(e)) {
// redispatch click // redispatch click
this.shadowRoot.querySelector('.click-area').click(); this.click();
} }
} }

View file

@ -35,16 +35,14 @@ storiesOf('Buttons|Button', module)
.add( .add(
'Within a form', 'Within a form',
() => html` () => html`
<lion-form id="form" @submit=${() => console.log('form submitted')} <form @submit=${() => console.log('native form submitted')}>
><form> <input name="foo" label="Foo" .modelValue=${'bar'} />
<lion-input name="foo" label="Foo" .modelValue=${'bar'}></lion-input> <input name="foo2" label="Foo2" .modelValue=${'bar'} />
<lion-input name="foo2" label="Foo2" .modelValue=${'bar'}></lion-input>
<lion-button <lion-button
type="submit" type="submit"
@click=${() => console.log(document.querySelector('#form').serializeGroup())} @click=${() => console.log(document.querySelector('#form').serializeGroup())}
>Submit</lion-button >Submit</lion-button
> >
</form></lion-form </form>
>
`, `,
); );

View file

@ -57,7 +57,8 @@ describe('lion-button', () => {
it('hides the native button in the UI', async () => { it('hides the native button in the UI', async () => {
const el = await fixture(`<lion-button>foo</lion-button>`); const el = await fixture(`<lion-button>foo</lion-button>`);
expect(el._nativeButtonNode.getAttribute('tabindex')).to.equal('-1'); expect(el._nativeButtonNode.getAttribute('tabindex')).to.equal('-1');
expect(window.getComputedStyle(el._nativeButtonNode).visibility).to.equal('hidden'); // TODO: If we abstract to an srOnlyMixin, we should test that the styling equals that of the srOnlyMixin output
expect(window.getComputedStyle(el._nativeButtonNode).clip).to.equal('rect(0px, 0px, 0px, 0px)');
}); });
it('can be disabled imperatively', async () => { it('can be disabled imperatively', async () => {
@ -231,6 +232,8 @@ describe('lion-button', () => {
<lion-button type="submit">foo</lion-button> <lion-button type="submit">foo</lion-button>
</form> </form>
`); `);
// Prevent page refresh
form.submit = () => {};
const button = form.querySelector('lion-button'); const button = form.querySelector('lion-button');
getTopElement(button).click(); getTopElement(button).click();
@ -245,6 +248,8 @@ describe('lion-button', () => {
<lion-button type="submit">foo</lion-button> <lion-button type="submit">foo</lion-button>
</form> </form>
`); `);
// Prevent page refresh
form.submit = () => {};
pressSpace(form.querySelector('lion-button')); pressSpace(form.querySelector('lion-button'));
await aTimeout(); await aTimeout();
@ -260,6 +265,8 @@ describe('lion-button', () => {
<lion-button type="submit">foo</lion-button> <lion-button type="submit">foo</lion-button>
</form> </form>
`); `);
// Prevent page refresh
form.submit = () => {};
pressEnter(form.querySelector('lion-button')); pressEnter(form.querySelector('lion-button'));
await aTimeout(); await aTimeout();
@ -267,6 +274,26 @@ describe('lion-button', () => {
expect(formSubmitSpy.called).to.be.true; expect(formSubmitSpy.called).to.be.true;
}); });
// input "enter" keypress mock doesn't seem to work right now, but should be tested in the future (maybe with Selenium)
it.skip('works with implicit form submission on-enter inside an input', async () => {
const formSubmitSpy = sinon.spy(e => e.preventDefault());
const form = await fixture(html`
<form @submit="${formSubmitSpy}">
<input name="foo" />
<input name="foo2" />
<lion-button type="submit">foo</lion-button>
</form>
`);
// Prevent page refresh
form.submit = () => {};
pressEnter(form.querySelector('input[name="foo2"]'));
await aTimeout();
await aTimeout();
expect(formSubmitSpy.called).to.be.true;
});
}); });
describe('click event', () => { describe('click event', () => {