Fix recursive render

This commit is contained in:
Gregory Schier 2017-01-13 11:21:03 -08:00
parent 0c66afbf4b
commit 3b8a73231e
3 changed files with 45 additions and 25 deletions

View File

@ -62,39 +62,56 @@ describe('buildRenderContext()', () => {
}); });
}); });
it('rendered recursive should not infinite loop', () => {
const ancestors = [{
// Sub Environment
type: models.requestGroup.type,
environment: {recursive: '{{ recursive }}/hello'}
}];
const context = renderUtils.buildRenderContext(ancestors);
expect(context).toEqual({recursive: '/hello'});
});
it('cascades properly and renders', () => { it('cascades properly and renders', () => {
const ancestors = [ const ancestors = [
{ {
type: models.requestGroup.type, type: models.requestGroup.type,
environment: {bar: '{{ foo }} parent', recursive: '{{ recursive }}', ancestor: true} environment: {
base_url: '{{ base_url }}/resource',
ancestor: true,
winner: 'folder parent'
}
}, },
{ {
type: models.requestGroup.type, type: models.requestGroup.type,
environment: {bar: '{{ foo }} grandparent', ancestor: true} environment: {
ancestor: true,
winner: 'folder grandparent'
}
} }
]; ];
const rootEnvironment = { const rootEnvironment = {
type: models.environment.type, type: models.environment.type,
data: {foo: 'root', root: true} data: {winner: 'root', root: true}
}; };
const subEnvironment = { const subEnvironment = {
type: models.environment.type, type: models.environment.type,
data: {foo: 'sub', sub: true} data: {winner: 'sub', sub: true, base_url: 'https://insomnia.rest'}
}; };
const context = renderUtils.buildRenderContext( const context = renderUtils.buildRenderContext(ancestors,
ancestors,
rootEnvironment, rootEnvironment,
subEnvironment subEnvironment
); );
expect(context).toEqual({ expect(context).toEqual({
foo: 'sub', base_url: 'https://insomnia.rest/resource',
bar: 'sub parent',
recursive: '{{ recursive }}',
ancestor: true, ancestor: true,
winner: 'folder parent',
root: true, root: true,
sub: true sub: true
}); });

View File

@ -60,33 +60,35 @@ export function render (template, context = {}) {
} }
export function buildRenderContext (ancestors, rootEnvironment, subEnvironment) { export function buildRenderContext (ancestors, rootEnvironment, subEnvironment) {
const renderContext = {};
if (rootEnvironment) {
Object.assign(renderContext, rootEnvironment.data);
}
if (subEnvironment) {
Object.assign(renderContext, subEnvironment.data);
}
if (!Array.isArray(ancestors)) { if (!Array.isArray(ancestors)) {
ancestors = []; ancestors = [];
} }
const environments = [];
if (rootEnvironment) {
environments.push(rootEnvironment.data);
}
if (subEnvironment) {
environments.push(subEnvironment.data);
}
// Merge all environments. Note that we're reversing ancestors because we want to merge // Merge all environments. Note that we're reversing ancestors because we want to merge
// from top-down (closest ancestor should win) // from top-down (closest ancestor should win)
for (let doc of ancestors.reverse()) { for (const doc of ancestors.reverse()) {
if (!doc.environment) { if (!doc.environment) {
continue; continue;
} }
environments.push(doc.environment);
Object.assign(renderContext, doc.environment);
} }
// Now we're going to render the renderContext with itself. const renderContext = {};
// This is to support templating inside environments for (const environment of environments) {
return recursiveRender(renderContext, renderContext); Object.assign(renderContext, recursiveRender(environment, renderContext));
}
return renderContext;
} }
export function recursiveRender (obj, context) { export function recursiveRender (obj, context) {

View File

@ -355,6 +355,7 @@ class App extends Component {
} }
// Force refresh if environment changes // Force refresh if environment changes
// TODO: Only do this for environments in this workspace (not easy because they're nested)
if (doc.type === models.environment.type) { if (doc.type === models.environment.type) {
console.log('[App] Forcing update from environment change', change); console.log('[App] Forcing update from environment change', change);
this._wrapper.forceRequestPaneRefresh(); this._wrapper.forceRequestPaneRefresh();