Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.attachShadow({mode: 'open'});
}
public connectedCallback(): void {
// tslint:disable-next-line:no-inner-html
this.shadowRoot!.innerHTML = `
<div></div>
<div class="wrapper">
<div>Test text</div>
</div>
`;
}
}
const tag = defineCE(Test);
const test = await fixture(`<${tag}>`);
expect(test.target).toEqual(jasmine.any(HTMLElement));
expect(test.target.textContent).toBe('Test text');
});
it('throws an error if no provider exists for context', done => {
@consumer
class Consumer extends CustomElement {
@value public contextValue!: number;
}
const tag = defineCE(Consumer);
window.onerror = message => {
expect(message).toContain('No provider found for Consumer');
done();
};
fixture(`<${tag}>`);
});
it('initializes and fills "observedAttributes"', () => {
class Test extends CustomElement {
public static readonly observedAttributes: ReadonlyArray = [];
@attribute('a1', Boolean)
public attr1: boolean | null = null;
@attribute('a2', String)
public attr2: string | null = null;
}
defineCE(Test);
expect(Test.observedAttributes).toEqual(['a1', 'a2']);
});
@gear() public readonly state!: FormState;
@option()
public onSubmit(): void {}
}
@field()
class Field extends CustomElement {
@gear() public readonly formApi!: FormApi;
@gear() public readonly input!: FieldInputProps;
@gear() public readonly meta!: FieldMetaProps;
@option() public readonly name: string = 'test';
}
const fieldTag = defineCE(Field);
const formTag = defineCE(Form);
const formElement = await fixture(`
<${formTag}>
<${fieldTag}>
<input type="text">
`);
const inputElement = formElement.querySelector('input')!;
inputElement.dispatchEvent(new Event('focusin', {bubbles: true}));
expect(state.focus).toHaveBeenCalled();
});
it('provides context for all consumers', async () => {
@provider
class Provider extends CustomElement {
@value public providingValue: number = 2;
}
@consumer
class Consumer extends CustomElement {
@value public contextValue!: number;
}
const providerTag = defineCE(Provider);
const consumerTag = defineCE(Consumer);
const providerElement = await fixture(`
<${providerTag}>
<${consumerTag}>
<${consumerTag}>
`);
const [consumerElement1, consumerElement2] = Array.from(
providerElement.children as any,
);
expect(consumerElement1.contextValue).toBe(2);
expect(consumerElement2.contextValue).toBe(2);
});
@form()
class Test extends CustomElement {
@gear() public readonly formApi!: FormApi;
@gear() public readonly state!: FormState;
@option()
public initialValues: object = {
bar: 2,
foo: 1,
};
@option()
public onSubmit(): void {}
}
const tag = unsafeStatic(defineCE(Test));
await fixture(
html`<${tag} .initialValues="${{
bar: 2,
foo: 1,
}}">`,
);
expect(formSpyObject.initialize).not.toHaveBeenCalled();
});
@option()
public onSubmit(): void {}
}
@field()
class Field extends CustomElement {
@gear() public readonly formApi!: FormApi;
@gear() public readonly input!: FieldInputProps;
@gear() public readonly meta!: FieldMetaProps;
@option() public readonly name: string = 'test';
}
const fieldTag = defineCE(Field);
const formTag = defineCE(Form);
const formElement = await fixture(`
<${formTag}>
<${fieldTag}>
<input type="text">
`);
const inputElement = formElement.querySelector('input')!;
inputElement.dispatchEvent(new Event('focusin', {bubbles: true}));
expect(state.focus).toHaveBeenCalled();
});
it('sets string attribute', async () => {
class Test extends CustomElement {
public static readonly observedAttributes: ReadonlyArray = [];
@attribute('attr', String)
public attribute: string | null = null;
}
const tag = defineCE(Test);
const test = await fixture(`<${tag}>`);
test.attribute = 'str';
expect(test.getAttribute('attr')).toBe('str');
});
export const createSimpleContext = async (Provider, Consumer) => {
const providerTag = defineCE(Provider);
const consumerTag = defineCE(Consumer);
const providerElement = await fixture(`
<${providerTag}>
<${consumerTag}>
`);
return [providerElement, providerElement.children[0]];
};
export const createSimpleContext = async (Provider, Consumer) => {
const providerTag = defineCE(Provider);
const consumerTag = defineCE(Consumer);
const providerElement = await fixture(`
<${providerTag}>
<${consumerTag}>
`);
return [providerElement, providerElement.children[0]];
};