-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use hiff to compare SVG output to fixtures
Importantly, we normalize property order in the SVG style attribute before comparing, to avoid spurious errors on essentially identical images. A side benefit is nicer error messages on test failures.
- Loading branch information
Showing
3 changed files
with
36 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import hiff from 'hiff'; | ||
|
||
function normalizeStyle(style) { | ||
style = (style || '').split(';').map(s => s.trim()); | ||
style.sort(); | ||
return style.join(';'); | ||
} | ||
|
||
function normalizeAttrs(...$ns) { | ||
for (let $n of $ns) { | ||
if ($n.attr('style')) | ||
$n.attr('style', normalizeStyle($n.attr('style'))); | ||
} | ||
} | ||
|
||
function comparatorFn($n1, $n2, childChanges) { | ||
normalizeAttrs($n1, $n2); | ||
return hiff.defaultTagComparisonFn($n1, $n2, childChanges); | ||
} | ||
|
||
export default function compareSvg(svg1, svg2, options = {}) { | ||
return hiff.compare(svg1, svg2, Object.assign({}, options, {tagComparison: comparatorFn})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,17 @@ | ||
import fixtures from './fixtures'; | ||
import {render} from 'enzyme'; | ||
import { expect } from 'chai'; | ||
import compareSvg from './compareSvg'; | ||
|
||
describe('Graphical tests from fixtures.js', function() { | ||
for (let key of Object.keys(fixtures)) { | ||
describe(`${key}`, function() { | ||
it('should render as specified', function() { | ||
const wrapper = render(fixtures[key].jsx); | ||
expect(wrapper.html()).to.eq(fixtures[key].svg); | ||
}); | ||
}); | ||
} | ||
}); | ||
for (let key of Object.keys(fixtures)) { | ||
describe(`${key}`, function() { | ||
it('should render as specified', function() { | ||
const wrapper = render(fixtures[key].jsx); | ||
const result = compareSvg(wrapper.html(), fixtures[key].svg); | ||
const errorMessage = 'SVG output changed:\n' + result.changes.map(change => change.message).join('\n') + '\n'; | ||
expect(result.changes, errorMessage).to.be.empty; | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters