Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export const exampleAccordionPlain = (): HTMLElement => {
const div = document.createElement('div');

div.innerHTML = `
<article class="usa-accordion" id="accEx1">
<article class="usa-accordion" id="accEx1" data-heading-selector="h3">
<section class="usa-prose">
<h3>this is section header 1 (ex. 1)</h3>
<p>this is accordion content.</p>
Expand Down Expand Up @@ -43,38 +43,38 @@ export const exampleProseless = (): HTMLElement => {
const div = document.createElement('div');

div.innerHTML = `
<article class="usa-accordion" id="accEx1">
<article class="usa-accordion" id="accEx1" data-heading-selector="h3">
<section>
<h3>this is section header 1 (ex. 1)</h3>
<p>this is accordion content.</p>
<ul>
<li>fake list for content</li>
</ul>
<h4>Header meant to try and break stuff</h4>
<p>
Cuz they do this in content sometimes. this is the real test
</p>
<h3>this is section header 2 (ex. 1)</h3>
<p>This is more content</p>
<ol>
<li>trying out some different content types</li>
</ol>
<h3>this is section header 3 (ex. 1)</h3>
<p>Even more content</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras
iaculis fringilla orci eu sagittis. Phasellus sit amet neque
vitae ex molestie condimentum sit amet sed leo.
</p>
<h3>this is section header 4 (ex. 1)</h3>
<p>Even more content</p>
<p>
Donec ullamcorper porta nibh id accumsan. Quisque non ornare
neque, et volutpat dolor. Vestibulum nisi ipsum, sagittis in
ex ac, viverra placerat massa.
</p>
</section>
</article>
<ul>
<li>fake list for content</li>
</ul>
<h4>Header meant to try and break stuff</h4>
<p>
Cuz they do this in content sometimes. this is the real test
</p>
<h3>this is section header 2 (ex. 1)</h3>
<p>This is more content</p>
<ol>
<li>trying out some different content types</li>
</ol>
<h3>this is section header 3 (ex. 1)</h3>
<p>Even more content</p>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras
iaculis fringilla orci eu sagittis. Phasellus sit amet neque
vitae ex molestie condimentum sit amet sed leo.
</p>
<h3>this is section header 4 (ex. 1)</h3>
<p>Even more content</p>
<p>
Donec ullamcorper porta nibh id accumsan. Quisque non ornare
neque, et volutpat dolor. Vestibulum nisi ipsum, sagittis in
ex ac, viverra placerat massa.
</p>
</section>
</article>
`;

return div;
Expand All @@ -84,7 +84,7 @@ export const exampleAccordionBad = (): HTMLElement => {
const div = document.createElement('div');

div.innerHTML = `
<article class="usa-accordion" id="accEx1" data-open-sections="1,2,3">
<article class="usa-accordion" id="accEx1" data-open-sections="1,2,3" data-allow-multiple data-heading-selector="h3">
<section class="usa-prose">
<h3>this is section header 1 (ex. 1)</h3>
<p>this is accordion content.</p>
Expand Down Expand Up @@ -125,7 +125,7 @@ export const exampleAccordionInitialized = (): HTMLElement => {
const div = document.createElement('div');

div.innerHTML = `
<div class="usa-accordion " data-open-sections="1">
<div class="usa-accordion" data-open-sections="1">
<h4 class="usa-accordion__heading">
<button class="usa-accordion__button" aria-controls="acc1234" type="button" aria-expanded="true">
First Amendment
Expand Down Expand Up @@ -153,3 +153,22 @@ export const exampleAccordionInitialized = (): HTMLElement => {
</div>`;
return div;
};

export const exampleAccordionNoNesting = (): HTMLElement => {
const div = document.createElement('div');

div.innerHTML = `
<div class="cgdp-article-body cgdp-article-body--multiple" data-heading-selector="h2">
<section>
<h2>First Heading</h2>
<p>Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech</p>
<h2>Inner Heading</h2>
<p>Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech</p>
</section>
<section>
<h2>Second Section Heading</h2>
<p>Congress shall make no law respecting an establishment of religion, or prohibiting the free exercise thereof; or abridging the freedom of speech</p>
</section>
</div>`;
return div;
};
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
exampleAccordionBad,
exampleAccordionInitialized,
exampleProseless,
exampleAccordionNoNesting,
} from './example-dom';

describe('USAAccordion', () => {
Expand All @@ -17,10 +18,12 @@ describe('USAAccordion', () => {
const options: AccordionOptions = {
allowMultipleOpen: false,
openSections: [1],
headerSelector: '.usa-accordion__header',
};

beforeEach(() => {
container = exampleAccordionPlain();
const wrapper = exampleAccordionPlain();
container = wrapper.querySelector('.usa-accordion') as HTMLElement;
document.body.appendChild(container);
accordion = USAAccordion.create(container, options);
});
Expand All @@ -36,19 +39,23 @@ describe('USAAccordion', () => {

it('initializes accordion behavior with existing and styled HTML elements', () => {
document.getElementsByTagName('body')[0].innerHTML = '';
const domContainer = exampleAccordionInitialized();
const wrapper = exampleAccordionInitialized();
const domContainer = wrapper.querySelector('.usa-accordion') as HTMLElement;
document.body.append(domContainer);
accordion = USAAccordion.create(domContainer, options);
expect(accordion).toBeDefined();
expect(container).not.toHaveClass('usa-accordion--multiselectable');
});

it('does not apply usa-prose unless present', async () => {
it('does not apply usa-prose unless present', () => {
document.getElementsByTagName('body')[0].innerHTML = '';
const domContainer = exampleProseless();

const wrapper = exampleProseless();
const domContainer = wrapper.querySelector('.usa-accordion') as HTMLElement;
document.body.append(domContainer);
accordion = USAAccordion.create(domContainer, options);
const content = container.querySelector(

USAAccordion.create(domContainer, options);
const content = domContainer.querySelector(
'.usa-accordion__content'
) as HTMLDivElement;
expect(content).not.toHaveClass('usa-prose');
Expand All @@ -58,6 +65,7 @@ describe('USAAccordion', () => {
const customOptions: AccordionOptions = {
allowMultipleOpen: true,
openSections: [1],
headerSelector: '.usa-accordion__header',
};
accordion = USAAccordion.create(container, customOptions);
expect(container).toHaveClass('usa-accordion--multiselectable');
Expand Down Expand Up @@ -115,16 +123,17 @@ describe('USAAccordion', () => {
const customOptions: AccordionOptions = {
allowMultipleOpen: true,
openSections: [1],
headerSelector: '.usa-accordion__header',
};
accordion = USAAccordion.create(container, customOptions);
expect(container).toHaveClass('usa-accordion--multiselectable');
accordion.unregister();
expect(container).not.toHaveClass('usa-accordion--multiselectable');
expect(
document.querySelectorAll('usa-accordion__heading').length
document.querySelectorAll('.usa-accordion__heading').length
).toBeFalsy();
expect(
document.querySelectorAll('usa-accordion__content').length
document.querySelectorAll('.usa-accordion__content').length
).toBeFalsy();
});

Expand All @@ -141,4 +150,27 @@ describe('USAAccordion', () => {
await user.click(button);
expect(controlledSection!.hidden).toBeTruthy();
});

it('does not convert inner headings into buttons', async () => {
Object.defineProperty(window, 'innerWidth', {
writable: true,
configurable: true,
value: 375,
});

const domContainer = exampleAccordionNoNesting();
document.body.append(domContainer);
const newOptions = {
...options,
headerSelector: '.cgdp-article-body__heading',
};
USAAccordion.create(domContainer, newOptions);

window.dispatchEvent(new Event('resize'));

const allHeadings = document.body.querySelectorAll('h2');
const innerHeading = allHeadings[1];

expect(innerHeading.querySelector('button')).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export type AccordionOptions = {
allowMultipleOpen: boolean;
/** specifies sections opened at init. */
openSections: Array<number>;
/** header selector for accordion buttons */
headerSelector: string;
};
Loading