Skip to content

Commit

Permalink
feat: Allow tests files in /src (plus Label component tests) (#10634)
Browse files Browse the repository at this point in the history
* allow tests in jest confg

* sample stories for Label component

* passing tests

* stories to tsx!

* excluding knobs exports from published stories

* ts fix

* ts fix

* Label test to TS

* explicitly ignoring test files in webpack bundling

* linting stuff

* adding comment about test file exclusions
  • Loading branch information
rusackas authored Aug 19, 2020
1 parent 3f5d5cc commit b0380be
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
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 = {
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?$/],
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

0 comments on commit b0380be

Please # to comment.