move metric parsing to state instantiation (#15069)

This commit is contained in:
Elizabeth Thompson 2021-06-09 14:54:25 -07:00 committed by GitHub
parent 821519fcfa
commit 0c470feaef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View File

@ -233,4 +233,21 @@ describe('DatasourceEditor RTL', () => {
);
expect(warningMarkdown.value).toEqual('someone');
});
it('properly updates the metric information', async () => {
render(<DatasourceEditor {...props} />, {
useRedux: true,
});
const metricButton = screen.getByTestId('collection-tab-Metrics');
userEvent.click(metricButton);
const expandToggle = await screen.findAllByLabelText(/toggle expand/i);
userEvent.click(expandToggle[1]);
const certifiedBy = await screen.findByPlaceholderText(/certified by/i);
userEvent.type(certifiedBy, 'I am typing a new name');
const certificationDetails = await screen.findByPlaceholderText(
/certification details/i,
);
expect(certifiedBy.value).toEqual('I am typing a new name');
userEvent.type(certificationDetails, 'I am typing something new');
expect(certificationDetails.value).toEqual('I am typing something new');
});
});

View File

@ -307,7 +307,21 @@ class DatasourceEditor extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
datasource: props.datasource,
datasource: {
...props.datasource,
metrics: props.datasource.metrics?.map(metric => {
const {
certification: { details, certified_by: certifiedBy } = {},
warning_markdown: warningMarkdown,
} = JSON.parse(metric.extra || '{}') || {};
return {
...metric,
certification_details: details || '',
warning_markdown: warningMarkdown || '',
certified_by: certifiedBy,
};
}),
},
errors: [],
isDruid:
props.datasource.type === 'druid' ||
@ -936,18 +950,7 @@ class DatasourceEditor extends React.PureComponent {
</Fieldset>
</FormContainer>
}
collection={this.state.datasource.metrics?.map(metric => {
const {
certification: { details, certified_by: certifiedBy } = {},
warning_markdown: warningMarkdown,
} = JSON.parse(metric.extra || '{}') || {};
return {
...metric,
certification_details: details || '',
warning_markdown: warningMarkdown || '',
certified_by: certifiedBy,
};
})}
collection={this.state.datasource.metrics}
allowAddItem
onChange={this.onDatasourcePropChange.bind(this, 'metrics')}
itemGenerator={() => ({