|
14 | 14 | * limitations under the License.
|
15 | 15 | */
|
16 | 16 |
|
17 |
| -import { Theme } from "streamlit-component-lib"; |
| 17 | +import { Theme } from "streamlit-component-lib" |
18 | 18 |
|
19 | 19 | import {
|
20 | 20 | getChartDataGenerator,
|
21 | 21 | setChartThemeGenerator,
|
22 | 22 | getChartDimensions,
|
23 |
| -} from "./index"; |
| 23 | +} from "./index" |
24 | 24 |
|
25 | 25 | describe("getChartDataGenerator", () => {
|
26 | 26 | let getChartData: (figure: string) => {
|
27 |
| - data: object | null; |
28 |
| - hasChanged: boolean; |
29 |
| - }; |
| 27 | + data: object | null |
| 28 | + hasChanged: boolean |
| 29 | + } |
30 | 30 |
|
31 | 31 | beforeEach(() => {
|
32 |
| - getChartData = getChartDataGenerator(); |
33 |
| - }); |
| 32 | + getChartData = getChartDataGenerator() |
| 33 | + }) |
34 | 34 |
|
35 | 35 | test("should return parsed data and hasChanged true on first call", () => {
|
36 |
| - const figure = JSON.stringify({ key: "value" }); |
37 |
| - const result = getChartData(figure); |
| 36 | + const figure = JSON.stringify({ key: "value" }) |
| 37 | + const result = getChartData(figure) |
38 | 38 |
|
39 |
| - expect(result).toEqual({ data: { key: "value" }, hasChanged: true }); |
40 |
| - }); |
| 39 | + expect(result).toEqual({ data: { key: "value" }, hasChanged: true }) |
| 40 | + }) |
41 | 41 |
|
42 | 42 | test("should return hasChanged false for the same figure", () => {
|
43 |
| - const figure = JSON.stringify({ key: "value" }); |
44 |
| - getChartData(figure); |
45 |
| - const result = getChartData(figure); |
| 43 | + const figure = JSON.stringify({ key: "value" }) |
| 44 | + getChartData(figure) |
| 45 | + const result = getChartData(figure) |
46 | 46 |
|
47 |
| - expect(result).toEqual({ data: { key: "value" }, hasChanged: false }); |
48 |
| - }); |
| 47 | + expect(result).toEqual({ data: { key: "value" }, hasChanged: false }) |
| 48 | + }) |
49 | 49 |
|
50 | 50 | test("should return hasChanged true for a different figure", () => {
|
51 |
| - getChartData(JSON.stringify({ key: "value" })); |
52 |
| - const newFigure = JSON.stringify({ key: "newValue" }); |
53 |
| - const result = getChartData(newFigure); |
| 51 | + getChartData(JSON.stringify({ key: "value" })) |
| 52 | + const newFigure = JSON.stringify({ key: "newValue" }) |
| 53 | + const result = getChartData(newFigure) |
54 | 54 |
|
55 |
| - expect(result).toEqual({ data: { key: "newValue" }, hasChanged: true }); |
56 |
| - }); |
57 |
| -}); |
| 55 | + expect(result).toEqual({ data: { key: "newValue" }, hasChanged: true }) |
| 56 | + }) |
| 57 | +}) |
58 | 58 |
|
59 | 59 | // Unit tests for setChartThemeGenerator
|
60 | 60 | describe("setChartThemeGenerator", () => {
|
61 |
| - let setChartTheme: (newTheme: string, newAppTheme: Theme) => boolean; |
| 61 | + let setChartTheme: (newTheme: string, newAppTheme: Theme) => boolean |
62 | 62 |
|
63 | 63 | beforeEach(() => {
|
64 |
| - setChartTheme = setChartThemeGenerator(); |
65 |
| - }); |
| 64 | + setChartTheme = setChartThemeGenerator() |
| 65 | + }) |
66 | 66 |
|
67 | 67 | test("should apply the theme when theme changes", () => {
|
68 |
| - const newTheme = "dark"; |
| 68 | + const newTheme = "dark" |
69 | 69 | const newAppTheme = {
|
70 | 70 | textColor: "white",
|
71 | 71 | backgroundColor: "black",
|
72 | 72 | secondaryBackgroundColor: "gray",
|
73 |
| - } as Theme; |
74 |
| - const result = setChartTheme(newTheme, newAppTheme); |
| 73 | + } as Theme |
| 74 | + const result = setChartTheme(newTheme, newAppTheme) |
75 | 75 | const { use_theme: useTheme } =
|
76 |
| - global.window.Bokeh.require("core/properties"); |
| 76 | + global.window.Bokeh.require("core/properties") |
77 | 77 |
|
78 |
| - expect(result).toBe(true); |
79 |
| - expect(useTheme).toHaveBeenCalled(); |
80 |
| - }); |
| 78 | + expect(result).toBe(true) |
| 79 | + expect(useTheme).toHaveBeenCalled() |
| 80 | + }) |
81 | 81 |
|
82 | 82 | test("should not reapply the theme if it's the same", () => {
|
83 |
| - const newTheme = "dark"; |
| 83 | + const newTheme = "dark" |
84 | 84 | const newAppTheme = {
|
85 | 85 | textColor: "white",
|
86 | 86 | backgroundColor: "black",
|
87 | 87 | secondaryBackgroundColor: "gray",
|
88 |
| - } as Theme; |
89 |
| - setChartTheme(newTheme, newAppTheme); |
90 |
| - const result = setChartTheme(newTheme, newAppTheme); |
| 88 | + } as Theme |
| 89 | + setChartTheme(newTheme, newAppTheme) |
| 90 | + const result = setChartTheme(newTheme, newAppTheme) |
91 | 91 |
|
92 |
| - expect(result).toBe(false); |
93 |
| - }); |
| 92 | + expect(result).toBe(false) |
| 93 | + }) |
94 | 94 |
|
95 | 95 | test("should apply Streamlit theme when appropriate", () => {
|
96 |
| - const newTheme = "streamlit"; |
| 96 | + const newTheme = "streamlit" |
97 | 97 | const newAppTheme = {
|
98 | 98 | textColor: "white",
|
99 | 99 | backgroundColor: "black",
|
100 | 100 | secondaryBackgroundColor: "gray",
|
101 |
| - } as Theme; |
102 |
| - const result = setChartTheme(newTheme, newAppTheme); |
| 101 | + } as Theme |
| 102 | + const result = setChartTheme(newTheme, newAppTheme) |
103 | 103 |
|
104 |
| - expect(result).toBe(true); |
105 |
| - }); |
106 |
| -}); |
| 104 | + expect(result).toBe(true) |
| 105 | + }) |
| 106 | +}) |
107 | 107 |
|
108 | 108 | describe("getChartDimensions", () => {
|
109 | 109 | test("should return default dimensions when no width/height attributes are provided", () => {
|
110 |
| - const plot = { attributes: {} }; |
111 |
| - const result = getChartDimensions(plot, false); |
112 |
| - expect(result).toEqual({ width: 600, height: 600 }); |
113 |
| - }); |
| 110 | + const plot = { attributes: {} } |
| 111 | + const result = getChartDimensions(plot, false) |
| 112 | + expect(result).toEqual({ width: 600, height: 600 }) |
| 113 | + }) |
114 | 114 |
|
115 | 115 | test("should return provided dimensions when width/height attributes are set", () => {
|
116 |
| - const plot = { attributes: { width: 800, height: 400 } }; |
117 |
| - const result = getChartDimensions(plot, false); |
118 |
| - expect(result).toEqual({ width: 800, height: 400 }); |
119 |
| - }); |
| 116 | + const plot = { attributes: { width: 800, height: 400 } } |
| 117 | + const result = getChartDimensions(plot, false) |
| 118 | + expect(result).toEqual({ width: 800, height: 400 }) |
| 119 | + }) |
120 | 120 |
|
121 | 121 | test("should calculate new dimensions based on container width", () => {
|
122 | 122 | Object.defineProperty(document.documentElement, "clientWidth", {
|
123 | 123 | configurable: true,
|
124 | 124 | writable: true,
|
125 | 125 | value: 1200, // Set the desired value
|
126 |
| - }); |
127 |
| - |
128 |
| - const plot = { attributes: { width: 800, height: 400 } }; |
129 |
| - const result = getChartDimensions(plot, true); |
130 |
| - expect(result.width).toBe(1200); |
131 |
| - expect(result.height).toBeCloseTo(600); |
132 |
| - }); |
133 |
| -}); |
| 126 | + }) |
| 127 | + |
| 128 | + const plot = { attributes: { width: 800, height: 400 } } |
| 129 | + const result = getChartDimensions(plot, true) |
| 130 | + expect(result.width).toBe(1200) |
| 131 | + expect(result.height).toBeCloseTo(600) |
| 132 | + }) |
| 133 | +}) |
0 commit comments