Skip to content

Commit

Permalink
add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
precious-onyenaucheya-ons committed Nov 9, 2023
1 parent ac34793 commit 50abb7c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
54 changes: 52 additions & 2 deletions src/components/header/_macro.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ const EXAMPLE_HEADER_NAVIGATION_WITH_SITESEARCHAUTOSUGGEST = {
resultsTitleId: 'country-of-birth-suggestions',
noResults: 'No suggestions found.',
typeMore: 'Continue entering to get suggestions',
autosuggestData: '/test/fake/api/countries',
language: 'en-gb',
},
};

Expand Down Expand Up @@ -791,12 +791,13 @@ describe('mode: with site search autosuggest', () => {

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',
autosuggestData: '/test/fake/api/countries',
containerClasses: 'ons-autosuggest--header',
id: 'ons-site-search',
input: {
Expand All @@ -816,4 +817,53 @@ describe('mode: with site search autosuggest', () => {
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(1);
done();
}, 20);
});

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

setTimeout(() => {
const suggestionCount = $('.ons-autosuggest__option').length;
expect(suggestionCount).toBe(1);
done();
}, 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');
});
});
});
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

0 comments on commit 50abb7c

Please sign in to comment.