-
Notifications
You must be signed in to change notification settings - Fork 56
[NOJIRA] [FIX] [V2] Remove flexShrink:1 from SVG transformation on Babel Plugin #1275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`SessionReplayView.Privacy SVG Wrapper should wrap an SVG without explicit dimensions 1`] = ` | ||
| "import { SessionReplayView } from "@datadog/mobile-react-native-session-replay"; | ||
| /*#__PURE__*/React.createElement(SessionReplayView.Privacy, { | ||
| nativeID: "00000000-0000-0000-0000-000000000000", | ||
| collapsable: false, | ||
| pointerEvents: "box-none", | ||
| attributes: { | ||
| type: "svg", | ||
| hash: "fd87d9136a0280917b215dfeeef27092" | ||
| } | ||
| }, /*#__PURE__*/React.createElement(Svg, { | ||
| viewBox: "0 0 100 100" | ||
| }, /*#__PURE__*/React.createElement(Circle, { | ||
| cx: "50", | ||
| cy: "50", | ||
| r: "40", | ||
| fill: "blue" | ||
| })));" | ||
| `; | ||
|
|
||
| exports[`SessionReplayView.Privacy SVG Wrapper should wrap an inline SVG with the correct props 1`] = ` | ||
| "import { SessionReplayView } from "@datadog/mobile-react-native-session-replay"; | ||
| globalThis.__DD_RN_BABEL_PLUGIN_ENABLED__ = true; | ||
| /*#__PURE__*/React.createElement(SessionReplayView.Privacy, { | ||
| nativeID: "00000000-0000-0000-0000-000000000000", | ||
| collapsable: false, | ||
| pointerEvents: "box-none", | ||
| attributes: { | ||
| type: "svg", | ||
| hash: "5682723a5f88d4aa4e8c946f82eef34b", | ||
| width: "24", | ||
| height: "24" | ||
| } | ||
| }, /*#__PURE__*/React.createElement(Svg, { | ||
| width: "24", | ||
| height: "24" | ||
| }, /*#__PURE__*/React.createElement(Path, { | ||
| d: "M12 2L2 12h10z", | ||
| fill: "black" | ||
| })));" | ||
| `; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,11 +5,17 @@ | |
| */ | ||
|
|
||
| /* eslint quotes: ["off"] */ | ||
| import { transform } from '@babel/core'; | ||
| import * as parser from '@babel/parser'; | ||
| import traverse from '@babel/traverse'; | ||
| import * as t from '@babel/types'; | ||
| import fs from 'fs'; | ||
| import os from 'os'; | ||
| import path from 'path'; | ||
|
|
||
| import plugin from '../src/index'; | ||
| import { RNSvgHandler } from '../src/libraries/react-native-svg/handlers/RNSvgHandler'; | ||
| import { ReactNativeSVG } from '../src/libraries/react-native-svg'; | ||
|
|
||
| /** | ||
| * Helper function to test SVG transformation | ||
|
|
@@ -23,12 +29,12 @@ function transformSvg(code: string): string | undefined { | |
| let result: string | undefined; | ||
|
|
||
| traverse(ast, { | ||
| JSXElement(path) { | ||
| if (t.isJSXIdentifier(path.node.openingElement.name)) { | ||
| const name = path.node.openingElement.name.name; | ||
| JSXElement(nodePath) { | ||
| if (t.isJSXIdentifier(nodePath.node.openingElement.name)) { | ||
| const name = nodePath.node.openingElement.name.name; | ||
| if (name === 'Svg') { | ||
| const dimensions: Record<string, string> = {}; | ||
| const handler = new RNSvgHandler(t, path, name); | ||
| const handler = new RNSvgHandler(t, nodePath, name); | ||
| result = handler.transformSvgNode(dimensions); | ||
| } | ||
| } | ||
|
|
@@ -756,3 +762,68 @@ describe('React Native SVG Processing - RNSvgHandler', () => { | |
| }); | ||
| }); | ||
| }); | ||
|
|
||
| jest.mock('uuid', () => ({ | ||
| v4: () => '00000000-0000-0000-0000-000000000000' | ||
| })); | ||
|
|
||
| /** | ||
| * Helper to run the full babel plugin with SVG tracking enabled. | ||
| * Returns the transformed code string. | ||
| */ | ||
| function transformWithSvgTracking(code: string): string | undefined { | ||
| const tmpDir = path.join(os.tmpdir(), 'dd-svg-test-assets'); | ||
| const reactNativeSVG = new ReactNativeSVG(process.cwd(), tmpDir); | ||
|
|
||
| return transform(code, { | ||
| filename: 'file.tsx', | ||
| presets: ['@babel/preset-react', '@babel/preset-typescript'], | ||
| plugins: [ | ||
| [ | ||
| plugin, | ||
| { | ||
| sessionReplay: { svgTracking: true }, | ||
| __internal_reactNativeSVG: reactNativeSVG | ||
| } | ||
| ] | ||
| ], | ||
| configFile: false | ||
| })?.code as string | undefined; | ||
|
Comment on lines
+778
to
+791
|
||
| } | ||
|
|
||
| describe('SessionReplayView.Privacy SVG Wrapper', () => { | ||
| const tmpDir = path.join(os.tmpdir(), 'dd-svg-test-assets'); | ||
|
|
||
| afterAll(() => { | ||
| try { | ||
| fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
| } catch { | ||
| /* ignore cleanup errors */ | ||
| } | ||
| }); | ||
|
|
||
| it('should wrap an inline SVG with the correct props', () => { | ||
| const input = | ||
| '<Svg width="24" height="24"><Path d="M12 2L2 12h10z" fill="black" /></Svg>'; | ||
| const output = transformWithSvgTracking(input); | ||
|
|
||
| expect(output).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('should wrap an SVG without explicit dimensions', () => { | ||
| const input = | ||
| '<Svg viewBox="0 0 100 100"><Circle cx="50" cy="50" r="40" fill="blue" /></Svg>'; | ||
| const output = transformWithSvgTracking(input); | ||
|
|
||
| expect(output).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| it('should not set a style prop on the wrapper', () => { | ||
| const input = | ||
| '<Svg width="24" height="24"><Path d="M12 2L2 12h10z" fill="black" /></Svg>'; | ||
| const output = transformWithSvgTracking(input); | ||
|
|
||
| expect(output).not.toContain('flexShrink'); | ||
| expect(output).not.toContain('style'); | ||
| }); | ||
|
Comment on lines
+821
to
+828
|
||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test uses a fixed temp directory name (
os.tmpdir()/dd-svg-test-assets). When Jest runs in parallel workers, multiple suites/processes can write/remove the same directory, causing racy failures and non-deterministic snapshots. Use a unique temp dir per test file run (e.g.,fs.mkdtempSync(path.join(os.tmpdir(), 'dd-svg-test-assets-'))or includeprocess.pid/JEST_WORKER_ID), and pass that directory intotransformWithSvgTrackingand the cleanup hook.