Skip to content

Commit

Permalink
fix(template): transformData checks too strict
Browse files Browse the repository at this point in the history
The Template component shouldn't throw an error if you define
the transformData for a single templateKey.

Fix #347
  • Loading branch information
redox committed Oct 23, 2015
1 parent 5ae962b commit 609f123
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion components/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ function transformData(fn, templateKey, originalData) {
data = fn(originalData);
} else if (typeof fn === 'object') {
// ex: transformData: {hit, empty}
data = fn[templateKey] && fn[templateKey](originalData);
if (fn[templateKey]) {
data = fn[templateKey](originalData);
} else {
// if the templateKey doesn't exist, just use the
// original data
data = originalData;
}
} else {
throw new Error('`transformData` must be a function or an object');
}
Expand Down
13 changes: 13 additions & 0 deletions components/__tests__/Template-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ describe('Template', () => {
}).toThrow('`transformData` must return a `object`, got `undefined`.');
});

it('doesn\'t throw an error if the transformData is an object without the templateKey', () => {
templates = {test: 'it supports {{feature}}'};
data = {feature: 'replace me'};
templateKey = 'test';
transformData = {
anotherKey: (d) => { return d; }
};
let props = getProps();
expect(() => {
renderer.render(<Template {...props} />);
}).toNotThrow();
});

it('throws an error if the transformData returns an unexpected type', () => {
templates = {test: 'it supports {{feature}}'};
data = {feature: 'replace me'};
Expand Down

0 comments on commit 609f123

Please sign in to comment.