chore: add tests for plotTemplates replacements

This commit is contained in:
Matthias 2024-07-23 20:20:26 +02:00
parent 6d7ac23838
commit 22c010cf65
2 changed files with 105 additions and 2 deletions

View File

@ -52,7 +52,9 @@ function replaceTemplateColumns(template: Partial<PlotConfig>, nameMap: Record<s
const newKey = nameMap[key] || key;
newMainPlot[newKey] = template.main_plot[key];
}
newTemplate.main_plot = newMainPlot;
if ('main_plot' in template) {
newTemplate.main_plot = newMainPlot;
}
// Replace the keys of all elements in subplots
const newSubplots: Record<string, any> = {};
@ -64,7 +66,9 @@ function replaceTemplateColumns(template: Partial<PlotConfig>, nameMap: Record<s
}
newSubplots[subplotKey] = newSubplot;
}
newTemplate.subplots = newSubplots;
if ('subplots' in template) {
newTemplate.subplots = newSubplots;
}
return newTemplate;
}
@ -90,6 +94,7 @@ export function usePlotTemplates() {
plotTemplates,
applyPlotTemplate,
getTemplateContent,
replaceTemplateColumns,
plotTemplateNames: computed(() => Object.keys(plotTemplates.value)),
};
}

View File

@ -0,0 +1,98 @@
import { describe, it, expect } from 'vitest';
import { usePlotTemplates } from '@/composables/plotTemplates';
import { PlotConfig } from '@/types';
describe('plotTemplates.ts', () => {
it('Updates main plot values', () => {
const { replaceTemplateColumns } = usePlotTemplates();
const reMapping = { ema: 'ema_14' };
const template: Partial<PlotConfig> = {
main_plot: {
ema: {
color: '#ff8000',
type: 'line',
},
},
};
const expected: Partial<PlotConfig> = {
main_plot: {
ema_14: {
color: '#ff8000',
type: 'line',
},
},
};
expect(replaceTemplateColumns(template, reMapping)).toEqual(expected);
});
it('Updates subplot plot values', () => {
const { replaceTemplateColumns } = usePlotTemplates();
const reMapping = { rsi: 'rsi_14' };
const template: Partial<PlotConfig> = {
subplots: {
RSI: {
rsi: {
color: '#ff8000',
type: 'line',
},
},
},
};
const expected: Partial<PlotConfig> = {
subplots: {
RSI: {
rsi_14: {
color: '#ff8000',
type: 'line',
},
},
},
};
expect(replaceTemplateColumns(template, reMapping)).toEqual(expected);
});
it('Updates both main and subplot values', () => {
const { replaceTemplateColumns } = usePlotTemplates();
const reMapping = { ema: 'ema_200', macd: 'macd_5m', macdsignal: 'macdsignal_5m' };
const template: Partial<PlotConfig> = {
main_plot: {
ema: {
color: '#ff8000',
type: 'line',
},
},
subplots: {
MACD: {
macdsignal: {
color: '#ff8000',
type: 'line',
},
macd: {
color: '#1370f4',
type: 'line',
},
},
},
};
const expected: Partial<PlotConfig> = {
main_plot: {
ema_200: {
color: '#ff8000',
type: 'line',
},
},
subplots: {
MACD: {
macdsignal_5m: {
color: '#ff8000',
type: 'line',
},
macd_5m: {
color: '#1370f4',
type: 'line',
},
},
},
};
expect(replaceTemplateColumns(template, reMapping)).toEqual(expected);
});
});