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

Bugfix/87 element recycling without attributes #118

Merged
merged 1 commit into from
Jul 11, 2019
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Warn about usage of non-breaking space [#107](https://github.com/trivago/melody/pull/107)
- Fixed bindings of dispatchCustomEvent in `melody-streams` [#117](https://github.com/trivago/melody/pull/117)
- Fixed `combineRefs` unsubscription in `melody-streams` [#120](https://github.com/trivago/melody/pull/120)
- Melody sometimes removes classes from an element / element recycling without attributes [#118](https://github.com/trivago/melody/pull/118/files)

### Chore & Maintenance
- Removes `Chai` and `Sinon` support, Migrates tests to use `Jest`'s matchers. [#103](https://github.com/trivago/melody/pull/103)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<div>
{% if foo %}
<div class="foo"></div>
{% else %}
<div class="foo {{ bar ? 'bar' }}"></div>
{% endif %}
</div>


Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,34 @@ export default function Spaceless(props) {
"
`;

exports[`Compiler should correctly transform static to dynamic attrs.template 1`] = `
"
import { elementVoid, elementOpen, elementClose } from \\"melody-idom\\";
export const _template = {};
const _statics = [\\"class\\", \\"foo\\"];

_template.render = function (_context) {
elementOpen(\\"div\\", null, null);

if (_context.foo) {
elementVoid(\\"div\\", \\"7*U2;JR\\", _statics);
} else {
elementVoid(\\"div\\", null, null, \\"class\\", \\"foo \\" + (_context.bar ? \\"bar\\" : \\"\\"));
}

elementClose(\\"div\\");
};

if (process.env.NODE_ENV !== \\"production\\") {
_template.displayName = \\"StaticToDynamicAttrs\\";
}

export default function StaticToDynamicAttrs(props) {
return _template.render(props);
}
"
`;

exports[`Compiler should correctly transform styles.template 1`] = `
"
import { text, elementOpen, elementClose, elementOpenStart, elementOpenEnd, attr } from \\"melody-idom\\";
Expand Down
29 changes: 29 additions & 0 deletions packages/melody-idom/__tests__/ConditionalRenderingSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ describe('conditional rendering', () => {
});
});

describe('with static attributes', () => {
const _statics = ["class", "foo"];
function render(_context) {
elementOpen("div", null, null);

if (_context.condition) {
elementVoid("div", "7*U2;JR", _statics);
} else {
elementVoid("div", null, null, "class", "foo " + (_context.bar ? "bar" : ""));
}

elementClose("div");
}

it('should apply static attributes when recycling an element', () => {
patch(container, () => render({ condition: false, bar: true }));
expect(container.innerHTML).toMatchSnapshot();
patch(container, () => render({ condition: true, bar: true }));
expect(container.innerHTML).toMatchSnapshot();
});

it('should remove static attributes when recycling an element', () => {
patch(container, () => render({ condition: true, bar: true }));
expect(container.innerHTML).toMatchSnapshot();
patch(container, () => render({ condition: false, bar: true }));
expect(container.innerHTML).toMatchSnapshot();
});
});

describe('nodes', () => {
function render(condition) {
elementOpen('div', null, null, 'id', 'outer');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`conditional rendering with static attributes should apply static attributes when recycling an element 1`] = `"<div><div class=\\"foo bar\\"></div></div>"`;

exports[`conditional rendering with static attributes should apply static attributes when recycling an element 2`] = `"<div><div class=\\"foo\\"></div></div>"`;

exports[`conditional rendering with static attributes should remove static attributes when recycling an element 1`] = `"<div><div class=\\"foo\\"></div></div>"`;

exports[`conditional rendering with static attributes should remove static attributes when recycling an element 2`] = `"<div><div class=\\"foo bar\\"></div></div>"`;

1 change: 1 addition & 0 deletions packages/melody-idom/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ var matches = function(
// which means we can hook onto it freely
if (!data.key) {
data.key = key;
data.staticsApplied = false;
// but we'll need to update the parent element
const parentKeys = currentParent && getData(currentParent).keyMap;
if (parentKeys) {
Expand Down