|
| 1 | +/****************************************************************************** |
| 2 | + * |
| 3 | + * Copyright (c) 2017, the Perspective Authors. |
| 4 | + * |
| 5 | + * This file is part of the Perspective library, distributed under the terms of |
| 6 | + * the Apache License 2.0. The full license can be found in the LICENSE file. |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +const utils = require("@finos/perspective-test"); |
| 11 | +const path = require("path"); |
| 12 | + |
| 13 | +const simple_tests = require("@finos/perspective-viewer/test/js/simple_tests.js"); |
| 14 | + |
| 15 | +async function get_contents( |
| 16 | + page, |
| 17 | + selector = "perspective-viewer perspective-viewer-datagrid regular-table", |
| 18 | + shadow = false |
| 19 | +) { |
| 20 | + return await page.evaluate( |
| 21 | + async (selector, shadow) => { |
| 22 | + const viewer = document.querySelector(selector); |
| 23 | + return (shadow ? viewer.shadowRoot : viewer).innerHTML || "MISSING"; |
| 24 | + }, |
| 25 | + selector, |
| 26 | + shadow |
| 27 | + ); |
| 28 | +} |
| 29 | + |
| 30 | +utils.with_server({}, () => { |
| 31 | + describe.page( |
| 32 | + "superstore.html", |
| 33 | + () => { |
| 34 | + test.capture( |
| 35 | + "perspective-config-update event is fired when column style is changed", |
| 36 | + async (page) => { |
| 37 | + // Await the viewer element to exist on the page |
| 38 | + const viewer = await page.waitForSelector( |
| 39 | + "perspective-viewer" |
| 40 | + ); |
| 41 | + const {x, y} = await page.evaluate(async (viewer) => { |
| 42 | + // Await the table load |
| 43 | + await viewer.getTable(); |
| 44 | + |
| 45 | + // Open the config panel |
| 46 | + await viewer.toggleConfig(); |
| 47 | + |
| 48 | + // Register a listener for `perspective-config-update` event |
| 49 | + window.__events__ = []; |
| 50 | + viewer.addEventListener( |
| 51 | + "perspective-config-update", |
| 52 | + (evt) => { |
| 53 | + window.__events__.push(evt); |
| 54 | + } |
| 55 | + ); |
| 56 | + |
| 57 | + // Find the column config menu button |
| 58 | + const header_button = viewer.querySelector( |
| 59 | + "regular-table thead tr:last-child th" |
| 60 | + ); |
| 61 | + |
| 62 | + // Get the button coords (slightly lower than center |
| 63 | + // because of the location of the menu button within |
| 64 | + // this element) |
| 65 | + const rect = header_button.getBoundingClientRect(); |
| 66 | + return { |
| 67 | + x: Math.floor(rect.left + rect.width / 2), |
| 68 | + y: Math.floor(rect.top + (3 * rect.height) / 4), |
| 69 | + }; |
| 70 | + }, viewer); |
| 71 | + |
| 72 | + // Click the menu button |
| 73 | + await page.mouse.click(x, y); |
| 74 | + |
| 75 | + // Await the style menu existing on the page |
| 76 | + const style_menu = await page.waitForSelector( |
| 77 | + "perspective-number-column-style" |
| 78 | + ); |
| 79 | + |
| 80 | + const {x: xx, y: yy} = await page.evaluate( |
| 81 | + async (style_menu) => { |
| 82 | + // Find the 'bar' button |
| 83 | + const bar_button = |
| 84 | + style_menu.shadowRoot.querySelector( |
| 85 | + '#radio-list-1[name="foreground-list"]' |
| 86 | + ); |
| 87 | + |
| 88 | + // Get its coords |
| 89 | + const rect = bar_button.getBoundingClientRect(); |
| 90 | + return { |
| 91 | + x: Math.floor(rect.left + rect.width / 2), |
| 92 | + y: Math.floor(rect.top + rect.height / 2), |
| 93 | + }; |
| 94 | + }, |
| 95 | + style_menu |
| 96 | + ); |
| 97 | + |
| 98 | + // Click the button |
| 99 | + await page.mouse.click(xx, yy); |
| 100 | + |
| 101 | + const count = await page.evaluate(async (viewer) => { |
| 102 | + // Await the plugin rendering |
| 103 | + await viewer.flush(); |
| 104 | + |
| 105 | + // Count the events; |
| 106 | + return window.__events__.length; |
| 107 | + }, viewer); |
| 108 | + |
| 109 | + // Expect 1 event |
| 110 | + expect(count).toEqual(1); |
| 111 | + |
| 112 | + // Return the `<table>` contents |
| 113 | + return get_contents(page); |
| 114 | + } |
| 115 | + ); |
| 116 | + |
| 117 | + describe("Column style menu opens for", () => { |
| 118 | + async function test_column(page, selector, selector2) { |
| 119 | + const viewer = await page.waitForSelector( |
| 120 | + "perspective-viewer" |
| 121 | + ); |
| 122 | + const {x, y} = await page.evaluate( |
| 123 | + async (viewer, selector) => { |
| 124 | + await viewer.getTable(); |
| 125 | + await viewer.toggleConfig(); |
| 126 | + window.__events__ = []; |
| 127 | + viewer.addEventListener( |
| 128 | + "perspective-config-update", |
| 129 | + (evt) => { |
| 130 | + window.__events__.push(evt); |
| 131 | + } |
| 132 | + ); |
| 133 | + |
| 134 | + const header_button = viewer.querySelector( |
| 135 | + "regular-table thead tr:last-child th" + |
| 136 | + selector |
| 137 | + ); |
| 138 | + |
| 139 | + const rect = header_button.getBoundingClientRect(); |
| 140 | + return { |
| 141 | + x: Math.floor(rect.left + rect.width / 2), |
| 142 | + y: Math.floor(rect.top + (3 * rect.height) / 4), |
| 143 | + }; |
| 144 | + }, |
| 145 | + viewer, |
| 146 | + selector |
| 147 | + ); |
| 148 | + |
| 149 | + await page.mouse.click(x, y); |
| 150 | + const style_menu = await page.waitForSelector( |
| 151 | + `perspective-${selector2}-column-style` |
| 152 | + ); |
| 153 | + |
| 154 | + await new Promise((x) => setTimeout(x, 3000)); |
| 155 | + |
| 156 | + return get_contents( |
| 157 | + page, |
| 158 | + ` perspective-${selector2}-column-style`, |
| 159 | + true |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + test.capture("numeric columns", async (page) => { |
| 164 | + return await test_column(page, "", "number"); |
| 165 | + }); |
| 166 | + |
| 167 | + test.capture("string columns", async (page) => { |
| 168 | + return await test_column(page, ":nth-child(2)", "string"); |
| 169 | + }); |
| 170 | + |
| 171 | + // TODO Intl.supportedValuesOf doesn't exist in Chromium |
| 172 | + test.skip("date columns", async (page) => { |
| 173 | + await test_column(page, ":nth-child(3)", "datetime"); |
| 174 | + }); |
| 175 | + }); |
| 176 | + }, |
| 177 | + {root: path.join(__dirname, "..", "..")} |
| 178 | + ); |
| 179 | +}); |
| 180 | + |
| 181 | +// const click_details = async (page, x = 310, y = 300) => { |
| 182 | +// const viewer = await page.$("perspective-viewer"); |
| 183 | + |
| 184 | +// const click_event = page.evaluate( |
| 185 | +// element => |
| 186 | +// new Promise(resolve => { |
| 187 | +// element.addEventListener("perspective-click", e => { |
| 188 | +// resolve(e.detail); |
| 189 | +// }); |
| 190 | +// }), |
| 191 | +// viewer |
| 192 | +// ); |
| 193 | +// await page.mouse.click(x, y); |
| 194 | +// return await click_event; |
| 195 | +// }; |
0 commit comments