Skip to content
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

[10-10CG] Add aria-live div to Show More Facilities functionality #33786

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -19,6 +19,7 @@
const [searchInputError, setSearchInputError] = useState(null);
const [facilitiesListError, setFacilitiesListError] = useState(null);
const [facilities, setFacilities] = useState([]);
const [newFacilitiesCount, setNewFacilitiesCount] = useState(0);
const [pagination, setPagination] = useState({
currentPage: 0,
totalEntries: 0,
Expand All @@ -35,6 +36,14 @@
return facilities?.length < pagination.totalEntries;
};

const ariaLiveMessage = () => {
if (newFacilitiesCount > 0) {
const facilitySuffix = newFacilitiesCount === 1 ? 'y' : 'ies';
return `${newFacilitiesCount} new facilit${facilitySuffix} loaded`;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added helper to return either 1 new facility loaded, x new facilities loaded or it returns an empty string.

}
return '';
};

const isReviewPage = () => {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get('review') === 'true';
Expand Down Expand Up @@ -198,6 +207,7 @@

const showMoreFacilities = async e => {
e.preventDefault();
setNewFacilitiesCount(0);
setLoadingMoreFacilities(true);
const facilitiesResponse = await fetchFacilities({
...coordinates,
Expand All @@ -213,6 +223,7 @@
}

setFacilities([...facilities, ...facilitiesResponse.facilities]);
setNewFacilitiesCount(facilitiesResponse.facilities.length);
setPagination(facilitiesResponse.meta.pagination);
setSubmittedQuery(query);
setLoadingMoreFacilities(false);
Expand All @@ -236,9 +247,16 @@
return (
<>
<FacilityList {...facilityListProps} />
<div
aria-live="polite"
role="status"
className="vads-u-visibility--screen-reader"
>
{ariaLiveMessage()}
</div>
{loadingMoreFacilities && loader()}
{hasMoreFacilities() && (
<button

Check warning on line 259 in src/applications/caregivers/components/FormFields/FacilitySearch.jsx

View workflow job for this annotation

GitHub Actions / Linting (Files Changed)

src/applications/caregivers/components/FormFields/FacilitySearch.jsx:259:13:The <va-button> Web Component should be used instead of the button HTML element.

Check warning on line 259 in src/applications/caregivers/components/FormFields/FacilitySearch.jsx

View workflow job for this annotation

GitHub Actions / Linting (Files Changed)

src/applications/caregivers/components/FormFields/FacilitySearch.jsx:259:13:The <va-button> Web Component should be used instead of the button HTML element.
type="button"
className="va-button-link"
onClick={showMoreFacilities}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('CG <FacilitySearch>', () => {
radioList: container.querySelector('va-radio'),
searchInputError: queryByRole('alert'),
moreFacilities: queryByText('Load more facilities'),
ariaLiveStatus: queryByRole('status'),
formNavButtons: {
back: getByText('Back'),
forward: getByText('Continue'),
Expand All @@ -100,6 +101,7 @@ describe('CG <FacilitySearch>', () => {
expect(selectors().input).to.exist;
expect(selectors().radioList).not.to.exist;
expect(selectors().moreFacilities).not.to.exist;
expect(selectors().ariaLiveStatus).not.to.exist;
expect(queryByText(content['form-facilities-search-label'])).to.exist;
expect(queryByText('(*Required)')).to.exist;
});
Expand Down Expand Up @@ -138,6 +140,7 @@ describe('CG <FacilitySearch>', () => {
await waitFor(() => {
expect(selectors().radioList).to.exist;
expect(selectors().loader).to.not.exist;
expect(selectors().ariaLiveStatus.textContent).to.eq('');
expect(selectors().input).to.not.have.attr('error');
sinon.assert.calledWith(mapboxStub, 'Tampa');
sinon.assert.calledWith(facilitiesStub, {
Expand Down Expand Up @@ -382,7 +385,7 @@ describe('CG <FacilitySearch>', () => {
});

context('more facilities buttons', () => {
it('successfully loads more facilities on click', async () => {
it('successfully loads 1 more facility on click', async () => {
const { props, mockStore } = getData({});
const { selectors, getByText, container } = subject({
props,
Expand All @@ -405,19 +408,83 @@ describe('CG <FacilitySearch>', () => {
expect(selectors().loader).to.not.exist;
expect(selectors().input).to.not.have.attr('error');
expect(getByText(/Showing 1-1 of 1 facilities for/)).to.exist;
expect(selectors().ariaLiveStatus.textContent).to.eq('');
});

await waitFor(() => {
userEvent.click(selectors().moreFacilities);
expect(selectors().radioList).to.exist;
expect(selectors().loader).to.exist;
expect(selectors().ariaLiveStatus.textContent).to.eq('');
});

await waitFor(() => {
expect(selectors().radioList).to.exist;
expect(selectors().loader).to.not.exist;
expect(getByText(/Showing 1-2 of 2 facilities for/)).to.exist;
expect(selectors().input).to.not.have.attr('error');
expect(selectors().ariaLiveStatus.textContent).to.eq(
'1 new facility loaded',
);
});

await waitFor(() => {
sinon.assert.calledWith(mapboxStub, 'Tampa');
sinon.assert.calledWith(facilitiesStub, {
lat,
long,
perPage,
radius,
page: 2,
});
expect(mapboxStub.callCount).to.equal(1);
// This is 3 because there is an existing bug that using submit with the va-search-input component triggers two requests
// https://github.com/department-of-veterans-affairs/vets-design-system-documentation/issues/3117
expect(facilitiesStub.callCount).to.equal(3);
});
});

it('successfully loads 2 more facilities on click', async () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up somewhat duplicating the previous test for the different facility message texts that can show up. I started a refactor to make a reusable helper since a lot of the Show More Facilities tests are somewhat similar, but I wasn't liking how it was turning out so I kept it more verbose like this. I'm open to looking further into this if someone really thinks we should, but also fine to leave it as is.

const { props, mockStore } = getData({});
const { selectors, getByText, container } = subject({
props,
mockStore,
});

mapboxStub.resolves(mapBoxSuccessResponse);
facilitiesStub
.onFirstCall()
.resolves(mockFetchChildFacilityWithCaregiverSupportResponse);
facilitiesStub.onSecondCall().resolves(mockFetchFacilitiesResponse);

await waitFor(() => {
inputVaSearchInput(container, 'Tampa', selectors().input);
expect(selectors().loader).to.exist;
});

await waitFor(() => {
expect(selectors().radioList).to.exist;
expect(selectors().loader).to.not.exist;
expect(selectors().input).to.not.have.attr('error');
expect(getByText(/Showing 1-1 of 1 facilities for/)).to.exist;
expect(selectors().ariaLiveStatus.textContent).to.eq('');
});

await waitFor(() => {
userEvent.click(selectors().moreFacilities);
expect(selectors().radioList).to.exist;
expect(selectors().loader).to.exist;
expect(selectors().ariaLiveStatus.textContent).to.eq('');
});

await waitFor(() => {
expect(selectors().radioList).to.exist;
expect(selectors().loader).to.not.exist;
expect(getByText(/Showing 1-3 of 3 facilities for/)).to.exist;
expect(selectors().input).to.not.have.attr('error');
expect(selectors().ariaLiveStatus.textContent).to.eq(
'2 new facilities loaded',
);
});

await waitFor(() => {
Expand Down
Loading