import { Component, Fragment, h, Host, Prop, State } from '@stencil/core';
import { newSpecPage } from '@stencil/core/testing';
import { FunctionalComponent } from '../../declarations';
describe('jsx', () => {
it('Fragment', async () => {
@Component({ tag: 'cmp-a' })
class CmpA {
render() {
return (
<>
A
B
>
);
}
}
const { root } = await newSpecPage({
components: [CmpA],
html: ``,
});
expect(root).toEqualHtml(`
A
B
`);
});
it('render template', async () => {
@Component({ tag: 'cmp-a' })
class CmpA {
@Prop() complexProp: any;
render() {
return The answer is: {this.complexProp.value}
;
}
}
const OBJECT = { value: 42 };
const MyFunctionalCmp = () => ;
const { root } = await newSpecPage({
components: [CmpA],
template: () => ,
});
expect(root).toEqualHtml(`
The answer is: 42
`);
});
it('functional cmp with default props', async () => {
interface FunctionalCmpProps {
first?: string;
last?: string;
}
const FunctionalCmp: FunctionalComponent = ({ first = 'Kim', last = 'Doe' }) => (
Hi, my name is {first} {last}.
);
@Component({ tag: 'cmp-a' })
class CmpA {
render() {
return ;
}
}
const { root } = await newSpecPage({
components: [CmpA],
html: '',
});
expect(root).toEqualHtml(`
Hi, my name is Kim Doe.
`);
});
describe('event', () => {
@Component({ tag: 'cmp-a' })
class CmpA {
@State() lastEvent: any;
render() {
return (
(this.lastEvent = 'onClick')}
on-Click={() => (this.lastEvent = 'on-Click')}
on-scroll={() => (this.lastEvent = 'on-scroll')}
onIonChange={() => (this.lastEvent = 'onIonChange')}
on-IonChange={() => (this.lastEvent = 'on-IonChange')}
on-ALLCAPS={() => (this.lastEvent = 'on-ALLCAPS')}
>
{this.lastEvent}
);
}
}
it('click', async () => {
const { root, waitForChanges } = await newSpecPage({
components: [CmpA],
html: ``,
});
root.dispatchEvent(new CustomEvent('click'));
await waitForChanges();
expect(root.textContent).toBe('onClick');
});
it('Click', async () => {
const { root, waitForChanges } = await newSpecPage({
components: [CmpA],
html: ``,
});
root.dispatchEvent(new CustomEvent('Click'));
await waitForChanges();
expect(root.textContent).toBe('on-Click');
});
it('scroll', async () => {
const { root, waitForChanges } = await newSpecPage({
components: [CmpA],
html: ``,
});
root.dispatchEvent(new CustomEvent('scroll'));
await waitForChanges();
expect(root.textContent).toBe('on-scroll');
});
it('ionChange', async () => {
const { root, waitForChanges } = await newSpecPage({
components: [CmpA],
html: ``,
});
root.dispatchEvent(new CustomEvent('ionChange'));
await waitForChanges();
expect(root.textContent).toBe('onIonChange');
});
it('IonChange', async () => {
const { root, waitForChanges } = await newSpecPage({
components: [CmpA],
html: ``,
});
root.dispatchEvent(new CustomEvent('IonChange'));
await waitForChanges();
expect(root.textContent).toBe('on-IonChange');
});
it('ALLCAPS', async () => {
const { root, waitForChanges } = await newSpecPage({
components: [CmpA],
html: ``,
});
root.dispatchEvent(new CustomEvent('ALLCAPS'));
await waitForChanges();
expect(root.textContent).toBe('on-ALLCAPS');
});
});
});