Skip to content

Commit 609f123

Browse files
committed
fix(template): transformData checks too strict
The Template component shouldn't throw an error if you define the transformData for a single templateKey. Fix #347
1 parent 5ae962b commit 609f123

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

components/Template.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,13 @@ function transformData(fn, templateKey, originalData) {
6767
data = fn(originalData);
6868
} else if (typeof fn === 'object') {
6969
// ex: transformData: {hit, empty}
70-
data = fn[templateKey] && fn[templateKey](originalData);
70+
if (fn[templateKey]) {
71+
data = fn[templateKey](originalData);
72+
} else {
73+
// if the templateKey doesn't exist, just use the
74+
// original data
75+
data = originalData;
76+
}
7177
} else {
7278
throw new Error('`transformData` must be a function or an object');
7379
}

components/__tests__/Template-test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ describe('Template', () => {
127127
}).toThrow('`transformData` must return a `object`, got `undefined`.');
128128
});
129129

130+
it('doesn\'t throw an error if the transformData is an object without the templateKey', () => {
131+
templates = {test: 'it supports {{feature}}'};
132+
data = {feature: 'replace me'};
133+
templateKey = 'test';
134+
transformData = {
135+
anotherKey: (d) => { return d; }
136+
};
137+
let props = getProps();
138+
expect(() => {
139+
renderer.render(<Template {...props} />);
140+
}).toNotThrow();
141+
});
142+
130143
it('throws an error if the transformData returns an unexpected type', () => {
131144
templates = {test: 'it supports {{feature}}'};
132145
data = {feature: 'replace me'};

0 commit comments

Comments
 (0)