Skip to content

Commit dcbece0

Browse files
ethansharM-i-k-e-l
andauthored
Avoid failing rgba API (#2838)
* Avoid failing rgba API * Update src/style/__tests__/colors.spec.js Co-authored-by: Miki Leib <38354019+M-i-k-e-l@users.noreply.github.com> * Remove comment --------- Co-authored-by: Miki Leib <38354019+M-i-k-e-l@users.noreply.github.com>
1 parent 3c1a0f3 commit dcbece0

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/style/__tests__/colors.spec.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import uut from '../colors';
2+
import LogService from '../../services/LogService';
3+
24
const SYSTEM_COLORS = ['grey', 'white', 'black'];
35
const GetColorsByHexOptions = {validColors: SYSTEM_COLORS};
46

57
describe('style/Colors', () => {
8+
const logServiceSpy = jest.spyOn(LogService, 'error');
69
it('should add alpha to hex color value', () => {
710
expect(uut.rgba(uut.green30, 0.7)).toBe('rgba(0, 168, 126, 0.7)');
811
expect(uut.rgba(uut.red10, 0.7)).toBe('rgba(213, 39, 18, 0.7)');
@@ -29,7 +32,9 @@ describe('style/Colors', () => {
2932
});
3033

3134
it('should handle wrong number of params', () => {
32-
expect(() => uut.rgba(101, 136, 0.7)).toThrow(new Error('rgba can work with either 2 or 4 arguments'));
35+
expect(uut.rgba(101, 136, 0.7)).toBe(undefined);
36+
expect(uut.rgba(undefined, 0.2)).toBe(undefined);
37+
expect(logServiceSpy).toHaveBeenNthCalledWith(2, 'Colors.rgba fail due to invalid arguments');
3338
});
3439

3540
it('should handle invalid rgb code', () => {

src/style/colors.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import DesignTokensDM from './designTokensDM';
1010
import ColorName from './colorName';
1111
import Scheme, {Schemes, SchemeType} from './scheme';
1212
import type {ExtendTypeWith} from '../typings/common';
13+
import LogService from '../services/LogService';
1314

1415
export type DesignToken = {semantic?: [string]; resource_paths?: [string]; toString: Function};
1516
export type TokensOptions = {primaryColor: string};
@@ -101,9 +102,9 @@ export class Colors {
101102
* p3 - B part of RGB
102103
* p4 - opacity
103104
*/
104-
rgba(p1: string, p2: number): string;
105-
rgba(p1: number, p2: number, p3: number, p4: number): string;
106-
rgba(p1: number | string, p2: number, p3?: number, p4?: number): string {
105+
rgba(p1: string, p2: number): string | undefined;
106+
rgba(p1: number, p2: number, p3: number, p4: number): string | undefined;
107+
rgba(p1: number | string, p2: number, p3?: number, p4?: number): string | undefined {
107108
let hex;
108109
let opacity;
109110
let red;
@@ -129,7 +130,8 @@ export class Colors {
129130
blue = validateRGB(p3!);
130131
opacity = p4;
131132
} else {
132-
throw new Error('rgba can work with either 2 or 4 arguments');
133+
LogService.error('Colors.rgba fail due to invalid arguments');
134+
return undefined;
133135
}
134136
return `rgba(${red}, ${green}, ${blue}, ${opacity})`;
135137
}

0 commit comments

Comments
 (0)