sfirke commented on code in PR #43722: URL: https://github.com/apache/superset/pull/43722#discussion_r3898590603
########## superset-frontend/plugins/plugin-chart-handlebars/test/Handlebars.test.tsx: ########## @@ -0,0 +1,76 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +import { render } from '@superset-ui/core/spec'; +import Handlebars from '../src/Handlebars'; +import { HandlebarsProps } from '../src/types'; + +const templateSources: string[] = []; + +jest.mock('../src/components/Handlebars/HandlebarsViewer', () => ({ + HandlebarsViewer: ({ templateSource }: { templateSource: string }) => { + templateSources.push(templateSource); + return null; + }, +})); + +const renderChart = (formData: Partial<HandlebarsProps['formData']>) => { + templateSources.length = 0; + render( + <Handlebars + {...({ + data: [], + height: 100, + width: 100, + formData, + } as unknown as HandlebarsProps)} + />, + ); + return templateSources[0]; +}; + +test('puts the style block before the template, separated by a blank line', () => { Review Comment: Good catch — agreed, and fixed in 8a0e290. You're right that the old test was a change detector: it mocked out `HandlebarsViewer` and asserted on the concatenated string, so it pinned the implementation rather than the behavior and would have passed even if the pipeline still rendered the CSS as chart content. The test now renders the chart through the real Markdown pipeline and asserts on the DOM: the `<style>` block contains the configured CSS verbatim, and the chart's visible text (everything outside `<style>`) is just the table content. Two wrinkles worth flagging for anyone reading the diff: - `spec/helpers/shim.tsx` stubs `react-markdown`, `rehype-raw` and `rehype-sanitize` globally to dodge ESM parsing in Jest, so nothing is ever actually parsed. This suite opts back in via `jest.requireActual`, which meant allowing `react-markdown` and the ESM-only remark/rehype/vfile packages through `transformIgnorePatterns`. The stub stays in place for every other suite, and I confirmed `SafeMarkdown`, dashboard `Markdown` and `SliceInfo` still pass. - `style` isn't in the sanitizer's default allowlist — `hast-util-sanitize` unwraps the element and leaves its text behind — so the CSS only reaches the DOM where an operator has allowed the tag via `HTML_SANITIZATION_SCHEMA_EXTENSIONS`. The test sets that in the `#app` bootstrap data, otherwise there'd be no style block to assert on. To check the test actually earns its keep, I reverted `Handlebars.tsx` to the old concatenation and confirmed it fails. The failure is nicely specific — the universal selector after the blank line gets parsed as a Markdown list bullet, so the rule lands in the style block mangled: ``` - * { + { - font-family: monospace; + font-family: monospace; - } + }</style> ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
