Skip to content

Commit

Permalink
WIP Add the rest of the tests for what we expect re: unknown attributes
Browse files Browse the repository at this point in the history
**what is the change?:**
Adds tests for the following behavior -

- Numbers and booleans should be converted to strings, and not warn
- NaN, Symbols, functions, and objects should be converted to strings,
  and *should* warn

Going to add tests for the not-warning behavior in a follow-up.

These tests are not entirely passing - we either need to change what we
expect or change the behavior.

**why make this change?:**
Gets everyone on the same page about expected behavior, and codifies it
in a maintainable way

**test plan:**
`yarn test src/renderers/dom/shared/__tests__/ReactDOMAttribute-test.js`

**issue:**
facebook#10399
  • Loading branch information
flarnie committed Sep 2, 2017
1 parent ce88037 commit 41e3ef1
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/renderers/dom/shared/__tests__/ReactDOMAttribute-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,59 @@ describe('ReactDOMAttribute', () => {
ReactDOM.render(<div />, el);
expect(el.firstChild.hasAttribute('unknown')).toBe(false);
});

it('coerces unknown attributes to strings with numbers and booleans', () => {
var el = document.createElement('div');

function testCoerceToString(value) {
ReactDOM.render(<div unknown="something" />, el);
expect(el.firstChild.getAttribute('unknown')).toBe('something');
ReactDOM.render(<div unknown={value} />, el);
expect(el.firstChild.getAttribute('unknown')).toBe(value + '');
}

testCoerceToString(0);
testCoerceToString(-1);
testCoerceToString(42);
testCoerceToString(9000.99999);
// TODO: either change what we expect here or update the implementation
// so that these pass -
//
// testCoerceToString(true);
// testCoerceToString(false);
});

// TODO: get this test passing
xit('coerces unknown attributes to strings **and warns** with NaN, symbols, functions, and objects', () => {
var el = document.createElement('div');
spyOn(console, 'error');

function testCoerceToString(value) {
ReactDOM.render(<div unknown="something" />, el);
expect(el.firstChild.getAttribute('unknown')).toBe('something');
expectDev(console.error.calls.count(0)).toBe(0);
ReactDOM.render(<div unknown={value} />, el);
expect(el.firstChild.getAttribute('unknown')).toBe(value + '');
expectDev(console.error.calls.count(0)).toBe(1);
// TODO: add specific expectations about what the warning says
// expectDev(normalizeCodeLocInfo(console.error.calls.argsFor(0)[0])).toBe(...
console.error.calls.reset();
}

// TODO: this does not warn. We think it should.
testCoerceToString(NaN);

// TODO: either change what we expect or change our implementation
// this throws "TypeError: Cannot convert a Symbol value to a string"
// testCoerceToString(Symbol('foo'));


// TODO: either change what we expect or change our implementation
// this does not set it to the stringified function.
testCoerceToString(() => 'foo');

// TODO: this does not warn. We think it should.
testCoerceToString({hello: 'world'});
});
});
});

0 comments on commit 41e3ef1

Please sign in to comment.