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

Feature/add tests for autosuggest component #2893

Merged
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 128 additions & 5 deletions src/components/header/_macro.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,45 @@ const EXAMPLE_HEADER_NAVIGATION_WITH_SUBNAVIGATION_CONFIG = {
},
};

const EXAMPLE_HEADER_NAVIGATION_WITH_SITESEARCHAUTOSUGGEST = {
navigation: {
id: 'main-nav',
ariaLabel: 'Main menu',
currentPath: '#home',
itemsList: [
{
title: 'Home',
url: '#home',
},
{
title: 'Guidance',
url: '#0',
},
],
toggleNavigationButton: {
text: 'Menu',
ariaLabel: 'Toggle main menu',
},
},
siteSearchAutosuggest: {
label: 'label',
instructions: 'Use up and down keys to navigate.',
ariaYouHaveSelected: 'You have selected',
ariaMinChars: 'Enter 3 or more characters for suggestions.',
minChars: 3,
ariaResultsLabel: 'Country suggestions',
ariaOneResult: 'There is one suggestion available.',
ariaNResults: 'There are {n} suggestions available.',
ariaLimitedResults: 'Type more characters to improve your search',
moreResults: 'Continue entering to improve suggestions',
resultsTitle: 'Suggestions',
resultsTitleId: 'country-of-birth-suggestions',
noResults: 'No suggestions found.',
typeMore: 'Continue entering to get suggestions',
language: 'en-gb',
},
};

describe('macro: header', () => {
describe('mode: basic', () => {
it('passes jest-axe checks', async () => {
Expand Down Expand Up @@ -453,21 +492,21 @@ describe('macro: header', () => {
it('has the text for each list item', () => {
const $ = cheerio.load(renderComponent('header', EXAMPLE_HEADER_SERVICE_LIST_ITEMS));

const values = mapAll($('.ons-header-service-nav--main .ons-header-service-nav__item'), node => node.text().trim());
const values = mapAll($('.ons-header-service-nav--main .ons-header-service-nav__item'), (node) => node.text().trim());
expect(values).toEqual(['Title 1', 'Title 2', 'Title 3']);
});

it('has the link text for each list item', () => {
const $ = cheerio.load(renderComponent('header', EXAMPLE_HEADER_SERVICE_LINKS_MULTIPLE));

const values = mapAll($('.ons-header-service-nav--main .ons-header-service-nav__link'), node => node.text().trim());
const values = mapAll($('.ons-header-service-nav--main .ons-header-service-nav__link'), (node) => node.text().trim());
expect(values).toEqual(['Title 1', 'Title 2', 'Title 3']);
});

it('has the link href for each list item', () => {
const $ = cheerio.load(renderComponent('header', EXAMPLE_HEADER_SERVICE_LINKS_MULTIPLE));

const values = mapAll($('.ons-header-service-nav--main .ons-header-service-nav__link'), node => node.attr('href'));
const values = mapAll($('.ons-header-service-nav--main .ons-header-service-nav__link'), (node) => node.attr('href'));
expect(values).toEqual(['#1', '#2', '#3']);
});

Expand All @@ -486,14 +525,14 @@ describe('macro: header', () => {
it('has the link text for each list item for mobile', () => {
const $ = cheerio.load(renderComponent('header', EXAMPLE_HEADER_SERVICE_LINKS_MULTIPLE));

const values = mapAll($('.ons-header-service-nav--mobile .ons-header-service-nav__link'), node => node.text().trim());
const values = mapAll($('.ons-header-service-nav--mobile .ons-header-service-nav__link'), (node) => node.text().trim());
expect(values).toEqual(['Title 1', 'Title 2', 'Title 3']);
});

it('has the link href for each list item for mobile', () => {
const $ = cheerio.load(renderComponent('header', EXAMPLE_HEADER_SERVICE_LINKS_MULTIPLE));

const values = mapAll($('.ons-header-service-nav--mobile .ons-header-service-nav__link'), node => node.attr('href'));
const values = mapAll($('.ons-header-service-nav--mobile .ons-header-service-nav__link'), (node) => node.attr('href'));
expect(values).toEqual(['#1', '#2', '#3']);
});

Expand Down Expand Up @@ -742,3 +781,87 @@ describe('macro: header', () => {
});
});
});

describe('mode: with site search autosuggest', () => {
it('renders the search with expected parameters', () => {
const faker = templateFaker();
const buttonSpy = faker.spy('autosuggest');

faker.renderComponent('header', EXAMPLE_HEADER_NAVIGATION_WITH_SITESEARCHAUTOSUGGEST);

expect(buttonSpy.occurrences[0]).toEqual({
ariaLimitedResults: 'Type more characters to improve your search',
minChars: 3,
language: 'en-gb',
ariaMinChars: 'Enter 3 or more characters for suggestions.',
ariaNResults: 'There are {n} suggestions available.',
ariaOneResult: 'There is one suggestion available.',
ariaResultsLabel: 'Country suggestions',
ariaYouHaveSelected: 'You have selected',
containerClasses: 'ons-autosuggest--header',
id: 'ons-site-search',
input: {
accessiblePlaceholder: true,
autocomplete: 'off',
classes: 'ons-input-search ons-input-search--icon',
label: {
classes: 'ons-u-pl-m ons-label--white',
id: 'ons-site-search-label',
text: 'label',
},
},
instructions: 'Use up and down keys to navigate.',
moreResults: 'Continue entering to improve suggestions',
noResults: 'No suggestions found.',
resultsTitle: 'Suggestions',
typeMore: 'Continue entering to get suggestions',
});
});

describe('when the user inputs text', () => {
let $; // Initialize a Cheerio instance

const mockData = [
{ en: 'England' },
{ en: 'Wales' },
{ en: 'Scotland' },
{ en: 'United States of America' },
{ en: 'United States Virgin Islands' },
{ en: 'Åland Islands' },
];

beforeEach(() => {
$ = cheerio.load(
renderComponent('header', {
...EXAMPLE_HEADER_NAVIGATION_WITH_SITESEARCHAUTOSUGGEST,
autosuggestData: mockData,
}),
);
});

it('does not show suggestions when input length < minimum characters', () => {
$('.ons-js-autosuggest-input').val('En');

setTimeout(() => {
const suggestionCount = $('.ons-autosuggest__option').length;
expect(suggestionCount).toBe(0);
}, 20);
});

it('shows suggestions when input length >= minimum characters', () => {
$('.ons-js-autosuggest-input').val('Eng');

setTimeout(() => {
const suggestionCount = $('.ons-autosuggest__option').length;
expect(suggestionCount).toBe(1);
}, 20);
});

it('gets the language if set', () => {
$('.ons-js-autosuggest-input').val('Eng');

const autosuggestElement = $('.ons-js-autosuggest').attr('data-lang');
expect(autosuggestElement).toBe('en-gb');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
"fullWidth": true
---
{% from "components/header/_macro.njk" import onsHeader %}

{{
onsHeader({
"title": 'Design system',
"titleUrl": '#0',
"navigation": {
"id": 'main-nav',
"ariaLabel": 'Main menu',
"currentPath": '#home',
"itemsList": [
{
"text": 'Home',
"url": '#home'
},
{
"text": 'Guidance',
"url": '#0'
},
{
"text": 'Foundations',
"url": '#0'
},
{
"text": 'Components',
"url": '#0'
},
{
"text": 'Patterns',
"url": '#0'
}
],
"toggleNavigationButton": {
"text": 'Menu',
"ariaLabel": 'Toggle main menu'
}
},
'siteSearchAutosuggest': {
"autosuggestData": "/examples/data/country-of-birth.json",
"label": "search",
"instructions": "instructions",
"ariaYouHaveSelected": "ariaYouHaveSelected",
"ariaMinChars": "ariaMinChars",
"ariaResultsLabel": "ariaResultsLabel",
"ariaOneResult": "ariaOneResult",
"ariaNResults": "ariaNResults",
"ariaLimitedResults": "ariaLimitedResults",
"moreResults": "moreResults",
"resultsTitle": "resultsTitle",
"noResults": "noResults",
"typeMore": "typeMore"
}
})
}}
4 changes: 3 additions & 1 deletion src/components/navigation/_macro.njk
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
},
"instructions": params.siteSearchAutosuggest.instructions,
"ariaYouHaveSelected":params.siteSearchAutosuggest.ariaYouHaveSelected,
"minChars": params.siteSearchAutosuggest.minChars,
"ariaMinChars": params.siteSearchAutosuggest.ariaMinChars,
"ariaResultsLabel": params.siteSearchAutosuggest.ariaResultsLabel,
"ariaOneResult": params.siteSearchAutosuggest.ariaOneResult,
Expand All @@ -36,7 +37,8 @@
"resultsTitle": params.siteSearchAutosuggest.resultsTitle,
"autosuggestData": params.siteSearchAutosuggest.autosuggestData,
"noResults": params.siteSearchAutosuggest.noResults,
"typeMore": params.siteSearchAutosuggest.typeMore
"typeMore": params.siteSearchAutosuggest.typeMore,
"language": params.siteSearchAutosuggest.language
}) }}
</div>
{% endif %}
Expand Down
Loading