Skip to content

Commit

Permalink
Sort attributes by name in HTMLElement plugin (#3783)
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrottimark authored and cpojer committed Jun 26, 2017
1 parent 1199558 commit eb4c0ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 5 additions & 3 deletions packages/pretty-format/src/__tests__/html_element.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ describe('HTMLElement Plugin', () => {

it('supports an HTML element with multiple attributes', () => {
const parent = document.createElement('div');
// set attributes in unsorted order by name to verify sorting
parent.setAttribute('id', 123);
parent.setAttribute('class', 'classy');

expect(parent).toPrettyPrintTo('<div\n id="123"\n class="classy"\n/>');
expect(parent).toPrettyPrintTo('<div\n class="classy"\n id="123"\n/>');
});

it('supports an element with text content', () => {
Expand All @@ -71,11 +72,12 @@ describe('HTMLElement Plugin', () => {
const child = document.createElement('span');
parent.appendChild(child);

child.setAttribute('id', 123);
// set attributes in sorted order by name
child.setAttribute('class', 'classy');
child.setAttribute('id', 123);

expect(parent).toPrettyPrintTo(
'<div>\n <span\n id="123"\n class="classy"\n />\n</div>',
'<div>\n <span\n class="classy"\n id="123"\n />\n</div>',
);
});

Expand Down
7 changes: 6 additions & 1 deletion packages/pretty-format/src/plugins/html_element.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ function printChildren(flatChildren, print, indent, colors, opts) {

function printAttributes(attributes: Array<Attribute>, indent, colors, opts) {
return attributes
.sort()
.sort(
(attributeA, attributeB) =>
attributeA.name === attributeB.name
? 0
: attributeA.name < attributeB.name ? -1 : 1,
)
.map(attribute => {
return (
opts.spacing +
Expand Down

0 comments on commit eb4c0ab

Please sign in to comment.