|
| 1 | +import { expect } from '@esm-bundle/chai'; |
| 2 | +import { fixtureSync, oneEvent } from '@vaadin/testing-helpers'; |
| 3 | +import '../vaadin-chart.js'; |
| 4 | +import Highcharts from 'highcharts/es-modules/masters/highstock.src.js'; |
| 5 | + |
| 6 | +// eslint-disable-next-line no-script-url |
| 7 | +const UNSUPPORTED_URL = 'javascript:void(0)'; |
| 8 | + |
| 9 | +describe('vaadin-chart option values', () => { |
| 10 | + let chart, chartContainer; |
| 11 | + |
| 12 | + beforeEach(async () => { |
| 13 | + chart = fixtureSync(` |
| 14 | + <vaadin-chart> |
| 15 | + <vaadin-chart-series values="[10, 20, 30]"></vaadin-chart-series> |
| 16 | + </vaadin-chart> |
| 17 | + `); |
| 18 | + await oneEvent(chart, 'chart-load'); |
| 19 | + chartContainer = chart.$.chart; |
| 20 | + }); |
| 21 | + |
| 22 | + describe('point keys', () => { |
| 23 | + afterEach(() => { |
| 24 | + delete Object.prototype.custom; |
| 25 | + }); |
| 26 | + |
| 27 | + it('should set nested point keys', () => { |
| 28 | + const series = chart.configuration.addSeries({ keys: ['y', 'custom.value'], data: [[1, 'a']] }); |
| 29 | + expect(series.points[0].custom.value).to.equal('a'); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should ignore reserved segments in point keys', () => { |
| 33 | + const series = chart.configuration.addSeries({ |
| 34 | + keys: ['y', '__proto__.custom', 'constructor.prototype.custom'], |
| 35 | + data: [[1, 'a', 'b']], |
| 36 | + }); |
| 37 | + expect({}.custom).to.be.undefined; |
| 38 | + expect(series.points[0].y).to.equal(1); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('point options', () => { |
| 43 | + it('should ignore reserved option names in point objects', () => { |
| 44 | + const series = chart.configuration.series[0]; |
| 45 | + series.setData(JSON.parse('[{ "y": 1, "__proto__": { "custom": "a" }, "constructor": { "custom": "b" } }]')); |
| 46 | + const point = series.points[0]; |
| 47 | + expect(point).to.be.instanceOf(Highcharts.Point); |
| 48 | + expect(point.y).to.equal(1); |
| 49 | + expect(point.custom).to.be.undefined; |
| 50 | + expect(point.options).to.not.have.own.property('constructor'); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe('credits', () => { |
| 55 | + async function setCreditsHref(href) { |
| 56 | + chart.updateConfiguration({ credits: { enabled: true, href } }); |
| 57 | + await oneEvent(chart, 'chart-redraw'); |
| 58 | + } |
| 59 | + |
| 60 | + [ |
| 61 | + 'https://vaadin.com', |
| 62 | + 'HTTPS://VAADIN.COM', |
| 63 | + 'about.html', |
| 64 | + '/about', |
| 65 | + '#about', |
| 66 | + 'tel:+123', |
| 67 | + 'ftp://vaadin.com', |
| 68 | + ].forEach((href) => { |
| 69 | + it(`should keep supported credits links: ${href}`, async () => { |
| 70 | + await setCreditsHref(href); |
| 71 | + expect(chart.configuration.options.credits.href).to.equal(href); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + [UNSUPPORTED_URL, ` JAVASCRIPT:void(0)`, `java\tscript:void(0)`, 'data:text/html,Text', 'vbscript:Text'].forEach( |
| 76 | + (href) => { |
| 77 | + it(`should ignore credits links that use an unsupported URL scheme: ${JSON.stringify(href)}`, async () => { |
| 78 | + await setCreditsHref(href); |
| 79 | + expect(chartContainer.querySelector('.highcharts-credits')).to.be.ok; |
| 80 | + expect(chart.configuration.options.credits.href).to.be.undefined; |
| 81 | + }); |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + it('should ignore credits links that use an unsupported URL scheme on update', async () => { |
| 86 | + chart.updateConfiguration({ credits: { enabled: true } }); |
| 87 | + await oneEvent(chart, 'chart-redraw'); |
| 88 | + chart.configuration.credits.update({ href: UNSUPPORTED_URL }); |
| 89 | + expect(chart.configuration.options.credits.href).to.be.undefined; |
| 90 | + }); |
| 91 | + }); |
| 92 | +}); |
0 commit comments