Skip to content

Avoid failing rgba API #2838

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

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/style/__tests__/colors.spec.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import uut from '../colors';
import LogService from '../../services/LogService';

const SYSTEM_COLORS = ['grey', 'white', 'black'];
const GetColorsByHexOptions = {validColors: SYSTEM_COLORS};

describe('style/Colors', () => {
const logServiceSpy = jest.spyOn(LogService, 'error');
// jest.mock('../../services/LogService');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can be deleted

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

true

it('should add alpha to hex color value', () => {
expect(uut.rgba(uut.green30, 0.7)).toBe('rgba(0, 168, 126, 0.7)');
expect(uut.rgba(uut.red10, 0.7)).toBe('rgba(213, 39, 18, 0.7)');
Expand All @@ -29,7 +33,10 @@ describe('style/Colors', () => {
});

it('should handle wrong number of params', () => {
expect(() => uut.rgba(101, 136, 0.7)).toThrow(new Error('rgba can work with either 2 or 4 arguments'));
expect(uut.rgba(101, 136, 0.7)).toBe(undefined);
expect(logServiceSpy).toHaveBeenCalledWith('Colors.rgba fail due to invalid arguments');
expect(uut.rgba(undefined, 0.2)).toBe(undefined);
expect(logServiceSpy).toHaveBeenCalledWith('Colors.rgba fail due to invalid arguments');
});

it('should handle invalid rgb code', () => {
Expand Down
10 changes: 6 additions & 4 deletions src/style/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import DesignTokensDM from './designTokensDM';
import ColorName from './colorName';
import Scheme, {Schemes, SchemeType} from './scheme';
import type {ExtendTypeWith} from '../typings/common';
import LogService from '../services/LogService';

export type DesignToken = {semantic?: [string]; resource_paths?: [string]; toString: Function};
export type TokensOptions = {primaryColor: string};
Expand Down Expand Up @@ -101,9 +102,9 @@ export class Colors {
* p3 - B part of RGB
* p4 - opacity
*/
rgba(p1: string, p2: number): string;
rgba(p1: number, p2: number, p3: number, p4: number): string;
rgba(p1: number | string, p2: number, p3?: number, p4?: number): string {
rgba(p1: string, p2: number): string | undefined;
rgba(p1: number, p2: number, p3: number, p4: number): string | undefined;
rgba(p1: number | string, p2: number, p3?: number, p4?: number): string | undefined {
let hex;
let opacity;
let red;
Expand All @@ -129,7 +130,8 @@ export class Colors {
blue = validateRGB(p3!);
opacity = p4;
} else {
throw new Error('rgba can work with either 2 or 4 arguments');
LogService.error('Colors.rgba fail due to invalid arguments');
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it should be failed (also in tests)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What do you mean should be failed?
LogService.error doesn't throw an error

return undefined;
}
return `rgba(${red}, ${green}, ${blue}, ${opacity})`;
}
Expand Down