-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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: Allow tests files in /src (plus Label component tests) #10634
Changes from all commits
e22348b
ffca92e
4662397
f129253
9a025fa
5f5a658
f711ace
92de2ce
df64398
edd6f91
d4f6aa7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
/* global jest */ | ||
import React from 'react'; | ||
/* eslint-disable-next-line import/no-extraneous-dependencies */ | ||
import { ReactWrapper } from 'enzyme'; | ||
import { styledMount as mount } from 'spec/helpers/theming'; | ||
import Label from '.'; | ||
import { LabelGallery, bsStyleKnob } from './Label.stories'; | ||
|
||
describe('Label', () => { | ||
let wrapper: ReactWrapper; | ||
|
||
// test the basic component | ||
it('renders the base component (no onClick)', () => { | ||
expect(React.isValidElement(<Label />)).toBe(true); | ||
}); | ||
|
||
it('works with an onClick handler', () => { | ||
const mockAction = jest.fn(); | ||
wrapper = mount(<Label onClick={mockAction} />); | ||
wrapper.find('.label').simulate('click'); | ||
expect(mockAction).toHaveBeenCalled(); | ||
}); | ||
|
||
// test stories from the storybook! | ||
it('renders all the sorybook gallery variants', () => { | ||
wrapper = mount(<LabelGallery />); | ||
Object.values(bsStyleKnob.options).forEach(opt => { | ||
expect(wrapper.find(`.label-${opt}`).at(0).text()).toEqual( | ||
`style: "${opt}"`, | ||
); | ||
}); | ||
}); | ||
|
||
// test things NOT in the storybook! | ||
it('renders custom label styles without melting', () => { | ||
wrapper = mount(<Label bsStyle="foobar" />); | ||
expect(wrapper.find('Label.label-foobar')).toHaveLength(1); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,6 +230,7 @@ const config = { | |
}, | ||
{ | ||
test: /\.tsx?$/, | ||
exclude: [/\.test.tsx?$/], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe make this exclude There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Those are also excluded lower down, in the jsx (babel) loader. |
||
use: [ | ||
'thread-loader', | ||
babelLoader, | ||
|
@@ -255,8 +256,8 @@ const config = { | |
}, | ||
{ | ||
test: /\.jsx?$/, | ||
// include source code for plugins, but exclude node_modules within them | ||
exclude: [/superset-ui.*\/node_modules\//], | ||
// include source code for plugins, but exclude node_modules and test files within them | ||
exclude: [/superset-ui.*\/node_modules\//, /\.test.jsx?$/], | ||
include: [ | ||
new RegExp(`${APP_DIR}/src`), | ||
/superset-ui.*\/src/, | ||
|
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.
maybe we should move this to a fixtures file that the stories and the test can both import from? It's a bit weird to import from a stories file in the test file, and it would also mean you don't need to tell storybook that this isn't a story too
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.
In this case (exporting the knobs), I agree it's a little weird, but I really like importing the actual stories in tests, so I figured this could come along for the ride. Mounting the story exports helps make sure your tests cover the use cases of the component properly, and means that storybook edits (i.e. when adding new features/variants to a component) might cause test failures, which I think is a good thing.
All that being said, I'll try to find a good way around the weird knob export/exclusion (probably in another PR). It is a little screwy.