@@ -62,6 +62,22 @@ module ForestAdminDatasourceCustomizer
62
62
expect ( datasource . schema [ :charts ] ) . to include ( 'my_chart' )
63
63
expect ( datasource . render_chart ( caller , 'my_chart' ) ) . to eq ( { countCurrent : 10 , countPrevious : nil } )
64
64
end
65
+
66
+ it 'adds charts from datasource schema to the composite datasource' do
67
+ chart = 'my_chart'
68
+ collection = instance_double ( ForestAdminDatasourceToolkit ::Collection , name : 'dummy' )
69
+
70
+ datasource = instance_double ( ForestAdminDatasourceToolkit ::Datasource ,
71
+ schema : { charts : [ chart ] } ,
72
+ collections : { 'dummy' => collection } )
73
+
74
+ customizer = described_class . new
75
+ customizer . add_datasource ( datasource , { } )
76
+ customizer . stack . apply_queued_customizations ( { } )
77
+
78
+ charts = customizer . instance_variable_get ( :@composite_datasource ) . schema [ :charts ]
79
+ expect ( charts ) . to include ( chart )
80
+ end
65
81
end
66
82
67
83
context 'when using reload!' do
@@ -123,5 +139,69 @@ module ForestAdminDatasourceCustomizer
123
139
. to eq ( first_datasource )
124
140
end
125
141
end
142
+
143
+ it 'returns the validation schema' do
144
+ customizer = described_class . new
145
+ validation = instance_double ( ValidationDecorator , schema : { foo : 'bar' } )
146
+ customizer . stack . instance_variable_set ( :@validation , validation )
147
+
148
+ expect ( customizer . schema ) . to eq ( foo : 'bar' )
149
+ end
150
+
151
+ it 'applies include/exclude filters using PublicationDatasourceDecorator' do
152
+ decorated = described_class . new
153
+ datasource = build_datasource
154
+ publication_spy = instance_spy ( ForestAdminDatasourceCustomizer ::Decorators ::Publication ::PublicationDatasourceDecorator )
155
+
156
+ allow ( ForestAdminDatasourceCustomizer ::Decorators ::Publication ::PublicationDatasourceDecorator )
157
+ . to receive ( :new ) . and_return ( publication_spy )
158
+
159
+ allow ( publication_spy ) . to receive_messages ( schema : { charts : [ ] } , collections : { } )
160
+ allow ( publication_spy ) . to receive ( :keep_collections_matching )
161
+
162
+ decorated . add_datasource ( datasource , include : [ 'users' ] )
163
+
164
+ decorated . stack . apply_queued_customizations ( { } )
165
+
166
+ expect ( publication_spy ) . to have_received ( :keep_collections_matching ) . with ( [ 'users' ] , nil )
167
+ end
168
+
169
+ it 'raises an error if the chart is not found in any datasource' do
170
+ datasource = instance_double ( ForestAdminDatasourceToolkit ::Datasource ,
171
+ schema : { charts : [ ] } ,
172
+ render_chart : nil )
173
+
174
+ customizer = described_class . new
175
+ customizer . add_datasource ( datasource , { } )
176
+
177
+ expect do
178
+ customizer . render_chart ( :caller , 'unknown_chart' )
179
+ end . to raise_error ( ForestAdminAgent ::Http ::Exceptions ::NotFoundError , "Chart 'unknown_chart' is not defined in the dataSource." )
180
+ end
181
+
182
+ it 'calls run on the plugin with correct arguments' do
183
+ plugin_instance = instance_double ( MyPlugin , run : nil )
184
+ plugin = class_double ( MyPlugin , new : plugin_instance )
185
+
186
+ customizer = described_class . new
187
+ customizer . use ( plugin , [ :a ] )
188
+
189
+ customizer . stack . apply_queued_customizations ( { } )
190
+
191
+ expect ( plugin_instance ) . to have_received ( :run ) . with ( customizer , nil , [ :a ] )
192
+ end
193
+
194
+ it 'calls the handle with the collection customizer' do
195
+ customizer = described_class . new
196
+ customizer . add_datasource ( datasource , { } )
197
+ customizer . stack . apply_queued_customizations ( { } )
198
+
199
+ received_name = nil
200
+ handle = -> ( collection ) { received_name = collection . name }
201
+
202
+ customizer . customize_collection ( 'collection' , handle )
203
+
204
+ expect ( received_name ) . to eq ( 'collection' )
205
+ end
126
206
end
127
207
end
0 commit comments