1- import { ChartWidget } from "@mendix/shared-charts/main" ;
2- import { EditableValueBuilder , ListAttributeValueBuilder , listExp , list } from "@mendix/widget-plugin-test-utils" ;
3- import Big from "big.js" ;
4- import { mount , ReactWrapper } from "enzyme" ;
1+ jest . mock ( "@mendix/shared-charts/main" , ( ) => {
2+ const actualModule = jest . requireActual ( "@mendix/shared-charts/main" ) ;
3+ return {
4+ ...actualModule ,
5+ ChartWidget : jest . fn ( ( ) => null )
6+ } ;
7+ } ) ;
8+
9+ import { ChartWidget , setupBasicSeries } from "@mendix/shared-charts/main" ;
10+ import { listExpression } from "@mendix/widget-plugin-test-utils" ;
11+ import "@testing-library/jest-dom" ;
12+ import { render , RenderResult } from "@testing-library/react" ;
513import { createElement } from "react" ;
614import { SeriesType } from "../../typings/BarChartProps" ;
715import { BarChart } from "../BarChart" ;
816
917jest . mock ( "react-plotly.js" , ( ) => jest . fn ( ( ) => null ) ) ;
1018
1119describe ( "The BarChart widget" , ( ) => {
12- function renderBarChart ( configs : Array < Partial < SeriesType > > ) : ReactWrapper {
13- return mount (
20+ function renderBarChart ( configs : Array < Partial < SeriesType > > = [ { } ] ) : RenderResult {
21+ return render (
1422 < BarChart
1523 name = "bar-chart-test"
1624 class = "bar-chart-class"
1725 barmode = "group"
18- series = { configs . map ( setupBasicSeries ) }
26+ series = { configs . map ( setupBasicBarSeries ) }
1927 showLegend = { false }
2028 enableAdvancedOptions = { false }
2129 widthUnit = "percentage"
@@ -32,61 +40,60 @@ describe("The BarChart widget", () => {
3240 }
3341
3442 it ( "visualizes data as a bar chart" , ( ) => {
35- const barChart = renderBarChart ( [ { } ] ) ;
36- const data = barChart . find ( ChartWidget ) . prop ( "data" ) ;
37- expect ( data ) . toHaveLength ( 1 ) ;
38- expect ( data [ 0 ] ) . toHaveProperty ( "type" , "bar" ) ;
43+ renderBarChart ( ) ;
44+
45+ expect ( ChartWidget ) . toHaveBeenCalledWith (
46+ expect . objectContaining ( {
47+ data : expect . arrayContaining ( [ expect . objectContaining ( { type : "bar" } ) ] )
48+ } ) ,
49+ { }
50+ ) ;
3951 } ) ;
4052
4153 it ( "sets the bar color on the data series based on the barColor value" , ( ) => {
42- const barChart = renderBarChart ( [ { staticBarColor : listExp ( ( ) => "red" ) } , { staticBarColor : undefined } ] ) ;
43- const data = barChart . find ( ChartWidget ) . prop ( "data" ) ;
44- expect ( data ) . toHaveLength ( 2 ) ;
45- expect ( data [ 0 ] ) . toHaveProperty ( "marker.color" , "red" ) ;
46- expect ( data [ 1 ] ) . toHaveProperty ( "marker.color" , undefined ) ;
54+ renderBarChart ( [ { staticBarColor : listExpression ( ( ) => "red" ) } , { staticBarColor : undefined } ] ) ;
55+
56+ expect ( ChartWidget ) . toHaveBeenCalledWith (
57+ expect . objectContaining ( {
58+ data : expect . arrayContaining ( [
59+ expect . objectContaining ( { marker : expect . objectContaining ( { color : "red" } ) } ) ,
60+ expect . objectContaining ( { marker : expect . objectContaining ( { color : undefined } ) } )
61+ ] )
62+ } ) ,
63+ { }
64+ ) ;
4765 } ) ;
4866
4967 it ( "sets the appropriate transforms on the data series based on the aggregation type" , ( ) => {
50- const barChart = renderBarChart ( [ { aggregationType : "none" } , { aggregationType : "avg" } ] ) ;
51- const data = barChart . find ( ChartWidget ) . prop ( "data" ) ;
52- expect ( data ) . toHaveLength ( 2 ) ;
53- expect ( data [ 0 ] ) . toHaveProperty ( "transforms" , [ ] ) ;
54- expect ( data [ 1 ] ) . toHaveProperty ( "transforms" , [
55- {
56- type : "aggregate" ,
57- groups : [ "1" , "2" ] ,
58- aggregations : [
59- {
60- target : "y" ,
61- enabled : true ,
62- func : "avg"
63- }
64- ]
65- }
66- ] ) ;
68+ renderBarChart ( [ { aggregationType : "none" } , { aggregationType : "avg" } ] ) ;
69+
70+ expect ( ChartWidget ) . toHaveBeenCalledWith (
71+ expect . objectContaining ( {
72+ data : expect . arrayContaining ( [
73+ expect . objectContaining ( { transforms : expect . arrayContaining ( [ ] ) } ) ,
74+ expect . objectContaining ( {
75+ transforms : expect . arrayContaining ( [
76+ expect . objectContaining ( {
77+ type : "aggregate" ,
78+ groups : expect . arrayContaining ( [ "1" , "2" ] ) ,
79+ aggregations : expect . arrayContaining ( [
80+ expect . objectContaining ( { target : "y" , enabled : true , func : "avg" } )
81+ ] )
82+ } )
83+ ] )
84+ } )
85+ ] )
86+ } ) ,
87+ { }
88+ ) ;
6789 } ) ;
6890} ) ;
6991
70- function setupBasicSeries ( overwriteConfig : Partial < SeriesType > ) : SeriesType {
71- const xAttribute = new ListAttributeValueBuilder < Big > ( ) . build ( ) ;
72- const getXAttributeMock = jest . fn ( ) ;
73- getXAttributeMock . mockReturnValueOnce ( new EditableValueBuilder < Big > ( ) . withValue ( new Big ( 1 ) ) . build ( ) ) ;
74- getXAttributeMock . mockReturnValueOnce ( new EditableValueBuilder < Big > ( ) . withValue ( new Big ( 2 ) ) . build ( ) ) ;
75- xAttribute . get = getXAttributeMock ;
76-
77- const yAttribute = new ListAttributeValueBuilder < Big > ( ) . build ( ) ;
78- const getYAttributeMock = jest . fn ( ) ;
79- getYAttributeMock . mockReturnValueOnce ( new EditableValueBuilder < Big > ( ) . withValue ( new Big ( 3 ) ) . build ( ) ) ;
80- getYAttributeMock . mockReturnValueOnce ( new EditableValueBuilder < Big > ( ) . withValue ( new Big ( 6 ) ) . build ( ) ) ;
81- yAttribute . get = getYAttributeMock ;
92+ function setupBasicBarSeries ( overwriteConfig : Partial < SeriesType > ) : SeriesType {
93+ const basicSeries = setupBasicSeries ( overwriteConfig ) as SeriesType ;
8294
8395 return {
84- dataSet : "static" ,
85- customSeriesOptions : overwriteConfig . customSeriesOptions ?? "" ,
86- aggregationType : overwriteConfig . aggregationType ?? "avg" ,
87- staticBarColor : overwriteConfig . staticBarColor ?? undefined ,
88- staticDataSource : list ( 2 ) ,
89- staticXAttribute : xAttribute ,
90- staticYAttribute : yAttribute
96+ ...basicSeries ,
97+ staticBarColor : overwriteConfig . staticBarColor ?? undefined
9198 } ;
9299}
0 commit comments