Skip to content

Commit

Permalink
revert * as react imports; clean test setup;
Browse files Browse the repository at this point in the history
  • Loading branch information
elbakerino committed Oct 2, 2024
1 parent efbecf1 commit b0a788b
Show file tree
Hide file tree
Showing 17 changed files with 42 additions and 55 deletions.
2 changes: 1 addition & 1 deletion apps/demo/src/lib/ViewSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import React from 'react'

export interface IViewSettingsContext {
lang?: string
Expand Down
2 changes: 2 additions & 0 deletions apps/demo/tests/PageHome.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { render } from '@testing-library/react'
import { contentUIMapping } from '../src/components/ContentUI'
import { PageHome } from '../src/pages/PageHome'

// rm -rf node_modules/.cache && npm run test -- --testPathPattern=PageHome --no-cache

describe('PageHome', () => {
// it('DummyText', async() => {
// const {queryByText} = render(
Expand Down
37 changes: 20 additions & 17 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ const toPackageFolder = (pkg: [name: string, folder?: string]) => {
return pkg[1] || pkg[0]
}

// todo: something in TS/babel, no matter which preset, causes `export default Comp` to not work,
// e.g. for `import Box from '@mui/material/Box'` is `console.log(Box)` logs: `Box { boxClasses: [Getter], default: [Getter] }`
// but expects to be more like: `Object { "$$typeof": Symbol("react.forward_ref"), render: Box(inProps, ref), propTypes: {…}, … }`
// todo: maybe use babel-plugin-transform-imports to configure that? but looks like it is for ESM>CJS, but allows throwing for full lib imports
// todo: For tests in ESM sometimes `React` is undefined, yet somehow could not make it reproducible.
// It is the `import React from 'react'` vs `import * as React from 'react'` thing.
// - somehow it first failed, then worked with `"jsx": "react-jsxdev"`, afterwards some kind of caching?!
// - not depending on disabling transform of packages
const base: Config.InitialProjectOptions = {
// note: presets are deprecated
// preset: 'ts-jest/presets/default', // ts/tsx to CJS, js/jsx as-is
// preset: 'ts-jest/presets/default-esm-legacy', // ts/tsx to ESM, js/jsx as-is
// preset: 'ts-jest/presets/js-with-ts',// all to CJS
// preset: 'ts-jest/presets/js-with-ts-esm',// all to ESM
// preset: 'ts-jest/presets/js-with-babel',// TS with ts-jest to CJS, other with babel-jest
// preset: 'ts-jest/presets/js-with-babel-esm',// TS with ts-jest to ESM, other with babel-jest
cacheDirectory: '<rootDir>/node_modules/.cache/jest-tmp',
transformIgnorePatterns: [
// `node_modules/?!(${[
// ...packages,
// // ['@mui'] as [name: string, folder?: string], ['@ui-schema'] as [name: string, folder?: string],
// ].map(toPackageFolder).join('|')})`,
'node_modules/(?!(@mui)/)',
`node_modules/?!(${[
...packages,
['@mui'] as [name: string, folder?: string],
// ['@ui-schema'] as [name: string, folder?: string],
].map(toPackageFolder).join('|')})/`,
// `node_modules/?!(@mui)/`,
],
transform: {
...createDefaultEsmPreset({
babelConfig: {
plugins: [
'./babelImportDefaultPlugin.js',
// 'babel-plugin-transform-require-default',
// ['transform-imports', { // not validated/checked
// 'react': {
// // 'transform': 'import * as React from 'react'',
// // 'preventFullImport': true,
// },
// }],
],
},
}).transform,// ts/tsx to ESM, js/jsx as-is
Expand All @@ -64,6 +64,9 @@ const base: Config.InitialProjectOptions = {
coveragePathIgnorePatterns: [
'(tests/.*.mock).(jsx?|tsx?|ts?|js?)$',
'.*.(test|spec).(js|ts|tsx)$',
'<rootDir>/apps/demo',
'<rootDir>/apps/sandbox',
'<rootDir>/server/sandbox',
],
extensionsToTreatAsEsm: ['.ts', '.tsx'],
}
Expand Down
14 changes: 0 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
"clean": "rimraf dist && rimraf coverage && rimraf apps/demo/build && rimraf apps/sandbox/dist && rimraf server/feed/build",
"predev": "npm run clean",
"lint": "cross-env NODE_ENV=test NODE_OPTIONS=\"--max-old-space-size=8192 --no-deprecation --experimental-vm-modules --unhandled-rejections=strict\" eslint -c=.eslintrc --ext=.tsx,.ts --max-warnings=0 'apps/demo/src' 'packages'",
"test": "cross-env NODE_ENV=development NODE_OPTIONS=\"--max-old-space-size=8192 --no-warnings --no-deprecation --experimental-vm-modules --unhandled-rejections=strict\" jest -c=\"jest.config.ts\" --passWithNoTests",
"test-cjs": "cross-env NODE_ENV=development NODE_OPTIONS=\"--max-old-space-size=8192 --no-warnings --no-deprecation --experimental-vm-modules --unhandled-rejections=strict\" jest -c=\"jest.config-cjs.ts\" --passWithNoTests",
"test": "npm run jest -- -c=\"jest.config.ts\" --passWithNoTests",
"jest": "cross-env NODE_ENV=development NODE_OPTIONS=\"--max-old-space-size=8192 --no-warnings --no-deprecation --experimental-vm-modules --unhandled-rejections=strict\" jest",
"tdd": "cross-env NODE_ENV=development npm test -- --watch --coverage=false",
"dev": "concurrently --kill-others \"npm run app:demo:start\" \"npm run server:feed:start\"",
"prebuild": "npm run clean && npm run lint && npm run test && npm run tsc",
"tsc": "tsc --project tsconfig.json --noEmit",
"tsgen": "tsc --project tsconfig.packages.json --emitDeclarationOnly --pretty",
"build": "npm run app:demo:build && npm run server:feed:build && npm run build-packages && npm run build -w content-ui-sandbox",
"postbuild": "rimraf --glob dist/**/tests",
"webpack": "cross-env NODE_ENV=production webpack --mode=production",
"webpack-dev-server": "cross-env NODE_ENV=development webpack-dev-server --mode=development --hot",
"build-packages": "npm run tsgen && npm run babel-packages",
Expand Down Expand Up @@ -86,7 +87,6 @@
"@babel/cli": "^7.25.6",
"@babel/core": "^7.25.2",
"@babel/eslint-parser": "^7.25.1",
"@babel/plugin-transform-modules-commonjs": "^7.24.8",
"@babel/preset-env": "^7.25.4",
"@babel/preset-react": "^7.24.7",
"@babel/preset-typescript": "^7.24.7",
Expand All @@ -102,7 +102,6 @@
"@typescript-eslint/parser": "^8.7.0",
"babel-jest": "^29.7.0",
"babel-loader": "^9.2.1",
"babel-plugin-add-module-exports": "^1.0.4",
"concurrently": "^9.0.1",
"copy-webpack-plugin": "^12.0.2",
"cross-env": "^7.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/md-mui/Leafs/LeafToc.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import React from 'react'
import { Heading, ListItem, Root } from 'mdast'
import { MuiLink } from '@content-ui/md-mui/MuiComponents/MuiLink'
import Typography from '@mui/material/Typography'
Expand Down
2 changes: 1 addition & 1 deletion packages/md-mui/MuiComponents/MuiLink.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import React from 'react'
import { Link as LinkBase } from 'react-router-dom'
import MuiLinkBase, { LinkProps } from '@mui/material/Link'

Expand Down
2 changes: 1 addition & 1 deletion packages/md-mui/MuiComponents/MuiNavLink.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import React from 'react'
import { useMatch } from 'react-router-dom'
import MuiLink, { LinkProps } from '@mui/material/Link'
import { NavLinkProps, RouterLink } from '@content-ui/md-mui/MuiComponents/MuiLink'
Expand Down
10 changes: 5 additions & 5 deletions packages/md-mui/Renderer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import { Fragment, memo, ReactNode } from 'react'
import { useContentContext, EditorSelection } from '@content-ui/react/useContent'
import Typography from '@mui/material/Typography'
import { defaultTocIds, LeafToc, LeafTocContextType, useLeafToc } from '@content-ui/md-mui/Leafs/LeafToc'
Expand All @@ -11,7 +11,7 @@ export interface RendererProps {
handleTocClick?: LeafTocContextType['onClick']
}

export const Renderer = ({handleTocClick, editorSelection}: RendererProps): React.ReactNode => {
export const Renderer = ({handleTocClick, editorSelection}: RendererProps): ReactNode => {
const {root} = useContentContext()
const {headlines, tocInject} = useLeafToc(root, defaultTocIds)
const bodyNodes = root?.children?.filter(c => c.type !== 'footnoteDefinition')
Expand All @@ -34,7 +34,7 @@ export const Renderer = ({handleTocClick, editorSelection}: RendererProps): Reac
(typeof tocInject?.next?.rootIndex === 'number' && tocInject?.next?.rootIndex === i + 1) ||
(typeof tocInject?.next?.rootIndex === 'undefined' && tocInject?.start?.rootIndex === i)
) ?
<React.Fragment key={i}>
<Fragment key={i}>
{/* todo: slice the whole toc area and render separately, not only injecting but "container wrapping" */}
<ContentLeaf
elem={child.type}
Expand All @@ -50,7 +50,7 @@ export const Renderer = ({handleTocClick, editorSelection}: RendererProps): Reac
tocInject={tocInject}
onClick={handleTocClick}
/> : null}
</React.Fragment> :
</Fragment> :
<ContentLeaf
key={i}
elem={child.type}
Expand All @@ -69,4 +69,4 @@ export const Renderer = ({handleTocClick, editorSelection}: RendererProps): Reac
</>
}

export const RendererMemo = React.memo(Renderer)
export const RendererMemo = memo(Renderer)
2 changes: 1 addition & 1 deletion packages/md-mui/Viewer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import React from 'react'
import Box from '@mui/material/Box'
import { ContentProcessor, EditorSelection, useContent, useContentContext, WithContent } from '@content-ui/react/useContent'
import Typography from '@mui/material/Typography'
Expand Down
1 change: 1 addition & 0 deletions packages/md-mui/tests/ViewerFromText.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { BrowserRouter } from 'react-router-dom'

// npm run test -- --testPathPattern=ViewerFrom --no-cache
// rm -rf node_modules/.cache && npm run test -- --testPathPattern=ViewerFrom --no-cache
// rm -rf node_modules && npm i && npm run test -- --testPathPattern=ViewerFrom --no-cache

describe('ViewerFromText', () => {
it('Headline', async() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/react/ContentFileProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import React from 'react'
import { Root } from 'mdast'
import { ContentFileContextType, ContentContext, ContentSelectionContext, EditorSelection } from '@content-ui/react/useContent'
import { VFile } from 'vfile'
Expand Down
4 changes: 1 addition & 3 deletions packages/react/ContentLeaf.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { RootContent } from 'mdast'
import * as React from 'react'
import React, { useMemo, memo, createContext, useContext } from 'react'
import { EditorSelection } from '@content-ui/react/useContent'
import { useSettings } from '@content-ui/react/LeafSettings'
import { DecoratorProps, DecoratorPropsNext, ReactBaseDecorator, ReactDeco } from '@content-ui/react/EngineDecorator'

const {useMemo, memo, createContext, useContext} = React

export type GenericLeafsDataSpec<D extends {} = {}> = {
[k: string]: D
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react/LeafSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import React from 'react'

export type LeafsSettings = {
followEditor?: boolean
Expand Down
4 changes: 1 addition & 3 deletions packages/react/useContent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import * as React from 'react'
import React, { useCallback, useRef, useState } from 'react'
import { Processor } from 'unified'
import { VFile } from 'vfile'
import { Root } from 'mdast'

const {useCallback, useRef, useState} = React

export interface EditorSelectionPosition {
start: number
startLine: number
Expand Down
2 changes: 1 addition & 1 deletion packages/react/useLeafFollower.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from 'react'
import React from 'react'
import { useSettings } from '@content-ui/react/LeafSettings'

export const useLeafFollower = <E extends HTMLElement = HTMLElement>(selected: boolean | undefined) => {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"resolveJsonModule": true,
"isolatedModules": false,
"noEmit": true,
"jsx": "react-jsx"
"jsx": "preserve"
},
"paths": {
"@content-ui/input": [
Expand Down

0 comments on commit b0a788b

Please sign in to comment.