From 78535f4eb70c3f485b47b1f6e3a2060780c40298 Mon Sep 17 00:00:00 2001 From: ivan Date: Sun, 23 Jun 2024 22:11:14 +0300 Subject: [PATCH] test(autocomplete): cover autocomplete component add tests autocomplete component, for destroy, selectOption and setValues methods --- tests/spec/autocomplete/autocompleteSpec.js | 86 +++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tests/spec/autocomplete/autocompleteSpec.js b/tests/spec/autocomplete/autocompleteSpec.js index 73d4931397..b56b515134 100644 --- a/tests/spec/autocomplete/autocompleteSpec.js +++ b/tests/spec/autocomplete/autocompleteSpec.js @@ -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