Skip to content

Commit 4b1f5ce

Browse files
committed
test(EuiBadge): add color assertions to ensure correct color values are applies for each fill prop combination
1 parent 688b3d0 commit 4b1f5ce

1 file changed

Lines changed: 122 additions & 1 deletion

File tree

packages/eui/src/components/badge/badge.test.tsx

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@ import { requiredProps } from '../../test/required_props';
1212
import { render, renderHook } from '../../test/rtl';
1313
import { useEuiTheme } from '../../services';
1414

15-
import { EuiBadge, COLORS, ICON_SIDES } from './badge';
15+
import { EuiBadge, COLORS, ICON_SIDES, BadgeColor } from './badge';
1616
import { mathWithUnits, UseEuiTheme } from '@elastic/eui-theme-common';
17+
import { RenderResult } from '@testing-library/react';
18+
19+
interface Colors {
20+
backgroundColor: string;
21+
textColor: string;
22+
borderColor?: string;
23+
}
24+
25+
type ColorsMap = Record<BadgeColor, Colors>;
1726

1827
describe('EuiBadge', () => {
1928
shouldRenderCustomStyles(
@@ -251,4 +260,116 @@ describe('EuiBadge', () => {
251260
);
252261
});
253262
});
263+
264+
describe('fill', () => {
265+
// These are purposefully hardcoded to ensure no color values get updates
266+
// accidentally.
267+
const baseColorsMap: ColorsMap = {
268+
default: {
269+
backgroundColor: '#E3E8F2',
270+
textColor: '#07101F',
271+
},
272+
hollow: {
273+
backgroundColor: '#FFFFFF',
274+
textColor: '#07101F',
275+
borderColor: '#CAD3E2',
276+
},
277+
primary: { backgroundColor: '#D9E8FF', textColor: '#07101F' },
278+
accent: { backgroundColor: '#FDDDE9', textColor: '#07101F' },
279+
neutral: { backgroundColor: '#CFEEF7', textColor: '#07101F' },
280+
success: { backgroundColor: '#C9F3E3', textColor: '#07101F' },
281+
warning: { backgroundColor: '#FDE9B5', textColor: '#07101F' },
282+
risk: { backgroundColor: '#FFDEBF', textColor: '#07101F' },
283+
danger: { backgroundColor: '#FDDDD8', textColor: '#07101F' },
284+
};
285+
286+
const fillColorsMap: ColorsMap = {
287+
default: {
288+
backgroundColor: '#E3E8F2',
289+
textColor: '#07101F',
290+
},
291+
hollow: {
292+
backgroundColor: '#FFFFFF',
293+
textColor: '#07101F',
294+
borderColor: '#CAD3E2',
295+
},
296+
primary: { backgroundColor: '#0B64DD', textColor: '#FFFFFF' },
297+
accent: { backgroundColor: '#BC1E70', textColor: '#FFFFFF' },
298+
neutral: { backgroundColor: '#1C8CB5', textColor: '#FFFFFF' },
299+
success: { backgroundColor: '#008A5E', textColor: '#FFFFFF' },
300+
warning: { backgroundColor: '#FACB3D', textColor: '#6A4906' },
301+
risk: { backgroundColor: '#ED6723', textColor: '#FFFFFF' },
302+
danger: { backgroundColor: '#C61E25', textColor: '#FFFFFF' },
303+
};
304+
305+
const assertColor = (
306+
color: BadgeColor,
307+
result: RenderResult,
308+
colorsMap: ColorsMap,
309+
fill?: boolean
310+
) => {
311+
result.rerender(
312+
<EuiBadge color={color} fill={fill}>
313+
Badge
314+
</EuiBadge>
315+
);
316+
317+
const colors = colorsMap[color];
318+
319+
expect(result.container.firstChild).toHaveStyleRule(
320+
'--euiBadgeBackgroundColor',
321+
colors.backgroundColor
322+
);
323+
324+
expect(result.container.firstChild).toHaveStyleRule(
325+
'--euiBadgeTextColor',
326+
colors.textColor
327+
);
328+
329+
if (Object.hasOwn(colors, 'borderColor')) {
330+
expect(result.container.firstChild).toHaveStyleRule(
331+
'border-color',
332+
colors.borderColor
333+
);
334+
} else {
335+
expect(result.container.firstChild).not.toHaveStyleRule('border-color');
336+
}
337+
};
338+
339+
describe('non fill colors', () => {
340+
let result: RenderResult;
341+
342+
beforeEach(() => {
343+
result = render(<EuiBadge>Badge</EuiBadge>);
344+
});
345+
346+
COLORS.forEach((color) => {
347+
it(`applies correct values for color "${color}"`, () => {
348+
assertColor(color, result, baseColorsMap, false);
349+
});
350+
});
351+
});
352+
353+
describe('fill colors', () => {
354+
let result: RenderResult;
355+
356+
beforeEach(() => {
357+
result = render(<EuiBadge fill>Badge</EuiBadge>);
358+
});
359+
360+
COLORS.forEach((color) => {
361+
it(`applies correct values for color "${color}"`, () => {
362+
assertColor(color, result, fillColorsMap, true);
363+
});
364+
});
365+
});
366+
367+
it('defaults to fill = false', () => {
368+
const result = render(<EuiBadge fill>Badge</EuiBadge>);
369+
370+
COLORS.forEach((color) => {
371+
assertColor(color, result, baseColorsMap);
372+
});
373+
});
374+
});
254375
});

0 commit comments

Comments
 (0)