Skip to content
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

feat: check is react component #49

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ jobs:
run: npm run lint && npm run ci

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
9 changes: 7 additions & 2 deletions cypress/integration/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ describe('create component', () => {
.next()
.should('not.exist')

cy.contains('[INVALID_COMPONENT]:module cannot be executed')
cy.contains('create-j.k')
.should('exist')
.next()
.should('not.exist')
.should('have.text', '[INVALID_COMPONENT]:not a valid React component')

cy.contains('create-j.l')
.should('exist')
.next()
.should('have.text', '[INVALID_COMPONENT]:not a valid React component')

cy.contains('[INVALID_COMPONENT]:module not defined')
.should('exist')
Expand Down
2 changes: 1 addition & 1 deletion public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
icon: 'appstore',
component: 'create',
path: '/create',
components: ['create-h', 'create-i', 'create-jj', 'no-defined', 'create-path-error', 'create-empty-content', 'create-j', 'create-j.j', 'create-j.k', 'create-j.no-exist', 'create-timeout', 'create-slient'],
components: ['create-h', 'create-i', 'create-jj', 'no-defined', 'create-path-error', 'create-empty-content', 'create-j', 'create-j.j', 'create-j.k', 'create-j.l', 'create-j.no-exist', 'create-timeout', 'create-slient'],
},
{
label: 'RenderComponent',
Expand Down
6 changes: 3 additions & 3 deletions src/core/component/core.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Component, ComponentType } from 'react'
import { I18n, OnMessage } from '@variousjs/various'
import { onError } from '../helper'
import { isReactComponent, onError } from '../helper'
import { isComponentLoaded, getMountedComponents } from './helper'
import {
connect,
Expand Down Expand Up @@ -142,8 +142,8 @@ export default function (
return
}

if (typeof componentNode !== 'function') {
const errorMessage = 'module cannot be executed'
if (!isReactComponent(componentNode)) {
const errorMessage = 'not a valid React component'
onError({
name: nameWidthModule,
type: ERROR_TYPE.INVALID_COMPONENT,
Expand Down
8 changes: 7 additions & 1 deletion src/core/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getStore } from './store'
import connector from './connector'
import { ERROR_TYPE, ENV_KEY, CONFIG_KEY } from '../config'
import { ErrorType } from '../types'
import { ErrorType, RequiredComponent } from '../types'

export const getEnv = () => getStore(ENV_KEY)

Expand Down Expand Up @@ -43,3 +43,9 @@ export const onError = (args: ErrorType) => {

consoleError(name, `[${prefix}] ${message}`)
}

export const isReactComponent = (component: RequiredComponent) => (
!!component.prototype?.isReactComponent) || (
typeof component === 'function'
&& String(component).includes('createElement(')
)
2 changes: 2 additions & 0 deletions test/components/create-j.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export default () => (
export const j = () => (<div>j</div>)

export const k = 'k'

export const l = () => 'l'
Loading