|
| 1 | +/** |
| 2 | + * Consumer-side checks for the `relayout`, `restyle` and `update` types. |
| 3 | + * |
| 4 | + * Nothing here runs. `npm run test-types` compiles this file against the public entry point. |
| 5 | + * A line marked `@ts-expect-error` must fail to compile, so the build breaks if a type becomes too permissive. |
| 6 | + */ |
| 7 | + |
| 8 | +import * as Plotly from '../../lib/index'; |
| 9 | + |
| 10 | +declare const gd: HTMLElement; |
| 11 | + |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | +// relayout |
| 14 | +// --------------------------------------------------------------------------- |
| 15 | + |
| 16 | +Plotly.relayout(gd, { 'xaxis.autorange': true }); |
| 17 | +Plotly.relayout(gd, { 'xaxis.range[0]': 0, 'xaxis.range[1]': 5 }); |
| 18 | +Plotly.relayout(gd, { 'xaxis.title.font.size': 14 }); |
| 19 | +Plotly.relayout(gd, { 'annotations[0].text': 'hello' }); |
| 20 | +Plotly.relayout(gd, { 'updatemenus[0].buttons[1].label': 'hello' }); |
| 21 | +Plotly.relayout(gd, { xaxis: { autorange: true } }); |
| 22 | +Plotly.relayout(gd, { autosize: true }); |
| 23 | +Plotly.relayout(gd, 'xaxis.autorange', true); |
| 24 | +Plotly.relayout(gd, 'annotations[0].text', 'hello'); |
| 25 | + |
| 26 | +// @ts-expect-error top-level typo |
| 27 | +Plotly.relayout(gd, { titl: 'abc' }); |
| 28 | +// @ts-expect-error wrong value type on a known top-level key |
| 29 | +Plotly.relayout(gd, { autosize: 'yes' }); |
| 30 | + |
| 31 | +// A wrapper that reads the parameter types must see both call forms |
| 32 | +type RelayoutArg = Parameters<typeof Plotly.relayout>[1]; |
| 33 | +export const relayoutObject: RelayoutArg = { autosize: true }; |
| 34 | +export const relayoutPath: RelayoutArg = 'xaxis.autorange'; |
| 35 | + |
| 36 | +// --------------------------------------------------------------------------- |
| 37 | +// restyle |
| 38 | +// --------------------------------------------------------------------------- |
| 39 | + |
| 40 | +Plotly.restyle(gd, { 'marker.color': 'red' }); |
| 41 | +Plotly.restyle(gd, { 'marker.color': 'red' }, [0]); |
| 42 | +Plotly.restyle(gd, { 'line.width': 2, 'marker.size[0]': 10 }, 1); |
| 43 | +Plotly.restyle(gd, { visible: false }); |
| 44 | +Plotly.restyle(gd, 'marker.color', 'red'); |
| 45 | +Plotly.restyle(gd, 'marker.color', ['red', 'blue'], [0, 1]); |
| 46 | + |
| 47 | +// @ts-expect-error top-level typo |
| 48 | +Plotly.restyle(gd, { colr: 'red' }); |
| 49 | +// @ts-expect-error wrong value type on a known top-level key |
| 50 | +Plotly.restyle(gd, { visible: 'maybe' }); |
| 51 | + |
| 52 | +type RestyleArg = Parameters<typeof Plotly.restyle>[1]; |
| 53 | +export const restyleObject: RestyleArg = { visible: false }; |
| 54 | +export const restylePath: RestyleArg = 'marker.color'; |
| 55 | + |
| 56 | +// --------------------------------------------------------------------------- |
| 57 | +// update |
| 58 | +// --------------------------------------------------------------------------- |
| 59 | + |
| 60 | +Plotly.update(gd, { 'marker.color': 'red' }, { 'xaxis.range[0]': 0 }); |
| 61 | +Plotly.update(gd, { 'marker.color': 'red' }, { 'xaxis.range[0]': 0 }, [0]); |
| 62 | +Plotly.update(gd, { visible: false }, { autosize: true }); |
| 63 | + |
| 64 | +// @ts-expect-error top-level typo in the trace update |
| 65 | +Plotly.update(gd, { colr: 'red' }, { autosize: true }); |
| 66 | +// @ts-expect-error top-level typo in the layout update |
| 67 | +Plotly.update(gd, { visible: false }, { titl: 'abc' }); |
| 68 | + |
| 69 | +// --------------------------------------------------------------------------- |
| 70 | +// APIs that take a full figure must stay strict about unknown keys |
| 71 | +// --------------------------------------------------------------------------- |
| 72 | + |
| 73 | +Plotly.newPlot(gd, [], { autosize: true }); |
| 74 | +Plotly.validate([], { autosize: true }); |
| 75 | + |
| 76 | +// @ts-expect-error a dotted path is not a valid key in a full layout |
| 77 | +Plotly.newPlot(gd, [], { 'xaxis.autorange': true }); |
| 78 | +// @ts-expect-error a dotted typo must not pass in a full layout |
| 79 | +Plotly.newPlot(gd, [], { 'xaxis.autrange': true }); |
| 80 | +// @ts-expect-error a dotted path is not a valid key when validating a layout |
| 81 | +Plotly.validate([], { 'xaxis.autorange': true }); |
| 82 | +// @ts-expect-error a dotted path is not a valid key in a template layout |
| 83 | +const template: Plotly.Template = { layout: { 'xaxis.autorange': true } }; |
0 commit comments