Skip to content

Commit

Permalink
fix(list): Fixed unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abhiomkar committed Dec 7, 2018
1 parent a7e8edd commit 5c93287
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 44 deletions.
30 changes: 19 additions & 11 deletions test/unit/mdc-list/foundation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ test('exports cssClasses', () => {

test('defaultAdapter returns a complete adapter implementation', () => {
verifyDefaultAdapter(MDCListFoundation, [
'getListItemCount', 'getFocusedElementIndex', 'getAttributeForElementIndex', 'setAttributeForElementIndex',
'getListItemCount', 'getFocusedElementIndex', 'setAttributeForElementIndex',
'removeAttributeForElementIndex', 'addClassForElementIndex', 'removeClassForElementIndex',
'focusItemAtIndex', 'setTabIndexForListItemChildren', 'followHref', 'hasRadioAtIndex',
'focusItemAtIndex', 'setTabIndexForListItemChildren', 'hasRadioAtIndex',
'hasCheckboxAtIndex', 'isCheckboxCheckedAtIndex', 'setCheckedCheckboxOrRadioAtIndex', 'notifyAction',
]);
});
Expand Down Expand Up @@ -425,26 +425,34 @@ test('#handleKeydown space/enter key does not cause event.preventDefault when si
td.verify(preventDefault(), {times: 0});
});

test('#handleKeydown space/enter key call adapter.followHref regardless of singleSelection', () => {
test('#handleKeydown space key calls notifyAction for anchor element regardless of singleSelection', () => {
const {foundation, mockAdapter} = setupTest();
const target = {classList: ['mdc-list-item']};
const event = {key: 'Enter', target, preventDefault: () => {}};
const target = {tagName: 'A', classList: ['mdc-list-item']};
const event = {key: 'Space', target, preventDefault: () => {}};

td.when(mockAdapter.getFocusedElementIndex()).thenReturn(0);
td.when(mockAdapter.getListItemCount()).thenReturn(3);
td.when(mockAdapter.hasRadioAtIndex(0)).thenReturn(false);
td.when(mockAdapter.hasCheckboxAtIndex(0)).thenReturn(false);
td.when(mockAdapter.getAttributeForElementIndex(0, 'href')).thenReturn('http://test.url');
foundation.setSingleSelection(false);
foundation.handleKeydown(event, true, 0);
foundation.setSingleSelection(true);
foundation.handleKeydown(event, true, 0);
event.key = 'Space';
foundation.handleKeydown(event, true, 0);

td.verify(mockAdapter.notifyAction(0), {times: 2});
});

test('#handleKeydown enter key does not call notifyAction for anchor element', () => {
const {foundation, mockAdapter} = setupTest();
const target = {tagName: 'A', classList: ['mdc-list-item']};
const event = {key: 'Enter', target, preventDefault: () => {}};

td.when(mockAdapter.getFocusedElementIndex()).thenReturn(0);
td.when(mockAdapter.getListItemCount()).thenReturn(3);
foundation.setSingleSelection(false);
foundation.handleKeydown(event, true, 0);
foundation.setSingleSelection(true);
foundation.handleKeydown(event, true, 0);

td.verify(mockAdapter.followHref(0), {times: 4});
td.verify(mockAdapter.notifyAction(0), {times: 0}); // notifyAction will be called by handleClick event.
});

test('#handleKeydown space key does not cause preventDefault to be called if singleSelection=false', () => {
Expand Down
33 changes: 0 additions & 33 deletions test/unit/mdc-list/mdc-list.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,6 @@ test('adapter#getFocusedElementIndex returns the index of the currently selected
document.body.removeChild(root);
});

test('adapter#getAttributeForElementIndex returns the attribute value of element index', () => {
const {root, component} = setupTest();
document.body.appendChild(root);
const targetNode = root.querySelectorAll('.mdc-list-item')[1];
const testUrl = 'http://test.url';
targetNode.setAttribute('href', testUrl);
assert.equal(testUrl, component.getDefaultFoundation().adapter_.getAttributeForElementIndex(1, 'href'));
document.body.removeChild(root);
});

test('adapter#setAttributeForElementIndex does nothing if the element at index does not exist', () => {
const {root, component} = setupTest();
document.body.appendChild(root);
Expand Down Expand Up @@ -245,29 +235,6 @@ test('adapter#setTabIndexForListItemChildren sets the child button/a elements of
document.body.removeChild(root);
});

test('adapter#followHref invokes click on element with href', () => {
const {root, component} = setupTest();
const anchorTag = document.createElement('a');
anchorTag.href = '#';
anchorTag.click = td.func('click');
anchorTag.classList.add('mdc-list-item');
root.appendChild(anchorTag);
component.getDefaultFoundation().adapter_.followHref(root.querySelectorAll('.mdc-list-item').length - 1);

td.verify(anchorTag.click(), {times: 1});
});

test('adapter#followHref does not invoke click on element without href', () => {
const {root, component} = setupTest();
const anchorTag = document.createElement('a');
anchorTag.click = td.func('click');
anchorTag.classList.add('mdc-list-item');
root.appendChild(anchorTag);
component.getDefaultFoundation().adapter_.followHref(root.querySelectorAll('.mdc-list-item').length - 1);

td.verify(anchorTag.click(), {times: 0});
});

test('layout adds tabindex=-1 to all list items without a tabindex', () => {
const {root} = setupTest();
assert.equal(0, root.querySelectorAll('.mdc-list-item:not([tabindex])').length);
Expand Down

0 comments on commit 5c93287

Please sign in to comment.