Skip to content
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

Merged
merged 11 commits into from
Aug 19, 2020
2 changes: 1 addition & 1 deletion superset-frontend/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/
module.exports = {
testRegex: '\\/spec\\/.*(_spec|\\.test)\\.(j|t)sx?$',
testRegex: '(\\/spec|\\/src)\\/.*(_spec|\\.test)\\.(j|t)sx?$',
moduleNameMapper: {
'\\.(css|less)$': '<rootDir>/spec/__mocks__/styleMock.js',
'\\.(gif|ttf|eot)$': '<rootDir>/spec/__mocks__/fileMock.js',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export default {
title: 'Label',
component: Label,
decorators: [withKnobs],
excludeStories: ['bsStyleKnob'],
};

const bsStyleKnob = {
export const bsStyleKnob = {
Copy link
Member

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

Copy link
Member Author

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.

label: 'Types',
options: {
default: 'default',
Expand Down Expand Up @@ -63,9 +64,10 @@ export const InteractiveLabel = () => (
bsStyleKnob.label,
bsStyleKnob.options,
bsStyleKnob.defaultValue,
bsStyleKnob.groupId,
)}
onClick={boolean('Has onClick action', false) ? action('clicked') : false}
onClick={
boolean('Has onClick action', false) ? action('clicked') : undefined
}
>
{text('Label', 'Label!')}
</Label>
Expand Down
58 changes: 58 additions & 0 deletions superset-frontend/src/components/Label/Label.test.tsx
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);
});
});
5 changes: 3 additions & 2 deletions superset-frontend/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ const config = {
},
{
test: /\.tsx?$/,
exclude: [/\.test.tsx?$/],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make this exclude .test.jsx too since there's a decent chance we might move files around before converting them to TS?

Copy link
Member Author

Choose a reason for hiding this comment

The 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,
Expand All @@ -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/,
Expand Down