-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add tests for helpers #16
Conversation
fatalError: throwError, | ||
warning: throwError | ||
}); | ||
parseXML = (xml) => parser.parseFromString(xml, 'text/xml'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is more programmatic work that i like to have inside a test suite. maybe it should get its own tests? not sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, agreed! it feels worth it to extract XML parsing into its own helper. created #18 to track this
test/helpers.test.js
Outdated
it('should return child nodes as an array', () => { | ||
const parsed = parseXML('<MyDocument><A>foo</A><B>bar</B><C>baz</C></MyDocument>'); | ||
// expect(getChildren(firstChild)).to.be.an('array'); | ||
console.log(JSON.stringify(parsed, 2, 2)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird things were happening - i don't remember where i left off 😆
probably just PEBKAC at the time
test/helpers.test.js
Outdated
|
||
// @TODO fix this? | ||
it('should throw an error if called with `null`', () => { | ||
expect(() => validateConverters(null)).to.throw(Error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taveras we might as well fix this? it's the result of typeof null // => Object
foo: 'hello', | ||
bar: '5', | ||
baz: 'true' | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an interesting question — whether we should attempt to coerce attribute values. for now, the answer is probably "no" but we might consider it as a feature later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that feels helpful! I created #17 to track this
@@ -115,4 +115,3 @@ export function visitNode(node, index, converters, data) { | |||
|
|||
return createElement(type, newProps, ...childElements); | |||
}; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my IDE trims excess newlines. 🤷♂️
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hah, i blame these newlines on Vim
i don't feel strongly about this, but we can do whichever our final lint rules require
@@ -0,0 +1,233 @@ | |||
import { expect } from 'chai'; | |||
import { DOMParser } from 'xmldom'; | |||
import { isValidElement } from 'react'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added react
as a dev dependency for this
@@ -10,7 +10,7 @@ const ERR_INVALID_CONVERTER = 'XMLToReact: Invalid value for converter map argum | |||
* @private | |||
*/ | |||
export function validateConverters(converters) { | |||
if (typeof converters !== 'object') { | |||
if (typeof converters !== 'object' || !converters) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taveras added this missing falsy check to handle null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! left a few notes
fatalError: throwError, | ||
warning: throwError | ||
}); | ||
parseXML = (xml) => parser.parseFromString(xml, 'text/xml'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, agreed! it feels worth it to extract XML parsing into its own helper. created #18 to track this
const data = { foo: 'bar' } | ||
const { firstChild } = parseXML('<a x="1" y="two">hello</a>'); | ||
visitNode(firstChild, 0, converters, data); | ||
sinon.assert.calledWith(converters.a, { x: '1', y: 'two' }, data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
const element = visitNode(firstChild, 0, converters); | ||
|
||
expect(isValidElement(element)).to.be.true; | ||
expect(element.type).to.equal('ul'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how do you feel about using enzyme
for these tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered it but was concerned that it was overkill. I also considered rendering to markup. Ultimately I wanted to limit additional dependencies or complexity. Further, enzyme has adapters for multiple versions of React, but our library is meant to support any 14|15|16 (and we do)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's fair -- i'll review our use of enzyme in our other tests
Fixes #4