|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | + |
| 5 | +// Copyright 2025 Google LLC |
| 6 | +// |
| 7 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +// you may not use this file except in compliance with the License. |
| 9 | +// You may obtain a copy of the License at |
| 10 | +// |
| 11 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +// |
| 13 | +// Unless required by applicable law or agreed to in writing, software |
| 14 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +// See the License for the specific language governing permissions and |
| 17 | +// limitations under the License. |
| 18 | + |
| 19 | +import { jest } from "@jest/globals"; |
| 20 | +import "@testing-library/jest-dom"; |
| 21 | + |
| 22 | +describe("SeriesWidget", () => { |
| 23 | + let model; |
| 24 | + let el; |
| 25 | + let render; |
| 26 | + |
| 27 | + beforeEach(async () => { |
| 28 | + jest.resetModules(); |
| 29 | + document.body.innerHTML = "<div></div>"; |
| 30 | + el = document.body.querySelector("div"); |
| 31 | + |
| 32 | + const tableWidget = ( |
| 33 | + await import("../../bigframes/display/table_widget.js") |
| 34 | + ).default; |
| 35 | + render = tableWidget.render; |
| 36 | + |
| 37 | + model = { |
| 38 | + get: jest.fn(), |
| 39 | + set: jest.fn(), |
| 40 | + save_changes: jest.fn(), |
| 41 | + on: jest.fn(), |
| 42 | + }; |
| 43 | + }); |
| 44 | + |
| 45 | + it("should render the series as a table with an index and one value column", () => { |
| 46 | + // Mock the initial state |
| 47 | + model.get.mockImplementation((property) => { |
| 48 | + if (property === "table_html") { |
| 49 | + return ` |
| 50 | + <div class="paginated-table-container"> |
| 51 | + <div id="table-c" class="table-container"> |
| 52 | + <table class="bigframes-styles"> |
| 53 | + <thead> |
| 54 | + <tr> |
| 55 | + <th class="col-header-name"><div></div></th> |
| 56 | + <th class="col-header-name"><div>value</div></th> |
| 57 | + </tr> |
| 58 | + </thead> |
| 59 | + <tbody> |
| 60 | + <tr> |
| 61 | + <td class="cell-align-right">0</td> |
| 62 | + <td class="cell-align-left">a</td> |
| 63 | + </tr> |
| 64 | + <tr> |
| 65 | + <td class="cell-align-right">1</td> |
| 66 | + <td class="cell-align-left">b</td> |
| 67 | + </tr> |
| 68 | + </tbody> |
| 69 | + </table> |
| 70 | + </div> |
| 71 | + </div>`; |
| 72 | + } |
| 73 | + if (property === "orderable_columns") { |
| 74 | + return []; |
| 75 | + } |
| 76 | + return null; |
| 77 | + }); |
| 78 | + |
| 79 | + render({ model, el }); |
| 80 | + |
| 81 | + // Manually trigger the table_html change handler |
| 82 | + const tableHtmlChangeHandler = model.on.mock.calls.find( |
| 83 | + (call) => call[0] === "change:table_html", |
| 84 | + )[1]; |
| 85 | + tableHtmlChangeHandler(); |
| 86 | + |
| 87 | + // Check that the table has two columns |
| 88 | + const headers = el.querySelectorAll( |
| 89 | + ".paginated-table-container .col-header-name", |
| 90 | + ); |
| 91 | + expect(headers).toHaveLength(2); |
| 92 | + |
| 93 | + // Check that the headers are an empty string (for the index) and "value" |
| 94 | + expect(headers[0].textContent).toBe(""); |
| 95 | + expect(headers[1].textContent).toBe("value"); |
| 96 | + }); |
| 97 | +}); |
0 commit comments