You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2020/08/19 19:54:57 UTC

[incubator-superset] branch master updated: feat: Allow tests files in /src (plus Label component tests) (#10634)

This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new b0380be  feat: Allow tests files in  /src (plus Label component tests) (#10634)
b0380be is described below

commit b0380befa1ce4ba11df82ae93a11ffcf81d81762
Author: Evan Rusackas <ev...@preset.io>
AuthorDate: Wed Aug 19 12:54:26 2020 -0700

    feat: Allow tests files in  /src (plus Label component tests) (#10634)
    
    * 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
---
 superset-frontend/jest.config.js                   |  2 +-
 .../Label/{label.stories.jsx => Label.stories.tsx} |  8 +--
 .../src/components/Label/Label.test.tsx            | 58 ++++++++++++++++++++++
 superset-frontend/webpack.config.js                |  5 +-
 4 files changed, 67 insertions(+), 6 deletions(-)

diff --git a/superset-frontend/jest.config.js b/superset-frontend/jest.config.js
index 6a5931e..2d02d83 100644
--- a/superset-frontend/jest.config.js
+++ b/superset-frontend/jest.config.js
@@ -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',
diff --git a/superset-frontend/src/components/Label/label.stories.jsx b/superset-frontend/src/components/Label/Label.stories.tsx
similarity index 92%
rename from superset-frontend/src/components/Label/label.stories.jsx
rename to superset-frontend/src/components/Label/Label.stories.tsx
index be1153f..35ab1b9 100644
--- a/superset-frontend/src/components/Label/label.stories.jsx
+++ b/superset-frontend/src/components/Label/Label.stories.tsx
@@ -25,9 +25,10 @@ export default {
   title: 'Label',
   component: Label,
   decorators: [withKnobs],
+  excludeStories: ['bsStyleKnob'],
 };
 
-const bsStyleKnob = {
+export const bsStyleKnob = {
   label: 'Types',
   options: {
     default: 'default',
@@ -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>
diff --git a/superset-frontend/src/components/Label/Label.test.tsx b/superset-frontend/src/components/Label/Label.test.tsx
new file mode 100644
index 0000000..9227727
--- /dev/null
+++ b/superset-frontend/src/components/Label/Label.test.tsx
@@ -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);
+  });
+});
diff --git a/superset-frontend/webpack.config.js b/superset-frontend/webpack.config.js
index 412f345..66b04d0 100644
--- a/superset-frontend/webpack.config.js
+++ b/superset-frontend/webpack.config.js
@@ -230,6 +230,7 @@ const config = {
       },
       {
         test: /\.tsx?$/,
+        exclude: [/\.test.tsx?$/],
         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/,