Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
junedchhipa committed Jun 9, 2023
2 parents 3b12fb5 + bb1832f commit 6d0244c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/modules/Exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ class Exports {
columns.push('maximum')
} else {
series.map((s, sI) => {
const sname = s.name ? s.name : `series-${sI}`
const sname = (s.name ? s.name : `series-${sI}`) + ''
if (w.globals.axisCharts) {
columns.push(
sname.split(columnDelimiter).join('')
Expand Down
52 changes: 52 additions & 0 deletions tests/unit/download-csv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,42 @@ describe('Export Csv', () => {
expect.stringContaining('.csv')
)
})
it('export csv from simple bar chart with numeric series names should call triggerDownload with csv encoded file data', () => {
var options = {
chart: {
height: 380,
width: "100%",
type: "bar"
},
series: [
{
name: 2022,
data: [1,2]
},
{
name: 2023,
data: [2,3]
}
],
xaxis: {
categories: ["Category 1","Category 2"]
}
};
const csvData = "category,2022,2023\n" +
"Category 1,,2\n" +
"Category 2,,3"
const chart = createChartWithOptions(options)
chart.w.globals.collapsedSeriesIndices = [0]
const exports = new Exports(chart.ctx)
jest.spyOn(Exports.prototype,'triggerDownload')
exports.exportToCSV(chart.w.config.series,'fileName')
expect(Exports.prototype.triggerDownload).toHaveBeenCalledTimes(1)
expect(Exports.prototype.triggerDownload).toHaveBeenCalledWith(
expect.stringContaining(encodeURIComponent(csvData)),
expect.toBeUndefined,
expect.stringContaining('.csv')
)
})
it('export csv from simple line chart with two series should call triggerDownload with csv encoded file data', () => {
const series = [{name: 'series 1', data: [0,1]},{name: 'series 2', data: [1,2]}]
const csvData = "category,series 1,series 2\n" +
Expand All @@ -69,6 +105,22 @@ describe('Export Csv', () => {
expect.stringContaining('.csv')
)
})
it('export csv from simple line chart with numeric series names should call triggerDownload with csv encoded file data', () => {
const series = [{name: 2022, data: [0,1]},{name: 2023, data: [1,2]}]
const csvData = "category,2022,2023\n" +
"1,0,1\n" +
"2,1,2"
const chart = createChart('line', series)
const exports = new Exports(chart.ctx)
jest.spyOn(Exports.prototype,'triggerDownload')
exports.exportToCSV(chart.w.config.series,'fileName')
expect(Exports.prototype.triggerDownload).toHaveBeenCalledTimes(1)
expect(Exports.prototype.triggerDownload).toHaveBeenCalledWith(
expect.stringContaining(encodeURIComponent(csvData)),
expect.toBeUndefined,
expect.stringContaining('.csv')
)
})
it("export csv from simple line chart with two x y series should call triggerDownload with csv encoded file data", () => {
const series = [{
name: 'series 1',
Expand Down

0 comments on commit 6d0244c

Please sign in to comment.