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

Update values after upgrading custom elements #385

Merged
merged 4 commits into from
Jun 26, 2018
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
2 changes: 1 addition & 1 deletion src/lib/shady-render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const verifyShadyCSSVersion = () => {
return false;
}
return true;
}
};

/**
* Template factory which scopes template DOM using ShadyCSS.
Expand Down
3 changes: 2 additions & 1 deletion src/lit-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ export function render(
(container as TemplateContainer).__templateInstance = instance;

const fragment = instance._clone();
instance.update(result.values);

removeNodes(container, container.firstChild);
container.appendChild(fragment);

instance.update(result.values);
}

/**
Expand Down
66 changes: 66 additions & 0 deletions src/test/lib/lit-extended_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,71 @@ suite('lit-extended', () => {
assert.equal((container.firstElementChild as any).foo, 1234);
});

const suiteIfCustomElementsAreSupported = (window.customElements != null) ? suite : suite.skip;

suiteIfCustomElementsAreSupported('when rendering custom elements', () => {
suiteSetup(() => {
if (customElements.get('x-test-uses-property-setters') == null) {
class CustomElement extends HTMLElement {
public readonly calledSetter = false;
private _value?: string = undefined;

public get value(): string|undefined {
return this._value;
}

public set value(value: string|undefined) {
(this as {calledSetter: boolean}).calledSetter = true;
this._value = value;
}
}

customElements.define('x-test-uses-property-setters', CustomElement);
}
});

setup(() => {
// Container must be in the document for the custom element to upgrade
document.body.appendChild(container);
});

teardown(() => {
document.body.removeChild(container);
});

test('uses property setters for custom elements', () => {
render(html`
<x-test-uses-property-setters value=${'foo'}></x-test-uses-property-setters>
`, container);
const instance = container.firstElementChild as HTMLElement & {
value: string;
calledSetter: boolean;
};

assert.equal(instance.value, 'foo');
assert.isTrue(instance.calledSetter);
});

test('uses property setters in nested templates added after the initial render', () => {
const template = (content: any) => html`${content}`;

// Do an initial render
render(template('some content'), container);

// Now update the rendered template, render a nested template
const fragment = html`
<x-test-uses-property-setters value=${'foo'}></x-test-uses-property-setters>
`;
render(template(fragment), container);
const instance = container.firstElementChild as HTMLElement & {
value: string;
calledSetter: boolean;
};

assert.equal(instance.value, 'foo');
assert.isTrue(instance.calledSetter);
});
});

});
});
2 changes: 1 addition & 1 deletion src/test/lib/shady-render_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ suite('shady-render', () => {
window.console.warn = function() {
warnCount++;
warn.apply(window.console, arguments);
}
};
const fn = window.ShadyCSS.prepareTemplateDom;
window.ShadyCSS.prepareTemplateDom = undefined;

Expand Down