Skip to content

Commit

Permalink
DOP-5059: Default expanded Collapsibles (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeigs authored Oct 9, 2024
1 parent 82c77ec commit 7c08998
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/components/Collapsible/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { collapsibleStyle, headerContainerStyle, headerStyle, iconStyle, innerCo
import './styles.css';

const Collapsible = ({ nodeData: { children, options }, sectionDepth, ...rest }) => {
const { id, heading, sub_heading: subHeading } = options;
const { id, heading, expanded, sub_heading: subHeading } = options;
const { hash } = useLocation();

// get a list of all ids in collapsible content
Expand All @@ -25,7 +25,7 @@ const Collapsible = ({ nodeData: { children, options }, sectionDepth, ...rest })
return findAllNestedAttribute(children, 'id');
}, [children]);

const [open, setOpen] = useState(false);
const [open, setOpen] = useState(!!expanded);
const headingNodeData = {
id,
children: [{ type: 'text', value: heading }],
Expand Down
9 changes: 9 additions & 0 deletions tests/unit/Collapsible.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { render, act } from '@testing-library/react';
import { mockLocation } from '../utils/mock-location';
import Collapsible from '../../src/components/Collapsible';
import mockData from './data/Collapsible.test.json';
import expandedMockData from './data/CollapsibleExpanded.test.json';

describe('collapsible component', () => {
it('renders all the content in the options and children', () => {
Expand Down Expand Up @@ -42,4 +43,12 @@ describe('collapsible component', () => {
expect(icon.getAttribute('aria-label')).toContain('Chevron');
expect(icon.getAttribute('aria-label')).toContain('Down');
});

it('is default expanded if expanded option is truthy', async () => {
const renderResult = render(<Collapsible nodeData={expandedMockData} />),
button = renderResult.getByRole('button'),
icon = button.querySelector('[role=img]');
expect(icon.getAttribute('aria-label')).toContain('Chevron');
expect(icon.getAttribute('aria-label')).toContain('Down');
});
});
59 changes: 59 additions & 0 deletions tests/unit/data/CollapsibleExpanded.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"type": "directive",
"position": {
"start": {
"line": {
"$numberInt": "13"
}
}
},
"children": [
{
"type": "paragraph",
"position": {
"start": {
"line": {
"$numberInt": "17"
}
}
},
"children": [
{
"type": "text",
"position": {
"start": {
"line": {
"$numberInt": "17"
}
}
},
"value": "This is collapsible content"
}
]
},
{
"type": "code",
"position": {
"start": {
"line": {
"$numberInt": "19"
}
}
},
"lang": "javascript",
"copyable": true,
"emphasize_lines": [],
"value": "This is code within collapsible content",
"linenos": false
}
],
"domain": "mongodb",
"name": "collapsible",
"argument": [],
"options": {
"heading": "This is a heading",
"sub_heading": "This is a subheading",
"expanded": true,
"id": "this-is-a-heading"
}
}

0 comments on commit 7c08998

Please sign in to comment.