|
| 1 | +import { expect } from '@esm-bundle/chai'; |
| 2 | +import { fixtureSync, nextRender, oneEvent } from '@vaadin/testing-helpers'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import '../vaadin-chart.js'; |
| 5 | + |
| 6 | +describe('turbo-mode', () => { |
| 7 | + let chart; |
| 8 | + |
| 9 | + beforeEach(async () => { |
| 10 | + sinon.stub(console, 'warn'); |
| 11 | + }); |
| 12 | + |
| 13 | + afterEach(() => { |
| 14 | + console.warn.restore(); |
| 15 | + }); |
| 16 | + |
| 17 | + function addSeries(numDataItems) { |
| 18 | + const data = Array.from(Array(numDataItems).keys()); |
| 19 | + const series = fixtureSync(`<vaadin-chart-series values="[${data.join(',')}]"></vaadin-chart-series>`); |
| 20 | + chart.appendChild(series); |
| 21 | + } |
| 22 | + |
| 23 | + function addSeriesWithJS(numDataItems) { |
| 24 | + const data = Array.from(Array(numDataItems).keys()); |
| 25 | + chart.updateConfiguration({ |
| 26 | + series: [ |
| 27 | + { |
| 28 | + type: 'column', |
| 29 | + data |
| 30 | + } |
| 31 | + ] |
| 32 | + }); |
| 33 | + } |
| 34 | + |
| 35 | + describe('when initializing chart', () => { |
| 36 | + beforeEach(async () => { |
| 37 | + chart = document.createElement('vaadin-chart'); |
| 38 | + chart.updateConfiguration({ |
| 39 | + plotOptions: { |
| 40 | + series: { |
| 41 | + turboThreshold: 10 |
| 42 | + } |
| 43 | + } |
| 44 | + }); |
| 45 | + await nextRender(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('add series', () => { |
| 49 | + it('should not log warning when no series exceeds the turbo threshold', async () => { |
| 50 | + addSeries(9); |
| 51 | + document.body.appendChild(chart); |
| 52 | + await nextRender(); |
| 53 | + expect(console.warn.called).to.be.false; |
| 54 | + }); |
| 55 | + it('should log warning when initializing with a series that exceeds the turbo threshold', async () => { |
| 56 | + addSeries(11); |
| 57 | + document.body.appendChild(chart); |
| 58 | + await nextRender(); |
| 59 | + expect(console.warn.calledOnce).to.be.true; |
| 60 | + expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series'); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('add series with JS', () => { |
| 65 | + it('should not log warning when no series exceeds the turbo threshold', async () => { |
| 66 | + addSeriesWithJS(9); |
| 67 | + document.body.appendChild(chart); |
| 68 | + await nextRender(); |
| 69 | + expect(console.warn.called).to.be.false; |
| 70 | + }); |
| 71 | + it('should log warning when initializing with a series that exceeds the turbo threshold', async () => { |
| 72 | + addSeriesWithJS(11); |
| 73 | + document.body.appendChild(chart); |
| 74 | + await nextRender(); |
| 75 | + expect(console.warn.calledOnce).to.be.true; |
| 76 | + expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series'); |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + describe('with existing chart', () => { |
| 82 | + beforeEach(async () => { |
| 83 | + chart = fixtureSync(`<vaadin-chart></vaadin-chart>`); |
| 84 | + chart.updateConfiguration({ |
| 85 | + plotOptions: { |
| 86 | + series: { |
| 87 | + turboThreshold: 10 |
| 88 | + } |
| 89 | + } |
| 90 | + }); |
| 91 | + await nextRender(); |
| 92 | + }); |
| 93 | + |
| 94 | + describe('adding series', () => { |
| 95 | + it('should not log warning when no series exceeds the turbo threshold', async () => { |
| 96 | + addSeries(9); |
| 97 | + await oneEvent(chart, 'chart-redraw'); |
| 98 | + expect(console.warn.called).to.be.false; |
| 99 | + }); |
| 100 | + |
| 101 | + it('should log warning when a series exceeds the turbo threshold', async () => { |
| 102 | + addSeries(11); |
| 103 | + await oneEvent(chart, 'chart-redraw'); |
| 104 | + expect(console.warn.calledOnce).to.be.true; |
| 105 | + expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series'); |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('adding series with JS', () => { |
| 110 | + it('should not log warning when no series exceeds the turbo threshold', async () => { |
| 111 | + addSeriesWithJS(9); |
| 112 | + await oneEvent(chart, 'chart-redraw'); |
| 113 | + expect(console.warn.called).to.be.false; |
| 114 | + }); |
| 115 | + |
| 116 | + it('should log warning when a series exceeds the turbo threshold', async () => { |
| 117 | + addSeriesWithJS(11); |
| 118 | + await oneEvent(chart, 'chart-redraw'); |
| 119 | + expect(console.warn.calledOnce).to.be.true; |
| 120 | + expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series'); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + describe('adding points', () => { |
| 125 | + function addPoint(seriesIndex, data) { |
| 126 | + chart.configuration.series[seriesIndex].addPoint(data); |
| 127 | + } |
| 128 | + |
| 129 | + it('should not log warning when total number of items does not exceed the turbo threshold', async () => { |
| 130 | + addSeriesWithJS(4); |
| 131 | + await oneEvent(chart, 'chart-redraw'); |
| 132 | + addPoint(0, 5); |
| 133 | + addPoint(0, 6); |
| 134 | + addPoint(0, 7); |
| 135 | + chart.configuration.redraw(); |
| 136 | + expect(console.warn.called).to.be.false; |
| 137 | + }); |
| 138 | + |
| 139 | + it('should log warning when total number of items exceeds the turbo threshold', async () => { |
| 140 | + addSeriesWithJS(10); |
| 141 | + await oneEvent(chart, 'chart-redraw'); |
| 142 | + expect(console.warn.called).to.be.false; |
| 143 | + |
| 144 | + addPoint(0, 11); |
| 145 | + chart.configuration.redraw(); |
| 146 | + expect(console.warn.calledOnce).to.be.true; |
| 147 | + expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series'); |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + describe('single log entry', () => { |
| 152 | + it('should only log the warning once', async () => { |
| 153 | + addSeriesWithJS(13); |
| 154 | + await oneEvent(chart, 'chart-redraw'); |
| 155 | + expect(console.warn.calledOnce).to.be.true; |
| 156 | + expect(console.warn.args[0][0]).to.include('<vaadin-chart> Turbo mode has been enabled for one or more series'); |
| 157 | + |
| 158 | + console.warn.resetHistory(); |
| 159 | + addSeriesWithJS(16); |
| 160 | + await oneEvent(chart, 'chart-redraw'); |
| 161 | + expect(console.warn.called).to.be.false; |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + describe('production mode', () => { |
| 166 | + const originalDevelopmentMode = window.Vaadin.developmentMode; |
| 167 | + before(() => { |
| 168 | + window.Vaadin.developmentMode = false; |
| 169 | + }); |
| 170 | + after(() => { |
| 171 | + window.Vaadin.developmentMode = originalDevelopmentMode; |
| 172 | + }); |
| 173 | + |
| 174 | + it('should not log warning in production', async () => { |
| 175 | + addSeriesWithJS(13); |
| 176 | + await oneEvent(chart, 'chart-redraw'); |
| 177 | + expect(console.warn.called).to.be.false; |
| 178 | + }); |
| 179 | + }); |
| 180 | + }); |
| 181 | +}); |
0 commit comments