-
Notifications
You must be signed in to change notification settings - Fork 13.6k
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
test: Adds tests to the AnnotationLayer component #13748
Merged
rusackas
merged 1 commit into
apache:master
from
michael-s-molina:tests-audit-annotation-layer-control
Apr 1, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
24 changes: 0 additions & 24 deletions
24
superset-frontend/src/explore/components/controls/AnnotationLayer.less
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
190 changes: 190 additions & 0 deletions
190
...-frontend/src/explore/components/controls/AnnotationLayerControl/AnnotationLayer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
/** | ||
* 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, waitFor } from 'spec/helpers/testing-library'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { getChartMetadataRegistry, ChartMetadata } from '@superset-ui/core'; | ||
import fetchMock from 'fetch-mock'; | ||
import setupColors from 'src/setup/setupColors'; | ||
import { ANNOTATION_TYPES_METADATA } from 'src/modules/AnnotationTypes'; | ||
import AnnotationLayer from './AnnotationLayer'; | ||
|
||
const defaultProps = { | ||
value: '', | ||
vizType: 'table', | ||
annotationType: ANNOTATION_TYPES_METADATA.FORMULA.value, | ||
}; | ||
|
||
beforeAll(() => { | ||
const supportedAnnotationTypes = Object.values(ANNOTATION_TYPES_METADATA).map( | ||
value => value.value, | ||
); | ||
|
||
fetchMock.get('glob:*/annotationlayermodelview/api/read?*', { | ||
result: [{ label: 'Chart A', value: 'a' }], | ||
}); | ||
|
||
fetchMock.get('glob:*/superset/user_slices*', [ | ||
{ id: 'a', title: 'Chart A', viz_type: 'table', data: {} }, | ||
]); | ||
|
||
setupColors(); | ||
|
||
getChartMetadataRegistry().registerValue( | ||
'table', | ||
new ChartMetadata({ | ||
name: 'Table', | ||
thumbnail: '', | ||
supportedAnnotationTypes, | ||
canBeAnnotationTypes: ['EVENT'], | ||
}), | ||
); | ||
}); | ||
|
||
test('renders with default props', () => { | ||
const { container } = render(<AnnotationLayer {...defaultProps} />); | ||
expect(container).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: 'Apply' })).toBeDisabled(); | ||
expect(screen.getByRole('button', { name: 'OK' })).toBeDisabled(); | ||
expect(screen.getByRole('button', { name: 'Cancel' })).toBeEnabled(); | ||
}); | ||
|
||
test('renders extra checkboxes when type is time series', () => { | ||
render(<AnnotationLayer {...defaultProps} />); | ||
expect( | ||
screen.queryByRole('button', { name: 'Show Markers' }), | ||
).not.toBeInTheDocument(); | ||
expect( | ||
screen.queryByRole('button', { name: 'Hide Line' }), | ||
).not.toBeInTheDocument(); | ||
userEvent.click(screen.getAllByText('Formula')[0]); | ||
userEvent.click(screen.getByText('Time series')); | ||
expect( | ||
screen.getByRole('button', { name: 'Show Markers' }), | ||
).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: 'Hide Line' })).toBeInTheDocument(); | ||
}); | ||
|
||
test('enables apply and ok buttons', async () => { | ||
render(<AnnotationLayer {...defaultProps} />); | ||
userEvent.type(screen.getByLabelText('Name'), 'Test'); | ||
userEvent.type(screen.getByLabelText('Formula'), '2x'); | ||
await waitFor(() => { | ||
expect(screen.getByRole('button', { name: 'Apply' })).toBeEnabled(); | ||
expect(screen.getByRole('button', { name: 'OK' })).toBeEnabled(); | ||
}); | ||
}); | ||
|
||
test('triggers addAnnotationLayer when apply button is clicked', () => { | ||
const addAnnotationLayer = jest.fn(); | ||
render( | ||
<AnnotationLayer | ||
{...defaultProps} | ||
name="Test" | ||
value="2x" | ||
addAnnotationLayer={addAnnotationLayer} | ||
/>, | ||
); | ||
userEvent.click(screen.getByRole('button', { name: 'Apply' })); | ||
expect(addAnnotationLayer).toHaveBeenCalled(); | ||
}); | ||
|
||
test('triggers addAnnotationLayer and close when ok button is clicked', () => { | ||
const addAnnotationLayer = jest.fn(); | ||
const close = jest.fn(); | ||
render( | ||
<AnnotationLayer | ||
{...defaultProps} | ||
name="Test" | ||
value="2x" | ||
addAnnotationLayer={addAnnotationLayer} | ||
close={close} | ||
/>, | ||
); | ||
userEvent.click(screen.getByRole('button', { name: 'OK' })); | ||
expect(addAnnotationLayer).toHaveBeenCalled(); | ||
expect(close).toHaveBeenCalled(); | ||
}); | ||
|
||
test('triggers close when cancel button is clicked', () => { | ||
const close = jest.fn(); | ||
render(<AnnotationLayer {...defaultProps} close={close} />); | ||
userEvent.click(screen.getByRole('button', { name: 'Cancel' })); | ||
expect(close).toHaveBeenCalled(); | ||
}); | ||
|
||
test('triggers removeAnnotationLayer and close when remove button is clicked', () => { | ||
const removeAnnotationLayer = jest.fn(); | ||
const close = jest.fn(); | ||
render( | ||
<AnnotationLayer | ||
{...defaultProps} | ||
name="Test" | ||
value="2x" | ||
removeAnnotationLayer={removeAnnotationLayer} | ||
close={close} | ||
/>, | ||
); | ||
userEvent.click(screen.getByRole('button', { name: 'Remove' })); | ||
expect(removeAnnotationLayer).toHaveBeenCalled(); | ||
expect(close).toHaveBeenCalled(); | ||
}); | ||
|
||
test('renders chart options', async () => { | ||
render( | ||
<AnnotationLayer | ||
{...defaultProps} | ||
annotationType={ANNOTATION_TYPES_METADATA.EVENT.value} | ||
/>, | ||
); | ||
userEvent.click(screen.getByText('2 option(s)')); | ||
userEvent.click(screen.getByText('Superset annotation')); | ||
expect(await screen.findByLabelText('Annotation layer')).toBeInTheDocument(); | ||
userEvent.click(screen.getByText('Superset annotation')); | ||
userEvent.click(screen.getByText('Table')); | ||
expect(await screen.findByLabelText('Chart')).toBeInTheDocument(); | ||
}); | ||
|
||
test('keeps apply disabled when missing required fields', async () => { | ||
render( | ||
<AnnotationLayer | ||
{...defaultProps} | ||
annotationType={ANNOTATION_TYPES_METADATA.EVENT.value} | ||
sourceType="Table" | ||
/>, | ||
); | ||
userEvent.click(await screen.findByText('1 option(s)')); | ||
userEvent.click(screen.getByText('Chart A')); | ||
expect( | ||
screen.getByText('Annotation Slice Configuration'), | ||
).toBeInTheDocument(); | ||
|
||
userEvent.click(screen.getByRole('button', { name: 'Automatic Color' })); | ||
userEvent.click(screen.getByLabelText('Title Column')); | ||
userEvent.click(screen.getByText('None')); | ||
userEvent.click(screen.getByLabelText('Style')); | ||
userEvent.click(screen.getByText('Dashed')); | ||
userEvent.click(screen.getByLabelText('Opacity')); | ||
userEvent.click(screen.getByText('0.5')); | ||
|
||
const checkboxes = screen.getAllByRole('checkbox'); | ||
checkboxes.forEach(checkbox => userEvent.click(checkbox)); | ||
|
||
expect(screen.getByRole('button', { name: 'Apply' })).toBeDisabled(); | ||
}); |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer relative imports if they're in the same directory, but I don't have strong opinions on this. But it would be nice if we could enforce this with with some linting rules, e.g. requiring relative imports if in the same or parent directory, and absolute if elsewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@villebro I agree with you. I'm using relative imports if they're in the same directory and full paths otherwise. Since in this case, they are not in the same directory anymore, I used full paths. Totally agree with the Lint rules.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My apologies, I didn't notice that this file moved 🤦 My "internal" linter is ok with relative imports from parent directories, but absolute is also just fine! But it would be great if the linter enforced fully deterministic imports (will try to poke around to see if this is possible/makes sense)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem at all! 😄
This would be awesome!
@villebro @rusackas I think that after SIP-61 we'll have a more predictable structure, and that can allow us to use Webpack module aliases for our main folders and help with full imports.