Skip to content
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

refactor(sbb-button): lit migration #1997

Merged
merged 1 commit into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/sbb-button/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './sbb-button';
90 changes: 48 additions & 42 deletions src/components/sbb-button/sbb-button.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,97 +1,103 @@
import { E2EElement, E2EPage, newE2EPage } from '@stencil/core/testing';
import { waitForCondition } from '../../global/testing';
import { assert, expect, fixture } from '@open-wc/testing';
import { html } from 'lit/static-html.js';
import { sendKeys } from '@web/test-runner-commands';
import { EventSpy } from '../../global/testing/event-spy';
import { SbbButton } from './sbb-button';

describe('sbb-button', () => {
let element: E2EElement, page: E2EPage;
let element: SbbButton;

beforeEach(async () => {
page = await newE2EPage();
await page.setContent('<sbb-button id="focus-id">I am a button</sbb-button>');
element = await page.find('sbb-button');
element = await fixture(html`<sbb-button id="focus-id">I am a button</sbb-button>`);
});

it('renders', async () => {
expect(element).toHaveClass('hydrated');
assert.instanceOf(element, SbbButton);
});

describe('events', () => {
it('dispatches event on click', async () => {
await page.waitForChanges();
const clickSpy = await page.spyOnEvent('click');
await element.updateComplete;
const clickSpy = new EventSpy('click');

await element.click();
await waitForCondition(() => clickSpy.events.length === 1);
expect(clickSpy).toHaveReceivedEventTimes(1);
expect(clickSpy.count).to.be.equal(1);
});

it('should not dispatch event on click if disabled', async () => {
element.setAttribute('disabled', true);
element.setAttribute('disabled', 'true');

await page.waitForChanges();
await element.updateComplete;

const clickSpy = await page.spyOnEvent('click');
const clickSpy = new EventSpy('click');

await element.click();
expect(clickSpy).not.toHaveReceivedEvent();
element.click();
expect(clickSpy.count).not.to.be.greaterThan(0);
});

it('should dispatch event on click if is-static', async () => {
element.setAttribute('is-static', true);
element.setAttribute('is-static', 'true');

await page.waitForChanges();
await element.updateComplete;

const clickSpy = await page.spyOnEvent('click');
const clickSpy = new EventSpy('click');

await element.click();
expect(clickSpy).toHaveReceivedEvent();
expect(clickSpy.count).to.be.greaterThan(0);
});

it('should dispatch click event on pressing Enter', async () => {
const clickSpy = await page.spyOnEvent('click');
await element.press('Enter');
expect(clickSpy).toHaveReceivedEvent();
const clickSpy = new EventSpy('click');
element.focus();
await sendKeys({ press: 'Enter' });
expect(clickSpy.count).to.be.greaterThan(0);
});

it('should dispatch click event on pressing Space', async () => {
const clickSpy = await page.spyOnEvent('click');
await element.press(' ');
expect(clickSpy).toHaveReceivedEvent();
const clickSpy = new EventSpy('click');
element.focus();
await sendKeys({ press: ' ' });
expect(clickSpy.count).to.be.greaterThan(0);
});

it('should dispatch click event on pressing Enter with href', async () => {
element.setAttribute('href', 'test');
await page.waitForChanges();
element.setAttribute('href', '#');
await element.updateComplete;

const clickSpy = await page.spyOnEvent('click');
await element.press('Enter');
expect(clickSpy).toHaveReceivedEvent();
const clickSpy = new EventSpy('click');
element.focus();
await sendKeys({ press: 'Enter' });
expect(clickSpy.count).to.be.greaterThan(0);
});

it('should not dispatch click event on pressing Space with href', async () => {
element.setAttribute('href', 'test');
await page.waitForChanges();
element.setAttribute('href', '#');
await element.updateComplete;

const clickSpy = await page.spyOnEvent('click');
await element.press(' ');
expect(clickSpy).not.toHaveReceivedEvent();
const clickSpy = new EventSpy('click');
element.focus();
await sendKeys({ press: ' ' });
expect(clickSpy.count).not.to.be.greaterThan(0);
});

it('should stop propagating host click if disabled', async () => {
element.setProperty('disabled', true);
element.disabled = true;

const clickSpy = await page.spyOnEvent('click');
const clickSpy = new EventSpy('click');

element.triggerEvent('click');
await page.waitForChanges();
element.dispatchEvent(new CustomEvent('click'));
await element.updateComplete;

expect(clickSpy).not.toHaveReceivedEvent();
expect(clickSpy.count).not.to.be.greaterThan(0);
});

it('should receive focus', async () => {
await element.focus();
await page.waitForChanges();
element.focus();
await element.updateComplete;

expect(await page.evaluate(() => document.activeElement.id)).toBe('focus-id');
expect(document.activeElement.id).to.be.equal('focus-id');
});
});
});
Loading