From 9aab44fdb6c0c8fc732b7470369370fc070d2e4e Mon Sep 17 00:00:00 2001 From: Jose Quintas Date: Thu, 28 Nov 2024 17:20:06 +0100 Subject: [PATCH] add preset spec test and add pickers to global test --- .../pickers/preset-safe/actual.spec.tsx | 12 +++++ .../pickers/preset-safe/expected.spec.tsx | 12 +++++ .../pickers/preset-safe/preset-safe.test.ts | 41 ++++++++++++++ .../src/v8.0.0/preset-safe/actual.spec.js | 15 ------ .../src/v8.0.0/preset-safe/expected.spec.js | 18 ------- .../x-codemod/src/v8.0.0/preset-safe/index.ts | 2 + .../v8.0.0/preset-safe/preset-safe.test.ts | 53 +++++++++++++++---- 7 files changed, 109 insertions(+), 44 deletions(-) create mode 100644 packages/x-codemod/src/v8.0.0/pickers/preset-safe/actual.spec.tsx create mode 100644 packages/x-codemod/src/v8.0.0/pickers/preset-safe/expected.spec.tsx create mode 100644 packages/x-codemod/src/v8.0.0/pickers/preset-safe/preset-safe.test.ts delete mode 100644 packages/x-codemod/src/v8.0.0/preset-safe/actual.spec.js delete mode 100644 packages/x-codemod/src/v8.0.0/preset-safe/expected.spec.js diff --git a/packages/x-codemod/src/v8.0.0/pickers/preset-safe/actual.spec.tsx b/packages/x-codemod/src/v8.0.0/pickers/preset-safe/actual.spec.tsx new file mode 100644 index 000000000000..fc0a0af5b8cd --- /dev/null +++ b/packages/x-codemod/src/v8.0.0/pickers/preset-safe/actual.spec.tsx @@ -0,0 +1,12 @@ +// @ts-nocheck +import { FieldValueType } from '@mui/x-date-pickers/models'; + +interface DumbComponentProps { + valueType: FieldValueType; + foo: string; + bar: number; +} + +const myFunction = (param: FieldValueType) => { + console.log('Hello World!'); +}; diff --git a/packages/x-codemod/src/v8.0.0/pickers/preset-safe/expected.spec.tsx b/packages/x-codemod/src/v8.0.0/pickers/preset-safe/expected.spec.tsx new file mode 100644 index 000000000000..005a926c5056 --- /dev/null +++ b/packages/x-codemod/src/v8.0.0/pickers/preset-safe/expected.spec.tsx @@ -0,0 +1,12 @@ +// @ts-nocheck +import { PickerValueType } from '@mui/x-date-pickers/models'; + +interface DumbComponentProps { + valueType: PickerValueType; + foo: string; + bar: number; +} + +const myFunction = (param: PickerValueType) => { + console.log('Hello World!'); +}; diff --git a/packages/x-codemod/src/v8.0.0/pickers/preset-safe/preset-safe.test.ts b/packages/x-codemod/src/v8.0.0/pickers/preset-safe/preset-safe.test.ts new file mode 100644 index 000000000000..5129efe68f31 --- /dev/null +++ b/packages/x-codemod/src/v8.0.0/pickers/preset-safe/preset-safe.test.ts @@ -0,0 +1,41 @@ +import path from 'path'; +import { expect } from 'chai'; +import jscodeshift from 'jscodeshift'; +import transform from './index'; +import readFile from '../../../util/readFile'; + +function read(fileName) { + return readFile(path.join(__dirname, fileName)); +} + +describe('v8.0.0/pickers', () => { + describe('preset-safe', () => { + it('transforms code as needed', () => { + const actual = transform( + { + source: read('./actual.spec.tsx'), + path: require.resolve('./actual.spec.tsx'), + }, + { jscodeshift: jscodeshift.withParser('tsx') }, + {}, + ); + + const expected = read('./expected.spec.tsx'); + expect(actual).to.equal(expected, 'The transformed version should be correct'); + }); + + it('should be idempotent for expression', () => { + const actual = transform( + { + source: read('./expected.spec.tsx'), + path: require.resolve('./expected.spec.tsx'), + }, + { jscodeshift: jscodeshift.withParser('tsx') }, + {}, + ); + + const expected = read('./expected.spec.tsx'); + expect(actual).to.equal(expected, 'The transformed version should be correct'); + }); + }); +}); diff --git a/packages/x-codemod/src/v8.0.0/preset-safe/actual.spec.js b/packages/x-codemod/src/v8.0.0/preset-safe/actual.spec.js deleted file mode 100644 index f8a5b4f02aa2..000000000000 --- a/packages/x-codemod/src/v8.0.0/preset-safe/actual.spec.js +++ /dev/null @@ -1,15 +0,0 @@ -// @ts-nocheck -import * as React from 'react'; -import { TreeItem2 } from '@mui/x-tree-view/TreeItem2'; -import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView'; -import { PieChart } from '@mui/x-charts/PieChart'; - -const className = treeViewClasses.root; - -// prettier-ignore - - - - - - diff --git a/packages/x-codemod/src/v8.0.0/preset-safe/expected.spec.js b/packages/x-codemod/src/v8.0.0/preset-safe/expected.spec.js deleted file mode 100644 index b85fe4c34ed3..000000000000 --- a/packages/x-codemod/src/v8.0.0/preset-safe/expected.spec.js +++ /dev/null @@ -1,18 +0,0 @@ -// @ts-nocheck -import * as React from 'react'; -import { TreeItem } from '@mui/x-tree-view/TreeItem'; -import { SimpleTreeView } from '@mui/x-tree-view/SimpleTreeView'; -import { PieChart } from '@mui/x-charts/PieChart'; - -const className = treeViewClasses.root; - -// prettier-ignore - - - - - - diff --git a/packages/x-codemod/src/v8.0.0/preset-safe/index.ts b/packages/x-codemod/src/v8.0.0/preset-safe/index.ts index 92c7cb70ec21..22dbdb1f79e4 100644 --- a/packages/x-codemod/src/v8.0.0/preset-safe/index.ts +++ b/packages/x-codemod/src/v8.0.0/preset-safe/index.ts @@ -1,9 +1,11 @@ import transformTreeView from '../tree-view/preset-safe'; +import transformPickers from '../pickers/preset-safe'; import transformCharts from '../charts/preset-safe'; import { JsCodeShiftAPI, JsCodeShiftFileInfo } from '../../types'; export default function transformer(file: JsCodeShiftFileInfo, api: JsCodeShiftAPI, options: any) { file.source = transformTreeView(file, api, options); + file.source = transformPickers(file, api, options); file.source = transformCharts(file, api, options); return file.source; diff --git a/packages/x-codemod/src/v8.0.0/preset-safe/preset-safe.test.ts b/packages/x-codemod/src/v8.0.0/preset-safe/preset-safe.test.ts index b0ea5e858c7a..84b3d9c6e9af 100644 --- a/packages/x-codemod/src/v8.0.0/preset-safe/preset-safe.test.ts +++ b/packages/x-codemod/src/v8.0.0/preset-safe/preset-safe.test.ts @@ -1,27 +1,58 @@ import path from 'path'; import { expect } from 'chai'; import jscodeshift from 'jscodeshift'; +import fs from 'fs'; import transform from '.'; import readFile from '../../util/readFile'; function read(fileName) { - return readFile(path.join(__dirname, fileName)); + return readFile(fileName); +} + +function getAllDirs() { + return fs + .readdirSync(path.resolve(__dirname, '..')) + .filter( + (file) => + fs.statSync(path.resolve(__dirname, '..', file)).isDirectory() && file !== 'preset-safe', + ); } describe('v8.0.0', () => { - describe('preset-safe', () => { - it('transforms code as needed', () => { - const actual = transform({ source: read('./actual.spec.js') }, { jscodeshift }, {}); + const MOD_DIRS = getAllDirs(); - const expected = read('./expected.spec.js'); - expect(actual).to.equal(expected, 'The transformed version should be correct'); - }); + describe('preset-safe', () => { + MOD_DIRS.forEach((testDir) => { + const actualPath = path.resolve(__dirname, '..', testDir, 'preset-safe', 'actual.spec.tsx'); + const expectedPath = path.resolve( + __dirname, + '..', + testDir, + 'preset-safe', + 'expected.spec.tsx', + ); - it('should be idempotent', () => { - const actual = transform({ source: read('./expected.spec.js') }, { jscodeshift }, {}); + describe(`${testDir.replace(/-/g, ' ')}`, () => { + it('transforms code as needed', () => { + const actual = transform( + { source: read(actualPath) }, + { jscodeshift: jscodeshift.withParser('tsx') }, + {}, + ); + const expected = read(expectedPath); + expect(actual).to.equal(expected, 'The transformed version should be correct'); + }); - const expected = read('./expected.spec.js'); - expect(actual).to.equal(expected, 'The transformed version should be correct'); + it('should be idempotent', () => { + const actual = transform( + { source: read(expectedPath) }, + { jscodeshift: jscodeshift.withParser('tsx') }, + {}, + ); + const expected = read(expectedPath); + expect(actual).to.equal(expected, 'The transformed version should be correct'); + }); + }); }); }); });