Skip to content

Commit e220191

Browse files
chore: fixing badge component linting (#675)
## **Description** This PR fixes linting violations in the BadgeNetwork and BadgeWrapper components in the design-system-react package by: 1. Removing ESLint ignores for these components in the config 2. Improving test assertions using `toStrictEqual` instead of `toEqual` 3. Adding proper null checks before accessing parent elements 4. Implementing proper mock functions with return values 5. Adding the unicode flag to regex for better internationalization support 6. Improving code comments and spacing ## **Related issues** Part of: #657 ## **Manual testing steps** 1. Run ESLint on the project to verify no linting errors 2. Run the tests to confirm all tests still pass 3. Verify the Badge components render correctly in Storybook ## **Screenshots/Recordings** Not applicable for linting fixes. ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable - [x] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
1 parent f29eed5 commit e220191

File tree

3 files changed

+44
-22
lines changed

3 files changed

+44
-22
lines changed

eslint.config.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ const config = createConfig([
2828
'packages/design-system-react/src/components/AvatarGroup/AvatarGroup.tsx',
2929
'packages/design-system-react/src/components/AvatarNetwork/AvatarNetwork.test.tsx',
3030
'packages/design-system-react/src/components/AvatarToken/AvatarToken.test.tsx',
31-
'packages/design-system-react/src/components/BadgeNetwork/BadgeNetwork.test.tsx',
32-
'packages/design-system-react/src/components/BadgeWrapper/BadgeWrapper.test.tsx',
3331
'packages/design-system-react/src/components/temp-components/Jazzicon/Jazzicon.test.tsx',
3432
'packages/design-system-react/src/components/temp-components/Jazzicon/Jazzicon.tsx',
3533
'packages/design-system-react/src/components/temp-components/Maskicon/Maskicon.test.tsx',

packages/design-system-react/src/components/BadgeNetwork/BadgeNetwork.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ describe('BadgeNetwork', () => {
88
render(<BadgeNetwork name="TestNet" src="icon.png" data-testid="badge" />);
99

1010
const wrapper = screen.getByTestId('badge');
11-
const classList = wrapper.className.split(/\s+/);
11+
const classList = wrapper.className.split(/\s+/u);
1212

13-
expect(classList).toEqual(
13+
expect(classList).toStrictEqual(
1414
expect.arrayContaining([
1515
'h-[18px]',
1616
'w-[18px]',

packages/design-system-react/src/components/BadgeWrapper/BadgeWrapper.test.tsx

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
BadgeWrapperPosition,
66
BadgeWrapperPositionAnchorShape,
77
} from '../../types';
8+
89
import { BadgeWrapper } from './BadgeWrapper';
910

1011
describe('BadgeWrapper', () => {
@@ -18,7 +19,10 @@ describe('BadgeWrapper', () => {
1819
bottom: 0,
1920
x: 0,
2021
y: 0,
21-
toJSON: () => {},
22+
toJSON: () => {
23+
// Empty implementation needed for mock
24+
return '';
25+
},
2226
});
2327
});
2428
afterEach(() => {
@@ -45,8 +49,11 @@ describe('BadgeWrapper', () => {
4549
<div data-testid="anchor" />
4650
</BadgeWrapper>,
4751
);
48-
const badgeDiv = screen.getByTestId('badge').parentElement!;
49-
expect(badgeDiv).toHaveStyle({ bottom: '0px', right: '0px' });
52+
const badgeEl = screen.getByTestId('badge');
53+
// Ensure the parent element exists
54+
expect(badgeEl.parentElement).toBeInTheDocument();
55+
// Apply styles check only after confirming parent exists
56+
expect(badgeEl.parentElement).toHaveStyle({ bottom: '0px', right: '0px' });
5057
});
5158

5259
it('applies BottomLeft position correctly', () => {
@@ -58,8 +65,9 @@ describe('BadgeWrapper', () => {
5865
<div data-testid="anchor" />
5966
</BadgeWrapper>,
6067
);
61-
const badgeDiv = screen.getByTestId('badge').parentElement!;
62-
expect(badgeDiv).toHaveStyle({ bottom: '0px', left: '0px' });
68+
const badgeEl = screen.getByTestId('badge');
69+
expect(badgeEl.parentElement).toBeInTheDocument();
70+
expect(badgeEl.parentElement).toHaveStyle({ bottom: '0px', left: '0px' });
6371
});
6472

6573
it('applies TopLeft position correctly', () => {
@@ -71,8 +79,9 @@ describe('BadgeWrapper', () => {
7179
<div data-testid="anchor" />
7280
</BadgeWrapper>,
7381
);
74-
const badgeDiv = screen.getByTestId('badge').parentElement!;
75-
expect(badgeDiv).toHaveStyle({ top: '0px', left: '0px' });
82+
const badgeEl = screen.getByTestId('badge');
83+
expect(badgeEl.parentElement).toBeInTheDocument();
84+
expect(badgeEl.parentElement).toHaveStyle({ top: '0px', left: '0px' });
7685
});
7786

7887
it('applies TopRight position correctly', () => {
@@ -84,8 +93,9 @@ describe('BadgeWrapper', () => {
8493
<div data-testid="anchor" />
8594
</BadgeWrapper>,
8695
);
87-
const badgeDiv = screen.getByTestId('badge').parentElement!;
88-
expect(badgeDiv).toHaveStyle({ top: '0px', right: '0px' });
96+
const badgeEl = screen.getByTestId('badge');
97+
expect(badgeEl.parentElement).toBeInTheDocument();
98+
expect(badgeEl.parentElement).toHaveStyle({ top: '0px', right: '0px' });
8999
});
90100

91101
it('respects positionXOffset and positionYOffset', () => {
@@ -98,8 +108,9 @@ describe('BadgeWrapper', () => {
98108
<div data-testid="anchor" />
99109
</BadgeWrapper>,
100110
);
101-
const badgeDiv = screen.getByTestId('badge').parentElement!;
102-
expect(badgeDiv).toHaveStyle({ bottom: '10px', right: '5px' });
111+
const badgeEl = screen.getByTestId('badge');
112+
expect(badgeEl.parentElement).toBeInTheDocument();
113+
expect(badgeEl.parentElement).toHaveStyle({ bottom: '10px', right: '5px' });
103114
});
104115

105116
it('uses Rectangular anchor shape (no extra shape offset)', () => {
@@ -113,8 +124,9 @@ describe('BadgeWrapper', () => {
113124
<div data-testid="anchor" />
114125
</BadgeWrapper>,
115126
);
116-
const badgeDiv = screen.getByTestId('badge').parentElement!;
117-
expect(badgeDiv).toHaveStyle({ bottom: '4px', right: '3px' });
127+
const badgeEl = screen.getByTestId('badge');
128+
expect(badgeEl.parentElement).toBeInTheDocument();
129+
expect(badgeEl.parentElement).toHaveStyle({ bottom: '4px', right: '3px' });
118130
});
119131

120132
it('overrides with customPosition when provided', () => {
@@ -124,8 +136,9 @@ describe('BadgeWrapper', () => {
124136
<div data-testid="anchor" />
125137
</BadgeWrapper>,
126138
);
127-
const badgeDiv = screen.getByTestId('badge').parentElement!;
128-
expect(badgeDiv).toHaveStyle({
139+
const badgeEl = screen.getByTestId('badge');
140+
expect(badgeEl.parentElement).toBeInTheDocument();
141+
expect(badgeEl.parentElement).toHaveStyle({
129142
top: '1px',
130143
right: '2px',
131144
bottom: '3px',
@@ -175,7 +188,10 @@ describe('BadgeWrapper', () => {
175188
bottom: 0,
176189
x: 0,
177190
y: 0,
178-
toJSON: () => {},
191+
toJSON: () => {
192+
// Empty implementation needed for mock
193+
return '';
194+
},
179195
};
180196
const badgeRect = {
181197
width: 20,
@@ -186,7 +202,10 @@ describe('BadgeWrapper', () => {
186202
bottom: 0,
187203
x: 0,
188204
y: 0,
189-
toJSON: () => {},
205+
toJSON: () => {
206+
// Empty implementation needed for mock
207+
return '';
208+
},
190209
};
191210
jest
192211
.spyOn(HTMLElement.prototype, 'getBoundingClientRect')
@@ -199,7 +218,12 @@ describe('BadgeWrapper', () => {
199218
</BadgeWrapper>,
200219
);
201220

202-
const badgeDiv = screen.getByTestId('badge-m').parentElement!;
221+
const badgeEl = screen.getByTestId('badge-m');
222+
expect(badgeEl.parentElement).toBeInTheDocument();
223+
// We can assert that parentElement exists since we checked it's in the document
224+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
225+
const badgeDiv = badgeEl.parentElement!;
226+
203227
// Computed offsets:
204228
// anchorShapeXOffset = 100 * .1464 = 14.64
205229
// badgeCenteringXOffset = 20/2 = 10

0 commit comments

Comments
 (0)