Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Lens] Do not reset formatting when switching between custom ranges and auto histogram #82694

Merged
merged 8 commits into from
Nov 10, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,55 @@ describe('ranges', () => {
/^Bytes format:/
);
});

it('should not reset formatters when switching between custom ranges and auto histogram', () => {
const setStateSpy = jest.fn();

// Add an extra range
(state.layers.first.columns.col1 as RangeIndexPatternColumn).params.ranges.push({
from: DEFAULT_INTERVAL,
to: 2 * DEFAULT_INTERVAL,
label: '',
});

state.indexPatterns['1'].fieldFormatMap = {
MyField: { id: 'custom', params: {} },
};

// now set a format on the range operation
(state.layers.first.columns.col1 as RangeIndexPatternColumn).params.format = {
id: 'custom',
params: { decimals: 0 },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like the only difference between the "default" format of MyField and the overwrite on the column are the params. Your test doesn't really validate these params are kept - I commented out lines 763 to 766 and the tests still pass. I suggest not setting the same formatter in the field format map of the index pattern (line 759)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understood that completely. Should I switch it to test for params on the same line(759)?

Copy link
Contributor

@flash1293 flash1293 Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meant setting it to the number formatter there so you can actually check whether the format is persisted.

But I just realized your test is not testing the right thing anyway. lns-customBucketContainer-remove is removing one out of multiple ranges, but that's not what should be tested here - this is about switching between custom ranges in general and automatic ranges controlled by granularity.

A proper unit test should click the "Remove custom ranges" button, then check whether the state update issued by this still includes the custom format.

Let me know when you have problems adding such a test. I can do it and add it to your PR

Copy link
Contributor Author

@denismaxim0v denismaxim0v Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I meant setting it to the number formatter there so you can actually check whether the format is persisted.

But I just realized your test is not testing the right thing anyway. lns-customBucketContainer-remove is removing one out of multiple ranges, but that's not what should be tested here - this is about switching between custom ranges in general and automatic ranges controlled by granularity.

A proper unit test should click the "Remove custom ranges" button, then check whether the state update issued by this still includes the custom format.

Let me know when you have problems adding such a test. I can do it and add it to your PR

So, I have been trying to

it('should not reset formatters when switching between custom ranges and auto histogram', () => {
        const setStateSpy = jest.fn();
        (state.layers.first.columns.col1 as RangeIndexPatternColumn).params.ranges.push({
          from: null,
          to: null,
          label: '',
        });
        // set a default formatter for the sourceField used
        state.indexPatterns['1'].fieldFormatMap = {
          MyField: { id: 'custom', params: {} },
        };
        // now set a format on the range operation
        (state.layers.first.columns.col1 as RangeIndexPatternColumn).params.format = {
          id: 'custom',
          params: { decimals: 3 },
        };

        const instance = mount(
          <InlineOptions
            {...defaultOptions}
            state={state}
            setState={setStateSpy}
            columnId="col1"
            currentColumn={state.layers.first.columns.col1 as RangeIndexPatternColumn}
            layerId="first"
          />
        );
        //expect(instance.find(RangePopover)).toHaveLength(2);
        // This series of act closures are made to make it work properly the update flush
        act(() => {
          instance
            .find(".euiLink")
            .first()
            .prop('onClick')!({} as ReactMouseEvent);

          expect(setStateSpy).toHaveBeenCalledWith({
            ...state,
            layers: {
              first: {
                ...state.layers.first,
                columns: {
                  ...state.layers.first.columns,
                  col1: {
                    ...state.layers.first.columns.col1,
                    params: {
                      ...state.layers.first.columns.col1.params,
                      format: {
                        id: "custom",
                        params: { decimals: 3 }
                      }
                    },
                  },
                },
              },
            },
          });
        });

But it seems that Number of calls: 2 and I'm not sure how to handle that

};

const instance = mount(
<InlineOptions
{...defaultOptions}
state={state}
setState={setStateSpy}
columnId="col1"
currentColumn={state.layers.first.columns.col1 as RangeIndexPatternColumn}
layerId="first"
/>
);

expect(instance.find(RangePopover)).toHaveLength(2);

// This series of act closures are made to make it work properly the update flush
act(() => {
instance
.find('[data-test-subj="lns-customBucketContainer-remove"]')
.last()
.prop('onClick')!({} as ReactMouseEvent);
});

act(() => {
instance.update();
expect(instance.find(RangePopover).find(EuiText).prop('children')).toMatch(
/^Custom format:/
);
});
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export const rangeOperation: OperationDefinition<RangeIndexPatternColumn, 'field
type: newMode,
ranges: [{ from: 0, to: DEFAULT_INTERVAL, label: '' }],
maxBars: maxBarsDefaultValue,
format: undefined,
format: currentColumn.params.format,
parentFormat,
},
},
Expand Down