superset-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maximebeauche...@apache.org
Subject [incubator-superset] branch master updated: [sqllab] fix data grid's instant search function (#4717)
Date Fri, 30 Mar 2018 17:22:13 GMT
This is an automated email from the ASF dual-hosted git repository.

maximebeauchemin 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 069d61c  [sqllab] fix data grid's instant search function (#4717)
069d61c is described below

commit 069d61c53f03608f05a0684572b9a2bb545fd9a6
Author: Maxime Beauchemin <maximebeauchemin@gmail.com>
AuthorDate: Fri Mar 30 10:22:10 2018 -0700

    [sqllab] fix data grid's instant search function (#4717)
    
    * [sqllab] fix data grid's instant search function
    
    It looks like any non-string type would break the search feature.
    of `FilterableTable`
    
    * Addressing comments
---
 .../components/FilterableTable/FilterableTable.jsx | 10 +++++--
 superset/assets/javascripts/modules/visUtils.js    |  9 ++++--
 .../FilterableTable/FilterableTable_spec.jsx       | 34 ++++++++++++++++++++--
 3 files changed, 45 insertions(+), 8 deletions(-)

diff --git a/superset/assets/javascripts/components/FilterableTable/FilterableTable.jsx b/superset/assets/javascripts/components/FilterableTable/FilterableTable.jsx
index 7874aab..e54a6fc 100644
--- a/superset/assets/javascripts/components/FilterableTable/FilterableTable.jsx
+++ b/superset/assets/javascripts/components/FilterableTable/FilterableTable.jsx
@@ -97,10 +97,16 @@ export default class FilterableTable extends PureComponent {
     const values = [];
     for (const key in row) {
       if (row.hasOwnProperty(key)) {
-        values.push(row[key].toLowerCase());
+        const cellValue = row[key];
+        if (typeof cellValue === 'string') {
+          values.push(cellValue.toLowerCase());
+        } else if (typeof cellValue.toString === 'function') {
+          values.push(cellValue.toString());
+        }
       }
     }
-    return values.some(v => v.includes(text.toLowerCase()));
+    const lowerCaseText = text.toLowerCase();
+    return values.some(v => v.includes(lowerCaseText));
   }
 
   headerRenderer({ dataKey, label, sortBy, sortDirection }) {
diff --git a/superset/assets/javascripts/modules/visUtils.js b/superset/assets/javascripts/modules/visUtils.js
index eef2bab..9744179 100644
--- a/superset/assets/javascripts/modules/visUtils.js
+++ b/superset/assets/javascripts/modules/visUtils.js
@@ -1,9 +1,12 @@
 export function getTextWidth(text, fontDetails = '12px Roboto') {
   const canvas = document.createElement('canvas');
   const context = canvas.getContext('2d');
-  context.font = fontDetails;
-  const metrics = context.measureText(text);
-  return metrics.width;
+  if (context) {
+    // Won't work outside of a browser context (ie unit tests)
+    context.font = fontDetails;
+    return context.measureText(text).width;
+  }
+  return 100;
 }
 
 export default {
diff --git a/superset/assets/spec/javascripts/components/FilterableTable/FilterableTable_spec.jsx
b/superset/assets/spec/javascripts/components/FilterableTable/FilterableTable_spec.jsx
index 240a8e4..a4ca914 100644
--- a/superset/assets/spec/javascripts/components/FilterableTable/FilterableTable_spec.jsx
+++ b/superset/assets/spec/javascripts/components/FilterableTable/FilterableTable_spec.jsx
@@ -1,15 +1,43 @@
 import React from 'react';
 import { describe, it } from 'mocha';
 import { expect } from 'chai';
+import { mount } from 'enzyme';
 import FilterableTable from '../../../../javascripts/components/FilterableTable/FilterableTable';
 
 describe('FilterableTable', () => {
   const mockedProps = {
-    orderedColumnKeys: [],
-    data: [],
-    height: 0,
+    orderedColumnKeys: ['a', 'b', 'c'],
+    data: [
+      { a: 'a1', b: 'b1', c: 'c1', d: 0 },
+      { a: 'a2', b: 'b2', c: 'c2', d: 100 },
+    ],
+    height: 500,
   };
+  let wrapper;
+  beforeEach(() => {
+    wrapper = mount(<FilterableTable {...mockedProps} />);
+  });
   it('is valid element', () => {
     expect(React.isValidElement(<FilterableTable {...mockedProps} />)).to.equal(true);
   });
+  it('renders a grid with 2 rows', () => {
+    expect(wrapper.find('.ReactVirtualized__Grid')).to.have.length(1);
+    expect(wrapper.find('.ReactVirtualized__Table__row')).to.have.length(2);
+  });
+  it('filters on a string', () => {
+    const props = {
+      ...mockedProps,
+      filterText: 'b1',
+    };
+    wrapper = mount(<FilterableTable {...props} />);
+    expect(wrapper.find('.ReactVirtualized__Table__row')).to.have.length(1);
+  });
+  it('filters on a number', () => {
+    const props = {
+      ...mockedProps,
+      filterText: '100',
+    };
+    wrapper = mount(<FilterableTable {...props} />);
+    expect(wrapper.find('.ReactVirtualized__Table__row')).to.have.length(1);
+  });
 });

-- 
To stop receiving notification emails like this one, please contact
maximebeauchemin@apache.org.

Mime
View raw message