From d1f6245bcd1b805102f096db792ea4c06a94bfd5 Mon Sep 17 00:00:00 2001 From: Geido <60598000+geido@users.noreply.github.com> Date: Wed, 10 Mar 2021 22:29:59 +0200 Subject: [PATCH] test: Tests, dedicated directory and Storybook for the Badge component (#13513) * Move to dir and add storybook * Add tests * Remove no-restricted-imports --- .../components/TemplateParamsEditor.jsx | 2 +- .../src/common/components/common.stories.tsx | 10 - .../src/common/components/index.tsx | 1 - .../src/components/Badge/Badge.stories.tsx | 172 ++++++++++++++++++ .../src/components/Badge/Badge.test.tsx | 50 +++++ .../Badge.tsx => components/Badge/index.tsx} | 3 +- .../src/datasource/DatasourceEditor.jsx | 2 +- .../src/profile/components/Security.tsx | 2 +- 8 files changed, 226 insertions(+), 16 deletions(-) create mode 100644 superset-frontend/src/components/Badge/Badge.stories.tsx create mode 100644 superset-frontend/src/components/Badge/Badge.test.tsx rename superset-frontend/src/{common/components/Badge.tsx => components/Badge/index.tsx} (93%) diff --git a/superset-frontend/src/SqlLab/components/TemplateParamsEditor.jsx b/superset-frontend/src/SqlLab/components/TemplateParamsEditor.jsx index 8c02e0f36cad8..50d26db7295d1 100644 --- a/superset-frontend/src/SqlLab/components/TemplateParamsEditor.jsx +++ b/superset-frontend/src/SqlLab/components/TemplateParamsEditor.jsx @@ -18,7 +18,7 @@ */ import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; -import Badge from 'src/common/components/Badge'; +import Badge from 'src/components/Badge'; import { t, styled } from '@superset-ui/core'; import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls'; import { debounce } from 'lodash'; diff --git a/superset-frontend/src/common/components/common.stories.tsx b/superset-frontend/src/common/components/common.stories.tsx index d0f7dce692311..3a19041558ea7 100644 --- a/superset-frontend/src/common/components/common.stories.tsx +++ b/superset-frontend/src/common/components/common.stories.tsx @@ -32,7 +32,6 @@ import { DatePicker as AntdDatePicker, RangePicker as AntdRangePicker, } from './DatePicker'; -import Badge from './Badge'; import ProgressBar from './ProgressBar'; import { CronPicker, CronError } from './CronPicker'; @@ -254,15 +253,6 @@ export const Switch = () => ( ); -export const BadgeDefault = () => ; -export const BadgeColored = () => ; -export const BadgeTextColored = () => ( - -); -export const BadgeSuccess = () => ; -export const BadgeError = () => ; -export const BadgeSmall = () => ; - export function StyledCronPicker() { // @ts-ignore const inputRef = useRef(null); diff --git a/superset-frontend/src/common/components/index.tsx b/superset-frontend/src/common/components/index.tsx index c3ce3f39e8cbc..4f4ece2de4bba 100644 --- a/superset-frontend/src/common/components/index.tsx +++ b/superset-frontend/src/common/components/index.tsx @@ -57,7 +57,6 @@ export { TreeProps } from 'antd/lib/tree'; export { FormInstance } from 'antd/lib/form'; export { RadioChangeEvent } from 'antd/lib/radio'; -export { default as Badge } from './Badge'; export { default as Collapse } from './Collapse'; export { default as Progress } from './ProgressBar'; diff --git a/superset-frontend/src/components/Badge/Badge.stories.tsx b/superset-frontend/src/components/Badge/Badge.stories.tsx new file mode 100644 index 0000000000000..2ae1ac3455e22 --- /dev/null +++ b/superset-frontend/src/components/Badge/Badge.stories.tsx @@ -0,0 +1,172 @@ +/** + * 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 Badge, { BadgeProps } from '.'; + +export default { + title: 'Badge', + component: Badge, +}; + +type BadgeColor = Pick; +type BadgeColorValue = BadgeColor[keyof BadgeColor]; +type BadgeSize = Pick; +type BadgeSizeValue = BadgeSize[keyof BadgeSize]; + +const badgeColors: BadgeColorValue[] = [ + 'pink', + 'red', + 'yellow', + 'orange', + 'cyan', + 'green', + 'blue', + 'purple', + 'geekblue', + 'magenta', + 'volcano', + 'gold', + 'lime', +]; +const badgeSizes: BadgeSizeValue[] = ['default', 'small']; +const STATUSES = ['default', 'error', 'warning', 'success', 'processing']; + +const COLORS = { + label: 'colors', + options: badgeColors, + defaultValue: undefined, +}; + +const SIZES = { + label: 'sizes', + options: badgeSizes, + defaultValue: undefined, +}; + +export const InteractiveBadge = (args: BadgeProps) => ; + +InteractiveBadge.args = { + count: null, + color: null, + text: 'Text', + textColor: null, + status: 'success', + size: 'default', +}; + +InteractiveBadge.argTypes = { + status: { + control: { + type: 'select', + options: [undefined, ...STATUSES], + }, + }, + size: { + control: { + type: 'select', + options: SIZES.options, + }, + }, + color: { + control: { + type: 'select', + options: [undefined, ...COLORS.options], + }, + }, + textColor: { + control: { + type: 'select', + options: [undefined, ...COLORS.options], + }, + }, + count: { + control: { + type: 'select', + options: [undefined, ...Array(100).keys()], + }, + }, +}; + +InteractiveBadge.story = { + parameters: { + knobs: { + disable: true, + }, + }, +}; + +export const BadgeGallery = () => ( + <> + {SIZES.options.map(size => ( +
+

{size}

+ {COLORS.options.map(color => ( + + ))} +
+ ))} + +); + +export const BadgeTextGallery = () => ( + <> + {COLORS.options.map(color => ( + + ))} + +); + +BadgeGallery.story = { + parameters: { + actions: { + disable: true, + }, + controls: { + disable: true, + }, + knobs: { + disable: true, + }, + }, +}; + +BadgeTextGallery.story = { + parameters: { + actions: { + disable: true, + }, + controls: { + disable: true, + }, + knobs: { + disable: true, + }, + }, +}; diff --git a/superset-frontend/src/components/Badge/Badge.test.tsx b/superset-frontend/src/components/Badge/Badge.test.tsx new file mode 100644 index 0000000000000..01fb98c9dfa4b --- /dev/null +++ b/superset-frontend/src/components/Badge/Badge.test.tsx @@ -0,0 +1,50 @@ +/** + * 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 Badge from '.'; + +const mockedProps = { + count: 9, + text: 'Text', + textColor: 'orange', +}; + +test('should render', () => { + const { container } = render(); + expect(container).toBeInTheDocument(); +}); + +test('should render the count', () => { + render(); + expect(screen.getAllByText('9')[0]).toBeInTheDocument(); +}); + +test('should render the text', () => { + render(); + expect(screen.getByText('Text')).toBeInTheDocument(); +}); + +test('should render with the chosen textColor', () => { + render(); + const badge = screen.getAllByText('9')[0]; + expect(badge).toHaveStyle(` + color: 'orange'; + `); +}); diff --git a/superset-frontend/src/common/components/Badge.tsx b/superset-frontend/src/components/Badge/index.tsx similarity index 93% rename from superset-frontend/src/common/components/Badge.tsx rename to superset-frontend/src/components/Badge/index.tsx index ea5247fb9c4d8..8dfe3cbd37320 100644 --- a/superset-frontend/src/common/components/Badge.tsx +++ b/superset-frontend/src/components/Badge/index.tsx @@ -18,11 +18,10 @@ */ import React from 'react'; import { styled } from '@superset-ui/core'; -// eslint-disable-next-line no-restricted-imports import { Badge as AntdBadge } from 'antd'; import { BadgeProps as AntdBadgeProps } from 'antd/lib/badge'; -interface BadgeProps extends AntdBadgeProps { +export interface BadgeProps extends AntdBadgeProps { textColor?: string; } diff --git a/superset-frontend/src/datasource/DatasourceEditor.jsx b/superset-frontend/src/datasource/DatasourceEditor.jsx index 29f5b7099595d..315d5d6555d97 100644 --- a/superset-frontend/src/datasource/DatasourceEditor.jsx +++ b/superset-frontend/src/datasource/DatasourceEditor.jsx @@ -21,7 +21,7 @@ import PropTypes from 'prop-types'; import { Col, Well } from 'react-bootstrap'; import { Radio } from 'src/common/components/Radio'; import Alert from 'src/components/Alert'; -import Badge from 'src/common/components/Badge'; +import Badge from 'src/components/Badge'; import shortid from 'shortid'; import { styled, SupersetClient, t, supersetTheme } from '@superset-ui/core'; import Button from 'src/components/Button'; diff --git a/superset-frontend/src/profile/components/Security.tsx b/superset-frontend/src/profile/components/Security.tsx index 2c04bbb68078d..078fc42e4c74b 100644 --- a/superset-frontend/src/profile/components/Security.tsx +++ b/superset-frontend/src/profile/components/Security.tsx @@ -17,7 +17,7 @@ * under the License. */ import React from 'react'; -import Badge from 'src/common/components/Badge'; +import Badge from 'src/components/Badge'; import { t } from '@superset-ui/core'; import Label from 'src/components/Label';