Skip to content

Commit 94c53d3

Browse files
committed
fix(transformData): add an explicit error message
In case the `transformData` is not returning any value or the wrong type, we now throw an explicit error. Fix #212
1 parent 200a7fe commit 94c53d3

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

components/Template.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,22 @@ function transformData(fn, templateKey, originalData) {
5757
return originalData;
5858
}
5959

60+
var data;
6061
if (typeof fn === 'function') {
61-
return fn(originalData);
62+
data = fn(originalData);
6263
} else if (typeof fn === 'object') {
6364
// ex: transformData: {hit, empty}
64-
return fn[templateKey] && fn[templateKey](originalData) || originalData;
65+
data = fn[templateKey] && fn[templateKey](originalData);
66+
} else {
67+
throw new Error('`transformData` must be a function or an object');
6568
}
6669

67-
throw new Error('`transformData` must be a function or an object');
70+
var dataType = typeof data;
71+
var expectedType = typeof originalData;
72+
if (dataType !== expectedType) {
73+
throw new Error(`\`transformData\` must return a \`${expectedType}\`, got \`${dataType}\`.`);
74+
}
75+
return data;
6876
}
6977

7078
function renderTemplate({template, compileOptions, helpers, data}) {

components/__tests__/Template-test.js

+28
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,34 @@ describe('Template', () => {
110110
expect(out).toEqual(<div dangerouslySetInnerHTML={{__html: 'it supports transformData'}}></div>);
111111
});
112112

113+
it('throws an error if the transformData is not anything', () => {
114+
templates = {test: 'it supports {{feature}}'};
115+
data = {feature: 'replace me'};
116+
templateKey = 'test';
117+
transformData = () => {
118+
// return case if missing
119+
};
120+
121+
let props = getProps();
122+
expect(() => {
123+
renderer.render(<Template {...props} />);
124+
}).toThrow('`transformData` must return a `object`, got `undefined`.');
125+
});
126+
127+
it('throws an error if the transformData returns an unexpected type', () => {
128+
templates = {test: 'it supports {{feature}}'};
129+
data = {feature: 'replace me'};
130+
templateKey = 'test';
131+
transformData = () => {
132+
return true;
133+
};
134+
135+
let props = getProps();
136+
expect(() => {
137+
renderer.render(<Template {...props} />);
138+
}).toThrow('`transformData` must return a `object`, got `boolean`.');
139+
});
140+
113141
function getProps() {
114142
return {templates, data, templateKey, useCustomCompileOptions, templatesConfig, transformData};
115143
}

widgets/toggle.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ function prepareToggleData(transformData) {
104104
count: data.count
105105
};
106106

107-
return transformData && transformData(newData) || newData;
107+
if (transformData) {
108+
newData = transformData(newData);
109+
if (typeof newData !== 'object') {
110+
throw new Error('`transformData` must return an object.');
111+
}
112+
}
113+
114+
return newData;
108115
};
109116
}
110117

0 commit comments

Comments
 (0)