diff --git a/superset-frontend/src/components/AsyncEsmComponent/AsyncEsmComponent.stories.tsx b/superset-frontend/src/components/AsyncEsmComponent/AsyncEsmComponent.stories.tsx new file mode 100644 index 0000000000..415ea4b4fa --- /dev/null +++ b/superset-frontend/src/components/AsyncEsmComponent/AsyncEsmComponent.stories.tsx @@ -0,0 +1,51 @@ +/** + * 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 React from 'react'; +import AsyncEsmComponent, { PlaceholderProps } from '.'; + +export default { + title: 'AsyncEsmComponent', +}; + +const Placeholder = () => Loading...; + +const AsyncComponent = ({ bold }: { bold: boolean }) => ( + AsyncComponent +); + +const Component = AsyncEsmComponent( + new Promise(resolve => setTimeout(() => resolve(AsyncComponent), 3000)), + Placeholder, +); + +export const InteractiveEsmComponent = (args: PlaceholderProps) => ( + +); + +InteractiveEsmComponent.args = { + bold: true, +}; + +InteractiveEsmComponent.story = { + parameters: { + knobs: { + disable: true, + }, + }, +}; diff --git a/superset-frontend/src/components/AsyncEsmComponent/AsyncEsmComponent.test.tsx b/superset-frontend/src/components/AsyncEsmComponent/AsyncEsmComponent.test.tsx new file mode 100644 index 0000000000..75e9e5e0a4 --- /dev/null +++ b/superset-frontend/src/components/AsyncEsmComponent/AsyncEsmComponent.test.tsx @@ -0,0 +1,60 @@ +/** + * 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 React from 'react'; +import { render, screen } from 'spec/helpers/testing-library'; +import AsyncEsmComponent from 'src/components/AsyncEsmComponent'; + +const Placeholder = () => Loading...; + +const AsyncComponent = ({ bold }: { bold: boolean }) => ( + AsyncComponent +); + +const ComponentPromise = new Promise(resolve => + setTimeout(() => resolve(AsyncComponent), 500), +); + +test('renders without placeholder', async () => { + const Component = AsyncEsmComponent(ComponentPromise); + render(); + expect(screen.queryByRole('status')).not.toBeInTheDocument(); + expect(await screen.findByText('AsyncComponent')).toBeInTheDocument(); +}); + +test('renders with default placeholder', async () => { + const Component = AsyncEsmComponent(ComponentPromise); + render(); + expect(screen.getByRole('status')).toBeInTheDocument(); + expect(await screen.findByText('AsyncComponent')).toBeInTheDocument(); +}); + +test('renders with custom placeholder', async () => { + const Component = AsyncEsmComponent(ComponentPromise, Placeholder); + render(); + expect(screen.getByText('Loading...')).toBeInTheDocument(); + expect(await screen.findByText('AsyncComponent')).toBeInTheDocument(); +}); + +test('renders with custom props', async () => { + const Component = AsyncEsmComponent(ComponentPromise, Placeholder); + render(); + const asyncComponent = await screen.findByText('AsyncComponent'); + expect(asyncComponent).toBeInTheDocument(); + expect(asyncComponent).toHaveStyle({ fontWeight: 700 }); +}); diff --git a/superset-frontend/src/components/AsyncEsmComponent.tsx b/superset-frontend/src/components/AsyncEsmComponent/index.tsx similarity index 99% rename from superset-frontend/src/components/AsyncEsmComponent.tsx rename to superset-frontend/src/components/AsyncEsmComponent/index.tsx index a502926f31..c96368ca60 100644 --- a/superset-frontend/src/components/AsyncEsmComponent.tsx +++ b/superset-frontend/src/components/AsyncEsmComponent/index.tsx @@ -17,7 +17,7 @@ * under the License. */ import React, { CSSProperties, useEffect, useState, RefObject } from 'react'; -import Loading from './Loading'; +import Loading from '../Loading'; export type PlaceholderProps = { showLoadingForImport?: boolean;