Skip to content

Commit

Permalink
COMPASS-468 schema shows undefined (#689)
Browse files Browse the repository at this point in the history
* sort types on mount rather than during render phase

* pushed sorting field types up to index level

* COMPASS-469 show minicharts for unique long type

* attempting enzyme test

* adding tooltip to d3/shared

* getting tests for minicharts working.

* added a couple of tests

* added tests

* COMPASS-468 pushed sorting back to fields from index

* fixed error and improved test cases
  • Loading branch information
Satya authored and rueckstiess committed Dec 21, 2016
1 parent e8cfb38 commit 57ee4a0
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/internal-packages/schema/lib/component/field.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,18 @@ const Field = React.createClass({
},

componentWillMount() {
// sort the types in descending order and push undefined to the end
const types = _.sortBy(this.props.types, (type) => {
if (type.name === 'Undefined') {
return -Infinity;
}
return type.probability;
}).reverse();

// sets the active type to the first type in the props.types array
this.setState({
activeType: this.props.types.length > 0 ? this.props.types[0] : null
types: types,
activeType: types.length > 0 ? types[0] : null
});
},

Expand Down Expand Up @@ -139,13 +148,7 @@ const Field = React.createClass({
const cls = FIELD_CLASS + ' ' + (this.state.collapsed ? 'collapsed' : 'expanded');

// types represented as horizontal bars with labels
const types = _.sortBy(this.props.types, (type) => {
if (type.name === 'Undefined') {
return -Infinity;
}
return type.probability;
}).reverse();
const typeList = types.map((type) => {
const typeList = this.state.types.map((type) => {
// allow for semantic types and convert the type, e.g. geo coordinates
type = this.getSemanticType(type);
return (
Expand Down
141 changes: 141 additions & 0 deletions test/schema.field.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/* eslint no-unused-expressions: 0 */
/* eslint no-unused-vars: 0 */
const app = require('ampersand-app');
const chai = require('chai');
const chaiEnzyme = require('chai-enzyme');
const expect = chai.expect;
const React = require('react');
const AppRegistry = require('hadron-app-registry');

const mount = require('enzyme').mount;

chai.use(chaiEnzyme());

describe('<Schema />', () => {
let component;

const fieldProp = {
count: 2,
has_duplicates: true,
name: 'foo',
path: 'foo',
probability: 0.4
};

const typesWithUndefined = [
{
count: 2,
has_duplicates: true,
name: 'String',
path: 'foo',
probability: 0.4,
total_count: 0,
unique: 1,
values: ['bar', 'bar']
},
{
count: 3,
has_duplicates: true,
name: 'Undefined',
path: 'foo',
probability: 0.6,
total_count: 0,
type: 'Undefined'
}
];

const typesWithMultiple = [
{
count: 2,
has_duplicates: true,
name: 'Long',
path: 'foo',
probability: 0.2,
total_count: 0,
unique: 1,
values: [4, 2]
},
{
count: 3,
has_duplicates: true,
name: 'String',
path: 'foo',
probability: 0.3,
total_count: 0,
unique: 1,
values: ['bar', 'bar', 'foo']
},
{
count: 5,
has_duplicates: true,
name: 'Undefined',
path: 'foo',
probability: 0.5,
total_count: 0,
type: 'Undefined'
}
];

let appRegistry = app.appRegistry;
before(function() {
// Mock the AppRegistry with a new one so tests don't complain about
// appRegistry.getComponent (i.e. appRegistry being undefined)
app.appRegistry = new AppRegistry();
// register QueryStore
require('../src/internal-packages/query').activate();
});
after(function() {
// unregister QueryStore
require('../src/internal-packages/query').deactivate();
// Restore properties on the global app object,
// so they don't affect other tests
app.appRegistry = appRegistry;
});

context('when adding fields to the schema view', () => {
it('renders field types', () => {
const Field = require('../src/internal-packages/schema/lib/component/field');
const Type = require('../src/internal-packages/schema/lib/component/type');

fieldProp.types = typesWithUndefined;
component = mount(<Field {...fieldProp} />);
expect(component.find(Type)).to.have.length(2);
});

it('renders the first type as string', () => {
const Field = require('../src/internal-packages/schema/lib/component/field');
const Type = require('../src/internal-packages/schema/lib/component/type');

fieldProp.types = typesWithUndefined;
component = mount(<Field {...fieldProp} />);
expect(component.find(Type).at(0)).to.have.data('tip', 'String (40%)');
expect(component.find('.schema-field-type-string')).to.have.className('active');
});

it('renders the second type as undefined', () => {
const Field = require('../src/internal-packages/schema/lib/component/field');
const Type = require('../src/internal-packages/schema/lib/component/type');

fieldProp.types = typesWithUndefined;
component = mount(<Field {...fieldProp} />);
expect(component.find(Type).at(1)).to.have.data('tip', 'Undefined (60%)');
expect(component.find('.schema-field-type-undefined')).to.not.have.className('active');
});

context('when rendering multiple fields', () => {
it('renders type with highest probability first', () => {
const Field = require('../src/internal-packages/schema/lib/component/field');
const Type = require('../src/internal-packages/schema/lib/component/type');

fieldProp.types = typesWithMultiple;
component = mount(<Field {...fieldProp} />);
expect(component.find(Type).at(0)).to.have.data('tip', 'String (30%)');
expect(component.find('.schema-field-type-string')).to.have.className('active');
expect(component.find(Type).at(1)).to.have.data('tip', 'Long (20%)');
expect(component.find('.schema-field-type-long')).to.not.have.className('active');
expect(component.find(Type).at(2)).to.have.data('tip', 'Undefined (50%)');
expect(component.find('.schema-field-type-undefined')).to.not.have.className('active');
});
});
});
});

0 comments on commit 57ee4a0

Please sign in to comment.