sadpandajoe commented on code in PR #40000:
URL: https://github.com/apache/superset/pull/40000#discussion_r3432191563


##########
superset-frontend/src/chartCustomizations/components/TimeGrain/TimeGrainPreFilter.integration.test.tsx:
##########
@@ -0,0 +1,169 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Integration Test: Time Grain Pre-filter Feature (Customization plugin)
+ *
+ * Mirrors 
src/filters/components/TimeGrain/TimeGrainPreFilter.integration.test.tsx
+ * but targets the customization plugin at
+ * src/chartCustomizations/components/TimeGrain/TimeGrainFilterPlugin and adds
+ * a case for the escape hatch the customization version introduces.
+ */
+
+import {
+  render,
+  screen,
+  userEvent,
+  waitFor,
+} from 'spec/helpers/testing-library';
+import PluginFilterTimegrain from 
'src/chartCustomizations/components/TimeGrain/TimeGrainFilterPlugin';
+
+const data = [
+  { duration: 'PT1M', name: 'Minute' },
+  { duration: 'PT1H', name: 'Hour' },
+  { duration: 'P1D', name: 'Day' },
+  { duration: 'P1W', name: 'Week' },
+  { duration: 'P1M', name: 'Month' },
+];
+
+const baseConfig = {
+  height: 100,
+  width: 300,
+  setFilterActive: jest.fn(),
+  setHoveredFilter: jest.fn(),
+  unsetHoveredFilter: jest.fn(),
+  setFocusedFilter: jest.fn(),
+  unsetFocusedFilter: jest.fn(),
+  inputRef: { current: null },
+};
+
+/**
+ * Scenario: Dashboard owner configures a time grain filter to show only Hour, 
Day, Week.
+ * End-user opens the dashboard and can only select from those three options.
+ */
+test('time grain pre-filter restricts dashboard filter options', async () => {
+  const setDataMask = jest.fn();
+  const dashboardConfig = {
+    ...baseConfig,
+    data,
+    formData: {
+      nativeFilterId: 'time_grain_1',
+      defaultValue: null,
+      viz_type: 'filter_timegrain',
+      time_grains: ['PT1H', 'P1D', 'P1W'],
+    },
+    filterState: {
+      value: null,
+      validateStatus: undefined,
+      validateMessage: undefined,
+    },
+    setDataMask,
+  };
+
+  render(<PluginFilterTimegrain {...(dashboardConfig as any)} />);
+
+  setDataMask.mockClear();
+
+  await userEvent.click(screen.getByRole('combobox'));
+
+  await waitFor(() => {
+    const labels = screen.getAllByRole('option').map(o => o.textContent);
+    expect(labels).toEqual(['Hour', 'Day', 'Week']);
+    expect(labels).not.toContain('Minute');
+    expect(labels).not.toContain('Month');
+  });
+
+  await userEvent.click(screen.getByText('Day'));
+
+  await waitFor(() => {
+    expect(setDataMask).toHaveBeenCalledWith({
+      extraFormData: { time_grain_sqla: 'P1D' },
+      filterState: { label: 'Day', value: ['P1D'] },
+    });
+  });
+});
+
+/**
+ * Scenario: Dashboard owner disables pre-filter (unchecks the 
CollapsibleControl).
+ * No restrictions: all time grains appear in the runtime filter.
+ */
+test('all time grains appear when pre-filter is unchecked', async () => {
+  const dashboardConfig = {
+    ...baseConfig,
+    data,
+    formData: {
+      nativeFilterId: 'time_grain_1',
+      defaultValue: null,
+      viz_type: 'filter_timegrain',
+      time_grains: undefined,
+    },
+    filterState: {
+      value: null,
+      validateStatus: undefined,
+      validateMessage: undefined,
+    },
+    setDataMask: jest.fn(),
+  };
+
+  render(<PluginFilterTimegrain {...(dashboardConfig as any)} />);

Review Comment:
   same as above comment



##########
superset-frontend/src/chartCustomizations/components/TimeGrain/TimeGrainPreFilter.integration.test.tsx:
##########
@@ -0,0 +1,169 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Integration Test: Time Grain Pre-filter Feature (Customization plugin)
+ *
+ * Mirrors 
src/filters/components/TimeGrain/TimeGrainPreFilter.integration.test.tsx
+ * but targets the customization plugin at
+ * src/chartCustomizations/components/TimeGrain/TimeGrainFilterPlugin and adds
+ * a case for the escape hatch the customization version introduces.
+ */
+
+import {
+  render,
+  screen,
+  userEvent,
+  waitFor,
+} from 'spec/helpers/testing-library';
+import PluginFilterTimegrain from 
'src/chartCustomizations/components/TimeGrain/TimeGrainFilterPlugin';
+
+const data = [
+  { duration: 'PT1M', name: 'Minute' },
+  { duration: 'PT1H', name: 'Hour' },
+  { duration: 'P1D', name: 'Day' },
+  { duration: 'P1W', name: 'Week' },
+  { duration: 'P1M', name: 'Month' },
+];
+
+const baseConfig = {
+  height: 100,
+  width: 300,
+  setFilterActive: jest.fn(),
+  setHoveredFilter: jest.fn(),
+  unsetHoveredFilter: jest.fn(),
+  setFocusedFilter: jest.fn(),
+  unsetFocusedFilter: jest.fn(),
+  inputRef: { current: null },
+};
+
+/**
+ * Scenario: Dashboard owner configures a time grain filter to show only Hour, 
Day, Week.
+ * End-user opens the dashboard and can only select from those three options.
+ */
+test('time grain pre-filter restricts dashboard filter options', async () => {
+  const setDataMask = jest.fn();
+  const dashboardConfig = {
+    ...baseConfig,
+    data,
+    formData: {
+      nativeFilterId: 'time_grain_1',
+      defaultValue: null,
+      viz_type: 'filter_timegrain',
+      time_grains: ['PT1H', 'P1D', 'P1W'],
+    },
+    filterState: {
+      value: null,
+      validateStatus: undefined,
+      validateMessage: undefined,
+    },
+    setDataMask,
+  };
+
+  render(<PluginFilterTimegrain {...(dashboardConfig as any)} />);
+
+  setDataMask.mockClear();
+
+  await userEvent.click(screen.getByRole('combobox'));
+
+  await waitFor(() => {
+    const labels = screen.getAllByRole('option').map(o => o.textContent);
+    expect(labels).toEqual(['Hour', 'Day', 'Week']);
+    expect(labels).not.toContain('Minute');
+    expect(labels).not.toContain('Month');
+  });
+
+  await userEvent.click(screen.getByText('Day'));
+
+  await waitFor(() => {
+    expect(setDataMask).toHaveBeenCalledWith({
+      extraFormData: { time_grain_sqla: 'P1D' },
+      filterState: { label: 'Day', value: ['P1D'] },
+    });
+  });
+});
+
+/**
+ * Scenario: Dashboard owner disables pre-filter (unchecks the 
CollapsibleControl).
+ * No restrictions: all time grains appear in the runtime filter.
+ */
+test('all time grains appear when pre-filter is unchecked', async () => {
+  const dashboardConfig = {
+    ...baseConfig,
+    data,
+    formData: {
+      nativeFilterId: 'time_grain_1',
+      defaultValue: null,
+      viz_type: 'filter_timegrain',
+      time_grains: undefined,
+    },
+    filterState: {
+      value: null,
+      validateStatus: undefined,
+      validateMessage: undefined,
+    },
+    setDataMask: jest.fn(),
+  };
+
+  render(<PluginFilterTimegrain {...(dashboardConfig as any)} />);
+
+  await userEvent.click(screen.getByRole('combobox'));
+
+  await waitFor(() => {
+    const labels = screen.getAllByRole('option').map(o => o.textContent);
+    expect(labels).toEqual(['Minute', 'Hour', 'Day', 'Week', 'Month']);
+  });
+});
+
+/**
+ * Scenario: Dashboard owner narrowed the pre-filter after an end-user had
+ * already selected a value that now falls outside the allowlist.
+ * The current selection stays visible so the filter does not silently drop it.
+ */
+test('current selection stays visible when it is outside the pre-filter 
allowlist', async () => {
+  const dashboardConfig = {
+    ...baseConfig,
+    data,
+    formData: {
+      nativeFilterId: 'time_grain_1',
+      defaultValue: null,
+      viz_type: 'filter_timegrain',
+      time_grains: ['PT1H', 'P1D', 'P1W'],
+    },
+    filterState: {
+      value: ['P1M'],
+      validateStatus: undefined,
+      validateMessage: undefined,
+    },
+    setDataMask: jest.fn(),
+  };
+
+  render(<PluginFilterTimegrain {...(dashboardConfig as any)} />);

Review Comment:
   Same as comment on line 79



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to