From 45cca3a945c04002733356425312e7eecfba8fba Mon Sep 17 00:00:00 2001 From: Bruno Motta Date: Thu, 25 Feb 2021 20:48:34 -0300 Subject: [PATCH] test: CacheLabel (#13318) * add HTMLAttributes on Label Component * Split TooltipContent * moving "CacheLabel.jsx" to "CacheLabel" folder * remove "data-test-id" * Fix timestamp name * removing unused file --- .../src/components/CachedLabel.jsx | 88 ------------------- .../CachedLabel/CachedLabel.test.tsx | 39 ++++++++ .../CachedLabel/TooltipContent.test.tsx | 39 ++++++++ .../components/CachedLabel/TooltipContent.tsx | 42 +++++++++ .../src/components/CachedLabel/index.tsx | 57 ++++++++++++ .../src/components/Label/index.tsx | 2 +- 6 files changed, 178 insertions(+), 89 deletions(-) delete mode 100644 superset-frontend/src/components/CachedLabel.jsx create mode 100644 superset-frontend/src/components/CachedLabel/CachedLabel.test.tsx create mode 100644 superset-frontend/src/components/CachedLabel/TooltipContent.test.tsx create mode 100644 superset-frontend/src/components/CachedLabel/TooltipContent.tsx create mode 100644 superset-frontend/src/components/CachedLabel/index.tsx diff --git a/superset-frontend/src/components/CachedLabel.jsx b/superset-frontend/src/components/CachedLabel.jsx deleted file mode 100644 index a1f5a0c8a..000000000 --- a/superset-frontend/src/components/CachedLabel.jsx +++ /dev/null @@ -1,88 +0,0 @@ -/** - * 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 PropTypes from 'prop-types'; -import moment from 'moment'; -import { t } from '@superset-ui/core'; - -import Label from 'src/components/Label'; -import { Tooltip } from 'src/common/components/Tooltip'; - -const propTypes = { - onClick: PropTypes.func, - cachedTimestamp: PropTypes.string, - className: PropTypes.string, -}; - -class CacheLabel extends React.PureComponent { - constructor(props) { - super(props); - this.state = { - tooltipContent: '', - hovered: false, - }; - } - - updateTooltipContent() { - const cachedText = this.props.cachedTimestamp ? ( - - {t('Loaded data cached')} - {moment.utc(this.props.cachedTimestamp).fromNow()} - - ) : ( - t('Loaded from cache') - ); - - const tooltipContent = ( - - {cachedText}. {t('Click to force-refresh')} - - ); - this.setState({ tooltipContent }); - } - - mouseOver() { - this.updateTooltipContent(); - this.setState({ hovered: true }); - } - - mouseOut() { - this.setState({ hovered: false }); - } - - render() { - const labelType = this.state.hovered ? 'primary' : 'default'; - return ( - - - - ); - } -} -CacheLabel.propTypes = propTypes; - -export default CacheLabel; diff --git a/superset-frontend/src/components/CachedLabel/CachedLabel.test.tsx b/superset-frontend/src/components/CachedLabel/CachedLabel.test.tsx new file mode 100644 index 000000000..d9c7c5de7 --- /dev/null +++ b/superset-frontend/src/components/CachedLabel/CachedLabel.test.tsx @@ -0,0 +1,39 @@ +/** + * 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 moment from 'moment'; +import { TooltipContent } from './TooltipContent'; + +test('Rendering TooltipContent correctly - no timestamp', () => { + render(); + expect(screen.getByTestId('tooltip-content')?.textContent).toBe( + 'Loaded from cache. Click to force-refresh', + ); +}); + +test('Rendering TooltipContent correctly - with timestamp', () => { + render(); + expect(screen.getByTestId('tooltip-content')?.textContent).toBe( + `Loaded data cached ${moment + .utc('01-01-2000') + .fromNow()}. Click to force-refresh`, + ); +}); diff --git a/superset-frontend/src/components/CachedLabel/TooltipContent.test.tsx b/superset-frontend/src/components/CachedLabel/TooltipContent.test.tsx new file mode 100644 index 000000000..fa3b769a9 --- /dev/null +++ b/superset-frontend/src/components/CachedLabel/TooltipContent.test.tsx @@ -0,0 +1,39 @@ +/** + * 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 moment from 'moment'; +import { TooltipContent } from './TooltipContent'; + +test('Rendering TooltipContent correctly - no timestep', () => { + render(); + expect(screen.getByTestId('tooltip-content')?.textContent).toBe( + 'Loaded from cache. Click to force-refresh', + ); +}); + +test('Rendering TooltipContent correctly - with timestep', () => { + render(); + expect(screen.getByTestId('tooltip-content')?.textContent).toBe( + `Loaded data cached ${moment + .utc('01-01-2000') + .fromNow()}. Click to force-refresh`, + ); +}); diff --git a/superset-frontend/src/components/CachedLabel/TooltipContent.tsx b/superset-frontend/src/components/CachedLabel/TooltipContent.tsx new file mode 100644 index 000000000..f9029c2b6 --- /dev/null +++ b/superset-frontend/src/components/CachedLabel/TooltipContent.tsx @@ -0,0 +1,42 @@ +/** + * 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 moment from 'moment'; +import { t } from '@superset-ui/core'; + +interface Props { + cachedTimestamp?: string; +} +export const TooltipContent: React.FC = ({ cachedTimestamp }) => { + const cachedText = cachedTimestamp ? ( + + {t('Loaded data cached')} + {moment.utc(cachedTimestamp).fromNow()} + + ) : ( + t('Loaded from cache') + ); + + return ( + + {cachedText}. {t('Click to force-refresh')} + + ); +}; diff --git a/superset-frontend/src/components/CachedLabel/index.tsx b/superset-frontend/src/components/CachedLabel/index.tsx new file mode 100644 index 000000000..9b349a734 --- /dev/null +++ b/superset-frontend/src/components/CachedLabel/index.tsx @@ -0,0 +1,57 @@ +/** + * 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, { useState } from 'react'; +import { t } from '@superset-ui/core'; +import Label from 'src/components/Label'; +import { Tooltip } from 'src/common/components/Tooltip'; +import { TooltipContent } from './TooltipContent'; + +interface Props { + onClick?: React.MouseEventHandler; + cachedTimestamp?: string; + className?: string; +} + +const CacheLabel: React.FC = ({ + className, + onClick, + cachedTimestamp, +}) => { + const [hovered, setHovered] = useState(false); + + const labelType = hovered ? 'primary' : 'default'; + return ( + } + id="cache-desc-tooltip" + > + + + ); +}; + +export default CacheLabel; diff --git a/superset-frontend/src/components/Label/index.tsx b/superset-frontend/src/components/Label/index.tsx index 781e15f6a..d4f5407dc 100644 --- a/superset-frontend/src/components/Label/index.tsx +++ b/superset-frontend/src/components/Label/index.tsx @@ -31,7 +31,7 @@ export type Type = | 'primary' | 'secondary'; -export interface LabelProps { +export interface LabelProps extends React.HTMLAttributes { key?: string; className?: string; onClick?: OnClickHandler;