From 14c0bfc4d8cf1eb57b865856e6ecf0a7461d0be4 Mon Sep 17 00:00:00 2001 From: Miguel Rincon Date: Mon, 24 Mar 2025 10:49:53 +0100 Subject: [PATCH] feat(GlChart): Allow charts to opt-out of aria This change allows charts to explicitly disable aria by setting it to false. If aria's properties are not set, it gets enabled by default. Additionally, it refactors the merging the options object to make the code more readable. --- src/components/charts/chart/chart.spec.js | 112 +++++++++++------- src/components/charts/chart/chart.vue | 37 +++--- .../__snapshots__/column_chart.spec.js.snap | 9 -- .../__snapshots__/stacked_column.spec.js.snap | 12 -- src/utils/charts/config.js | 3 - 5 files changed, 89 insertions(+), 84 deletions(-) diff --git a/src/components/charts/chart/chart.spec.js b/src/components/charts/chart/chart.spec.js index cf1744c8af..0093f67a5c 100644 --- a/src/components/charts/chart/chart.spec.js +++ b/src/components/charts/chart/chart.spec.js @@ -104,42 +104,88 @@ describe('chart component', () => { expect(wrapper.vm.chart).toBeDefined(); }); - describe('sets chart options', () => { - beforeEach(async () => { - createWrapper({ - options: { - title: { text: 'My chart title' }, - }, + describe('chart options', () => { + describe('when options are set', () => { + beforeEach(async () => { + createWrapper({ + options: { + title: { text: 'My chart title' }, + }, + }); + await nextTick(); }); - await nextTick(); - }); - it('inits chart options, adding aria.enabled', async () => { - expect(wrapper.vm.chart.setOption).toHaveBeenCalledTimes(1); - expect(wrapper.vm.chart.setOption).toHaveBeenCalledWith({ - title: { text: 'My chart title' }, - aria: { enabled: true }, + it('inits chart options, adding aria.enabled', async () => { + expect(wrapper.vm.chart.setOption).toHaveBeenCalledTimes(1); + expect(wrapper.vm.chart.setOption).toHaveBeenCalledWith({ + title: { text: 'My chart title' }, + aria: { enabled: true }, + }); + + expect(wrapper.emitted('updated').length).toEqual(1); }); - expect(wrapper.emitted('updated').length).toEqual(1); - }); + it('reacts to options changes by merging options', async () => { + wrapper.setProps({ + options: { + title: { text: 'My new chart title' }, + xAxis: { show: true }, + }, + }); + await nextTick(); - it('reacts to options changes by merging options', async () => { - wrapper.setProps({ - options: { + expect(wrapper.vm.chart.setOption).toHaveBeenCalledTimes(2); + expect(wrapper.vm.chart.setOption).toHaveBeenLastCalledWith({ + aria: { enabled: true }, title: { text: 'My new chart title' }, xAxis: { show: true }, - }, + }); + expect(wrapper.emitted('updated').length).toEqual(2); }); - await nextTick(); + }); + + describe('when aria is explicitly disabled', () => { + it('does not enable aria', async () => { + createWrapper({ + options: { aria: { enabled: false } }, + }); + await nextTick(); - expect(wrapper.vm.chart.setOption).toHaveBeenCalledTimes(2); - expect(wrapper.vm.chart.setOption).toHaveBeenLastCalledWith({ - title: { text: 'My new chart title' }, - xAxis: { show: true }, - aria: { enabled: true }, + expect(wrapper.vm.chart.setOption).toHaveBeenCalledWith({ + aria: { enabled: false }, + }); + }); + }); + + describe('when toolbox is enabled', () => { + it('sets grid top to `toolboxHeight`', async () => { + createWrapper({ + options: { toolbox: { show: true } }, + }); + await nextTick(); + + expect(wrapper.vm.chart.setOption).toHaveBeenCalledWith({ + aria: { enabled: true }, + toolbox: { show: true }, + grid: { top: toolboxHeight }, + }); + }); + + it('increases grid top by `toolboxHeight`', async () => { + createWrapper({ + options: { + toolbox: { show: true }, + grid: { top: 100 }, + }, + }); + await nextTick(); + + expect(wrapper.vm.chart.setOption).toHaveBeenCalledWith({ + aria: { enabled: true }, + toolbox: { show: true }, + grid: { top: 100 + toolboxHeight }, + }); }); - expect(wrapper.emitted('updated').length).toEqual(2); }); }); @@ -206,18 +252,4 @@ describe('chart component', () => { }); }); }); - - describe('with toolbox in options', () => { - it('increases grid top by `toolboxHeight`', async () => { - const optionsWithToolbox = { toolbox: { show: true } }; - wrapper = shallowMount(Chart, { propsData: { options: optionsWithToolbox } }); - await nextTick(); - - expect(wrapper.vm.chart.setOption).toHaveBeenCalledWith({ - ...optionsWithToolbox, - grid: { top: toolboxHeight }, - aria: { enabled: true }, - }); - }); - }); }); diff --git a/src/components/charts/chart/chart.vue b/src/components/charts/chart/chart.vue index c2e81dad29..361d22ac81 100644 --- a/src/components/charts/chart/chart.vue +++ b/src/components/charts/chart/chart.vue @@ -1,6 +1,7 @@