Skip to content

Commit

Permalink
Edit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
texodus committed Sep 9, 2019
1 parent c9c3b02 commit 3b3d5c7
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ module.exports = require("datasaur-local").extend("PerspectiveDataModel", {
editor._row = this._view.to_json(args);
editor.el.addEventListener("blur", () => setTimeout(() => editor.cancelEditing()));
editor._table = this._table;
editor._data = this.data;
return editor;
},

Expand Down
32 changes: 25 additions & 7 deletions packages/perspective-viewer-hypergrid/src/js/editors.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ function getEditorValueText(updated) {
return this.localizer.format(updated);
}

function getEditorValueNumber(updated) {
this._row.then(([old]) => {
const index = old.__INDEX__;
delete old["__INDEX__"];
const colname = Object.keys(old)[0];
this._table.update([{__INDEX__: index, [colname]: Number(updated.replace(/,/g, ""))}]);
});
return this.localizer.format(updated);
}

function getEditorValueDate(updated) {
updated = new Date(updated);
this._row.then(([old]) => {
Expand All @@ -87,6 +97,13 @@ function getEditorValueDate(updated) {
return this.localizer.format(updated);
}

function saveEditorValue(x) {
var save = !(x && x === this.initialValue) && this.grid.fireBeforeCellEdit(this.event.gridCell, this.initialValue, x, this);
if (save) {
this._data[this.event.gridCell.y - 1][this.event.gridCell.x] = x;
}
}

export function set_editors(grid) {
const date = Textfield.extend("perspective-date", {
localizer: grid.localization.get("chromeDate"),
Expand All @@ -95,7 +112,8 @@ export function set_editors(grid) {
setEditorValue: setEditorValueDate,
validateEditorValue: validateEditorValueDate,
setBounds: setBoundsDate,
selectAll: () => {}
selectAll: () => {},
saveEditorValue
});

const datetime = Textfield.extend("perspective-datetime", {
Expand All @@ -105,23 +123,23 @@ export function set_editors(grid) {
setEditorValue: setEditorValueDatetime,
validateEditorValue: validateEditorValueDate,
setBounds: setBoundsDate,
selectAll: () => {}
selectAll: () => {},
saveEditorValue
});

const text = Textfield.extend("perspective-text", {
setBounds: setBoundsText,
setEditorValue: setEditorValueText,
getEditorValue: getEditorValueText,
validateEditorValue: function(str) {
return this.localizer.invalid && this.localizer.invalid(str || this.input.value);
}
saveEditorValue
});

const number = Textfield.extend("perspective-number", {
setBounds: setBoundsText,
setEditorValue: setEditorValueText,
getEditorValue: getEditorValueText,
validateEditorValue: x => isNaN(Number(x))
getEditorValue: getEditorValueNumber,
validateEditorValue: x => isNaN(Number(x.replace(/,/g, ""))),
saveEditorValue
});

grid.cellEditors.add(number);
Expand Down
38 changes: 38 additions & 0 deletions packages/perspective-viewer-hypergrid/test/html/editable.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<!--
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.
-->

<!DOCTYPE html>
<html>
<head>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
<script src="perspective-viewer.js"></script>
<script src="perspective-viewer-hypergrid.js"></script>

<link rel='stylesheet' href="demo.css">

</head>
<body>

<perspective-viewer columns='["Row ID","Order ID","Order Date","Sales"]' editable>

</perspective-viewer>

<script>

var xhr = new XMLHttpRequest();
xhr.open('GET', 'superstore.csv', true);
xhr.onload = function () {
document.getElementsByTagName('perspective-viewer')[0].load(xhr.response);
}
xhr.send(null);

</script>

</body>
</html>
114 changes: 114 additions & 0 deletions packages/perspective-viewer-hypergrid/test/js/editing.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/******************************************************************************
*
* 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 utils = require("@finos/perspective-test");
const path = require("path");

const {dblclick} = require("./utils.js");

utils.with_server({}, () => {
describe.page(
"superstore.html",
() => {
describe("editing UI opens", () => {
test.capture("should not edit an immutable viewer", async page => {
await page.$("perspective-viewer");
await page.shadow_click("perspective-viewer", "#config_button");
await page.waitForSelector("perspective-viewer[settings]");
await dblclick(page);
await page.waitForSelector("perspective-viewer:not([updating])");
});
});
},
{reload_page: true, root: path.join(__dirname, "..", "..")}
);

describe.page(
"editable.html",
() => {
describe("editing UI opens", () => {
test.capture("should open an editor on a string column", async page => {
await page.$("perspective-viewer");
await page.shadow_click("perspective-viewer", "#config_button");
await page.waitForSelector("perspective-viewer[settings]");
await dblclick(page);
await page.waitForSelector("perspective-viewer:not([updating])");
});

test.capture("should open an editor on an integer column", async page => {
await page.$("perspective-viewer");
await page.shadow_click("perspective-viewer", "#config_button");
await page.waitForSelector("perspective-viewer[settings]");
await dblclick(page, 50);
await page.waitForSelector("perspective-viewer:not([updating])");
});

test.capture("should open an editor on a datetime column", async page => {
await page.$("perspective-viewer");
await page.shadow_click("perspective-viewer", "#config_button");
await page.waitForSelector("perspective-viewer[settings]");
await dblclick(page, 400);
await page.waitForSelector("perspective-viewer:not([updating])");
});
});

describe("editing UI saves", () => {
test.capture("should save edits to a string column", async page => {
await page.$("perspective-viewer");
await page.shadow_click("perspective-viewer", "#config_button");
await page.waitForSelector("perspective-viewer[settings]");
await dblclick(page);
await page.keyboard.sendCharacter("a");
await page.keyboard.sendCharacter("b");
await page.keyboard.sendCharacter("c");
await page.keyboard.sendCharacter("d");
await page.keyboard.press("Enter");
await page.waitForSelector("perspective-viewer:not([updating])");
});

test.capture("should save edits to an integer column", async page => {
await page.$("perspective-viewer");
await page.shadow_click("perspective-viewer", "#config_button");
await page.waitForSelector("perspective-viewer[settings]");
await dblclick(page, 50);
await page.keyboard.sendCharacter("1");
await page.keyboard.sendCharacter("2");
await page.keyboard.press("Enter");
await page.waitForSelector("perspective-viewer:not([updating])");
});

test.capture("should save edits of negative numbers to an integer column", async page => {
await page.$("perspective-viewer");
await page.shadow_click("perspective-viewer", "#config_button");
await page.waitForSelector("perspective-viewer[settings]");
await dblclick(page, 50);
await page.keyboard.sendCharacter("-");
await page.keyboard.sendCharacter("1");
await page.keyboard.sendCharacter("2");
await page.keyboard.press("Enter");
await page.waitForSelector("perspective-viewer:not([updating])");
});

test.capture("should fail to save edits of invalid integers", async page => {
await page.$("perspective-viewer");
await page.shadow_click("perspective-viewer", "#config_button");
await page.waitForSelector("perspective-viewer[settings]");
await dblclick(page, 50);
await page.keyboard.sendCharacter("1");
await page.keyboard.sendCharacter("2");
await page.keyboard.sendCharacter("A");
await page.keyboard.press("Enter");
await page.waitFor(100);
await page.waitForSelector("perspective-viewer:not([updating])");
});
});
},
{reload_page: true, root: path.join(__dirname, "..", "..")}
);
});
16 changes: 16 additions & 0 deletions packages/perspective-viewer-hypergrid/test/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
*
*/

exports.dblclick = async (page, x = 310, y = 300) => {
await page.evaluate(
({x, y}) => {
const example = document
.querySelector("perspective-viewer")
.shadowRoot.querySelector("perspective-hypergrid")
.shadowRoot.querySelector("canvas");

const event = document.createEvent("MouseEvents");
event.initMouseEvent("dblclick", true, false, window, 0, x, y, x, y, false, false, false, false, 2, null);
example.dispatchEvent(event);
},
{x, y}
);
};

exports.click_details = async (page, x = 310, y = 300) => {
const viewer = await page.$("perspective-viewer");

Expand Down

0 comments on commit 3b3d5c7

Please sign in to comment.