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

test(autocomplete): cover autocomplete component #487

Merged
Merged
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
86 changes: 86 additions & 0 deletions tests/spec/autocomplete/autocompleteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,94 @@ describe('Autocomplete Plugin', function () {
});
});
});

it('destroy method should properly dispose autocomplete component', function () {
const normal = document.querySelector('#normal-autocomplete');
const limited = document.querySelector('#limited-autocomplete');

expect(normal.parentNode.querySelector('.autocomplete-content')).not.toBeNull();
expect(limited.parentNode.querySelector('.autocomplete-content')).not.toBeNull();

const normalInstance = M.Autocomplete.getInstance(normal);
const limitedInstance = M.Autocomplete.getInstance(limited);
normalInstance.destroy();
limitedInstance.destroy();

expect(normal.parentNode.querySelector('.autocomplete-content')).toBeNull();
expect(limited.parentNode.querySelector('.autocomplete-content')).toBeNull();
});

it('selectOption method should chose only from showed dropdown', function (done) {
const normal = document.querySelector('#normal-autocomplete');
const autocompleteInstance = resetAutocomplete(normal, [
{ id: 'Value Q1' },
{ id: 'Value Q' },
{ id: 'Value R' }]);
const autocompleteEl = normal.parentNode.querySelector('.autocomplete-content');

focus(normal);
normal.value = 'Q';
keyup(normal, 81);

setTimeout(function () {
expect(autocompleteEl.children.length).toBe(2);
const dropdownAutocompleteIds = Array
.from(autocompleteEl.querySelectorAll('li'))
.map(liElement => liElement.getAttribute('data-id'));
expect(dropdownAutocompleteIds).toEqual(['Value Q1', 'Value Q']);

autocompleteInstance.selectOption('Value R');
expect(normal.value)
.withContext('Only options from dropdown can be selected through selectOption')
.toBe('Q');
autocompleteInstance.selectOption('Value Q');
expect(normal.value)
.withContext('Only options from dropdown can be selected through selectOption')
.toBe('Value Q');
done();
}, 200);
});

it('setValues method should chose from any init data entry', function (done) {
const normal = document.querySelector('#normal-autocomplete');
const autocompleteInstance = resetAutocomplete(normal, [
{ id: 'Value Q1' },
{ id: 'Value Q' },
{ id: 'Value R' }]);
const autocompleteEl = normal.parentNode.querySelector('.autocomplete-content');

focus(normal);
normal.value = 'Q';
keyup(normal, 81);

setTimeout(function () {
expect(autocompleteEl.children.length).toBe(2);
const dropdownAutocompleteIds = Array
.from(autocompleteEl.querySelectorAll('li'))
.map(liElement => liElement.getAttribute('data-id'));
expect(dropdownAutocompleteIds).toEqual(['Value Q1', 'Value Q']);

autocompleteInstance.setValues([{ id: 'Value R' }]);
expect(normal.value)
.withContext('Any option from init data can be selected through setValues')
.toBe('Value R');
autocompleteInstance.setValues([{ id: 'Value Q' }]);
expect(normal.value)
.withContext('Any option from init data can be selected through setValues')
.toBe('Value Q');
done();
}, 200);
});
});

function resetAutocomplete(autocompleteElement, data) {
M.Autocomplete.getInstance(autocompleteElement).destroy();
return M.Autocomplete.init(autocompleteElement, {
data: data,
minLength: 0
});
}

function openDropdownAndSelectFirstOption(autocomplete, onFinish) {
click(autocomplete);
keyup(autocomplete, 9); // works
Expand Down
Loading