-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor series color resolution, fix nits
- Loading branch information
1 parent
ac3ee18
commit 60973ef
Showing
9 changed files
with
83 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...ility_solution/infra/public/components/asset_details/hooks/use_chart_series_color.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useEuiTheme } from '@elastic/eui'; | ||
import { renderHook } from '@testing-library/react'; | ||
import { useChartSeriesColor } from './use_chart_series_color'; | ||
|
||
describe('useChartSeriesColor', () => { | ||
let seriesDefaultColor: string; | ||
|
||
beforeEach(() => { | ||
const { result } = renderHook(() => useEuiTheme()); | ||
|
||
// Don't try to test a hardcoded value, just use what is provided by EUI. | ||
// If in the future this value changes, the tests won't break. | ||
seriesDefaultColor = result.current.euiTheme.colors.backgroundLightText; | ||
}); | ||
|
||
it('returns a default color value if given no input', () => { | ||
const { result } = renderHook(() => useChartSeriesColor()); | ||
|
||
expect(result.current).not.toBe(''); | ||
expect(result.current).toBe(seriesDefaultColor); | ||
}); | ||
|
||
it('returns a default color value if given an empty string', () => { | ||
const { result } = renderHook(() => useChartSeriesColor('')); | ||
|
||
expect(result.current).not.toBe(''); | ||
expect(result.current).toBe(seriesDefaultColor); | ||
}); | ||
|
||
it('returns the provided color input', () => { | ||
const { result } = renderHook(() => useChartSeriesColor('#fff')); | ||
|
||
expect(result.current).not.toBe(seriesDefaultColor); | ||
expect(result.current).toBe('#fff'); | ||
}); | ||
}); |
21 changes: 21 additions & 0 deletions
21
...ervability_solution/infra/public/components/asset_details/hooks/use_chart_series_color.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { useEuiTheme } from '@elastic/eui'; | ||
|
||
/** | ||
* Provides either the input color, or yields the default EUI theme | ||
* color for use as the KPI chart series color. | ||
* @param seriesColor A user-defined color value | ||
* @returns Either the input `seriesColor` or the default color from EUI | ||
*/ | ||
export const useChartSeriesColor = (seriesColor?: string): string => { | ||
const { euiTheme } = useEuiTheme(); | ||
|
||
// Prevent empty string being used as a valid color | ||
return seriesColor || euiTheme.colors.backgroundLightText; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters