Skip to content

Commit

Permalink
Fix autocomplete error when filtering string column w/null values
Browse files Browse the repository at this point in the history
* Factor out a function for filtering out null values before feeding
them into the autocomplete choices
* Add unit testing config to perspective-viewer package
* Add a unit test to cover this bugfix
  • Loading branch information
jspillers committed Jul 23, 2019
1 parent 8360a64 commit 10eeba5
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -116,6 +116,7 @@ website/i18n/*
.idea
cmake-build-debug
.ipynb_checkpoints
.python-version

package.json.lerna_backup
website/static/css/material.dark.css
Expand Down
2 changes: 1 addition & 1 deletion packages/perspective-viewer-d3fc/package.json
Expand Up @@ -30,7 +30,7 @@
"build:webpack:cjs": "webpack --color --config src/config/cjs.config.js",
"build:webpack:umd": "webpack --color --config src/config/umd.config.js",
"build:webpack": "npm-run-all build:webpack:cjs build:webpack:umd",
"build": "npm-run-all --silent build:babel build:webpack",
"build": "npm-run-all --silent build:babel build:webpack:cjs build:webpack:umd",
"test:build": "cpx \"test/html/*\" dist/umd",
"watch": "webpack --color --watch --config src/config/d3fc.watch.config.js",
"test:run": "jest --silent --color 2>&1",
Expand Down
12 changes: 10 additions & 2 deletions packages/perspective-viewer/package.json
Expand Up @@ -22,7 +22,8 @@
"build": "npm-run-all --silent build:babel build:webpack:cjs build:webpack:umd",
"watch": "webpack --color --watch --config src/config/view.config.js",
"test:build": "cpx \"test/html/*\" dist/umd && cpx \"test/csv/*\" dist/umd && cpx \"test/css/*\" dist/umd",
"test:run": "jest --silent --color",
"test:run": "jest --silent --color 2>&1",
"test:unit": "jest --config=test/js/jest.unit.config.js --color --watch",
"test": "npm-run-all test:build test:run",
"clean": "rimraf dist",
"clean:screenshots": "rimraf \"screenshots/**/*.@(failed|diff).png\"",
Expand All @@ -40,7 +41,14 @@
"verbose": true,
"testURL": "http://localhost/",
"transform": {},
"automock": false
"automock": false,
"transform": {
".js$": "./test/js/transform.js",
".html$": "html-loader-jest"
},
"setupFiles": [
"./test/js/beforeEachSpec.js"
]
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/perspective-viewer/src/js/computed_column.js
Expand Up @@ -138,8 +138,8 @@ class ComputedColumn extends HTMLElement {
this.state.swap_target = false;

for (let i = 0; i < computation.num_params; i++) {
this._input_columns.innerHTML += `<div class="psp-cc-computation__input-column"
data-index="${i}"
this._input_columns.innerHTML += `<div class="psp-cc-computation__input-column"
data-index="${i}"
drop-target>
<span class="psp-label__requiredType ${input_type}"></span>
<span class="psp-label__placeholder">Param ${i + 1}</span>
Expand Down
23 changes: 13 additions & 10 deletions packages/perspective-viewer/src/js/viewer/dom_element.js
Expand Up @@ -65,17 +65,13 @@ export class DomElement extends PerspectiveElement {

if (filter) {
row.setAttribute("filter", filter);

if (type === "string") {
const v = this._table.view({row_pivots: [name], aggregates: {}});
v.to_json().then(json => {
row.choices(
json
.slice(1, json.length)
.map(x => x.__ROW_PATH__)
.filter(x => (Array.isArray(x) ? x.filter(v => !!v).length > 0 : !!x))
);
});
v.delete();
const view = this._table.view({row_pivots: [name], aggregates: {}});
view.to_json().then(json => {
row.choices(this._autocomplete_choices(json)) }
);
view.delete();
}
}

Expand Down Expand Up @@ -299,4 +295,11 @@ export class DomElement extends PerspectiveElement {
this.load(data);
}
}

_autocomplete_choices(json) {
return json
.slice(1, json.length)
.map(x => x.__ROW_PATH__)
.filter(x => (Array.isArray(x) ? x.filter(v => !!v).length > 0 : !!x));
}
}
17 changes: 17 additions & 0 deletions packages/perspective-viewer/test/js/beforeEachSpec.js
@@ -0,0 +1,17 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

global.CustomEvent = () => {};
global.customElements = {define: () => {}};
global.HTMLElement = class {
getAttribute() {}
hasAttribute() {}
removeAttribute() {}
setAttribute() {}
};
20 changes: 20 additions & 0 deletions packages/perspective-viewer/test/js/jest.unit.config.js
@@ -0,0 +1,20 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

module.exports = {
roots: ["unit"],
verbose: true,
testURL: "http://localhost/",
automock: false,
transform: {
".js$": "./transform.js",
".html$": "html-loader-jest"
},
setupFiles: ["./beforeEachSpec.js"]
};
13 changes: 13 additions & 0 deletions packages/perspective-viewer/test/js/transform.js
@@ -0,0 +1,13 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

const config = require("../../babel.config.js");
config.presets[0][1].modules = "auto";

module.exports = require("babel-jest").createTransformer(config);
35 changes: 35 additions & 0 deletions packages/perspective-viewer/test/js/unit/dom_element.spec.js
@@ -0,0 +1,35 @@
/******************************************************************************
*
* Copyright (c) 2018, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

import {DomElement} from "../../../src/js/viewer/dom_element";

jest.mock("../../../src/less/computed_column.less", () => jest.fn());

describe(DomElement, () => {
describe("._autocomplete_choices", () => {
let dom_element, json_choices;

beforeEach(() => {
dom_element = new DomElement();
json_choices = [
{__ROW_PATH__: [], foo: 2},
{__ROW_PATH__: [null], foo: 25},
{__ROW_PATH__: ["somestring"], foo: 3},
{__ROW_PATH__: ["otherstring"], foo: 3}
];
});

test("the first value and null values are filtered out", () => {
expect(dom_element._autocomplete_choices(json_choices)).toEqual([
["somestring"],
["otherstring"]
]);
});
});
});

0 comments on commit 10eeba5

Please sign in to comment.