Skip to content

Commit 79c2a50

Browse files
committed
cleaned up redundant code and added tests
1 parent 904f9cb commit 79c2a50

File tree

2 files changed

+49
-25
lines changed

2 files changed

+49
-25
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React from 'react';
2+
import { render, screen, userEvent } from '@mongodb-js/testing-library-compass';
3+
import { expect } from 'chai';
4+
import CreateIndexOptionsAccordion from './create-index-options-accordion';
5+
6+
describe('CreateIndexOptionsAccordion', () => {
7+
it('renders the button with "Options" text', () => {
8+
render(
9+
<CreateIndexOptionsAccordion>Test Content</CreateIndexOptionsAccordion>
10+
);
11+
expect(screen.getByText('Options')).to.exist;
12+
});
13+
14+
it('toggles the accordion content when the button is clicked', () => {
15+
render(
16+
<CreateIndexOptionsAccordion>Test Content</CreateIndexOptionsAccordion>
17+
);
18+
19+
const button = screen.getByRole('button', { name: /options/i });
20+
expect(screen.queryByText('Test Content')).to.not.exist;
21+
22+
userEvent.click(button);
23+
expect(screen.getByText('Test Content')).to.exist;
24+
25+
userEvent.click(button);
26+
expect(screen.queryByText('Test Content')).to.not.exist;
27+
});
28+
29+
it('renders the correct icon based on the open state', () => {
30+
render(
31+
<CreateIndexOptionsAccordion>Test Content</CreateIndexOptionsAccordion>
32+
);
33+
34+
const button = screen.getByRole('button', { name: /options/i });
35+
expect(
36+
screen.getByTestId('create-index-options-accordion-icon')
37+
).to.have.attribute('aria-label', 'Chevron Right Icon');
38+
39+
userEvent.click(button);
40+
expect(
41+
screen.getByTestId('create-index-options-accordion-icon')
42+
).to.have.attribute('aria-label', 'Chevron Down Icon');
43+
});
44+
});

packages/compass-indexes/src/components/create-index-form/create-index-options-accordion.tsx

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
import {
22
Body,
33
css,
4-
cx,
54
Icon,
65
palette,
76
useDarkMode,
87
} from '@mongodb-js/compass-components';
98
import React, { useState } from 'react';
109

11-
const buttonLightThemeStyles = css({
12-
color: palette.gray.dark2,
13-
});
14-
const buttonDarkThemeStyles = css({
15-
color: palette.white,
16-
});
17-
1810
const buttonStyles = css({
1911
fontWeight: 'bold',
2012
display: 'flex',
@@ -29,18 +21,10 @@ const buttonStyles = css({
2921
},
3022
});
3123

32-
const buttonIconContainerStyles = css({
24+
const iconStyles = css({
3325
marginLeft: '4px',
3426
});
3527

36-
const iconDarkThemeStyles = css({
37-
color: palette.gray.base,
38-
});
39-
40-
const iconLightThemeStyles = css({
41-
color: palette.gray.dark1,
42-
});
43-
4428
const CreateIndexOptionsAccordion = ({
4529
children,
4630
}: {
@@ -56,10 +40,7 @@ const CreateIndexOptionsAccordion = ({
5640
return (
5741
<>
5842
<button
59-
className={cx(
60-
darkMode ? buttonDarkThemeStyles : buttonLightThemeStyles,
61-
buttonStyles
62-
)}
43+
className={buttonStyles}
6344
type="button"
6445
aria-expanded={open ? 'true' : 'false'}
6546
onClick={onOpenChange}
@@ -68,11 +49,10 @@ const CreateIndexOptionsAccordion = ({
6849
Options
6950
</Body>
7051
<Icon
71-
className={
72-
(cx(darkMode ? iconDarkThemeStyles : iconLightThemeStyles),
73-
buttonIconContainerStyles)
74-
}
52+
color={darkMode ? palette.gray.base : palette.gray.dark1}
53+
className={iconStyles}
7554
glyph={open ? 'ChevronDown' : 'ChevronRight'}
55+
data-testid="create-index-options-accordion-icon"
7656
/>
7757
</button>
7858

0 commit comments

Comments
 (0)