|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2020 Google Inc. |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +import {verifyDefaultAdapter} from '../../../../testing/helpers/foundation'; |
| 25 | +import {setUpFoundationTest} from '../../../../testing/helpers/setup'; |
| 26 | +import {MDCSelectHelperTextFoundation} from '../foundation'; |
| 27 | + |
| 28 | +const {cssClasses, strings} = MDCSelectHelperTextFoundation; |
| 29 | + |
| 30 | +describe('MDCSelectHelperTextFoundation', () => { |
| 31 | + it('exports cssClasses', () => { |
| 32 | + expect(MDCSelectHelperTextFoundation.cssClasses).toEqual(cssClasses); |
| 33 | + }); |
| 34 | + |
| 35 | + it('exports strings', () => { |
| 36 | + expect(MDCSelectHelperTextFoundation.strings).toEqual(strings); |
| 37 | + }); |
| 38 | + |
| 39 | + it('defaultAdapter returns a complete adapter implementation', () => { |
| 40 | + verifyDefaultAdapter(MDCSelectHelperTextFoundation, [ |
| 41 | + 'addClass', |
| 42 | + 'removeClass', |
| 43 | + 'hasClass', |
| 44 | + 'setAttr', |
| 45 | + 'removeAttr', |
| 46 | + 'setContent', |
| 47 | + ]); |
| 48 | + }); |
| 49 | + |
| 50 | + const setupTest = |
| 51 | + () => { |
| 52 | + const {foundation, mockAdapter} = |
| 53 | + setUpFoundationTest(MDCSelectHelperTextFoundation); |
| 54 | + return {foundation, mockAdapter}; |
| 55 | + } |
| 56 | + |
| 57 | + it('#setContent sets the content of the helper text element', () => { |
| 58 | + const {foundation, mockAdapter} = setupTest(); |
| 59 | + foundation.setContent('foo'); |
| 60 | + expect(mockAdapter.setContent).toHaveBeenCalledWith('foo'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('#setPersistent toggles the persistent class', () => { |
| 64 | + const {foundation, mockAdapter} = setupTest(); |
| 65 | + foundation.setPersistent(true); |
| 66 | + expect(mockAdapter.addClass) |
| 67 | + .toHaveBeenCalledWith(cssClasses.HELPER_TEXT_PERSISTENT); |
| 68 | + foundation.setPersistent(false); |
| 69 | + expect(mockAdapter.removeClass) |
| 70 | + .toHaveBeenCalledWith(cssClasses.HELPER_TEXT_PERSISTENT); |
| 71 | + }); |
| 72 | + |
| 73 | + it('#setValidation toggles the validation class', () => { |
| 74 | + const {foundation, mockAdapter} = setupTest(); |
| 75 | + foundation.setValidation(true); |
| 76 | + expect(mockAdapter.addClass) |
| 77 | + .toHaveBeenCalledWith(cssClasses.HELPER_TEXT_VALIDATION_MSG); |
| 78 | + foundation.setValidation(false); |
| 79 | + expect(mockAdapter.removeClass) |
| 80 | + .toHaveBeenCalledWith(cssClasses.HELPER_TEXT_VALIDATION_MSG); |
| 81 | + }); |
| 82 | + |
| 83 | + it('#showToScreenReader removes aria-hidden from helperText', () => { |
| 84 | + const {foundation, mockAdapter} = setupTest(); |
| 85 | + foundation.showToScreenReader(); |
| 86 | + expect(mockAdapter.removeAttr).toHaveBeenCalledWith('aria-hidden'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('#setValidity adds role="alert" to helper text if input is invalid and helper text is being used ' + |
| 90 | + 'as a validation message', |
| 91 | + () => { |
| 92 | + const {foundation, mockAdapter} = setupTest(); |
| 93 | + const inputIsValid = false; |
| 94 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_PERSISTENT) |
| 95 | + .and.returnValue(false); |
| 96 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_VALIDATION_MSG) |
| 97 | + .and.returnValue(true); |
| 98 | + foundation.setValidity(inputIsValid); |
| 99 | + expect(mockAdapter.setAttr).toHaveBeenCalledWith('role', 'alert'); |
| 100 | + }); |
| 101 | + |
| 102 | + it('#setValidity removes role="alert" if input is valid', () => { |
| 103 | + const {foundation, mockAdapter} = setupTest(); |
| 104 | + const inputIsValid = true; |
| 105 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_PERSISTENT) |
| 106 | + .and.returnValue(false); |
| 107 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_VALIDATION_MSG) |
| 108 | + .and.returnValue(true); |
| 109 | + foundation.setValidity(inputIsValid); |
| 110 | + expect(mockAdapter.removeAttr).toHaveBeenCalledWith('role'); |
| 111 | + }); |
| 112 | + |
| 113 | + it('#setValidity sets aria-hidden="true" on helper text by default', () => { |
| 114 | + const {foundation, mockAdapter} = setupTest(); |
| 115 | + const inputIsValid = true; |
| 116 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_PERSISTENT) |
| 117 | + .and.returnValue(false); |
| 118 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_VALIDATION_MSG) |
| 119 | + .and.returnValue(false); |
| 120 | + foundation.setValidity(inputIsValid); |
| 121 | + expect(mockAdapter.setAttr).toHaveBeenCalledWith('aria-hidden', 'true'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('#setValidity does not set aria-hidden on helper text when it is persistent', |
| 125 | + () => { |
| 126 | + const {foundation, mockAdapter} = setupTest(); |
| 127 | + const inputIsValid = true; |
| 128 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_PERSISTENT) |
| 129 | + .and.returnValue(true); |
| 130 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_VALIDATION_MSG) |
| 131 | + .and.returnValue(false); |
| 132 | + foundation.setValidity(inputIsValid); |
| 133 | + expect(mockAdapter.setAttr) |
| 134 | + .not.toHaveBeenCalledWith('aria-hidden', 'true'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('#setValidity does not set aria-hidden if input is invalid and helper text is validation message', |
| 138 | + () => { |
| 139 | + const {foundation, mockAdapter} = setupTest(); |
| 140 | + const inputIsValid = false; |
| 141 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_PERSISTENT) |
| 142 | + .and.returnValue(false); |
| 143 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_VALIDATION_MSG) |
| 144 | + .and.returnValue(true); |
| 145 | + foundation.setValidity(inputIsValid); |
| 146 | + expect(mockAdapter.setAttr) |
| 147 | + .not.toHaveBeenCalledWith('aria-hidden', 'true'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('#setValidity sets aria-hidden=true if input is valid and helper text is validation message', |
| 151 | + () => { |
| 152 | + const {foundation, mockAdapter} = setupTest(); |
| 153 | + const inputIsValid = true; |
| 154 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_PERSISTENT) |
| 155 | + .and.returnValue(false); |
| 156 | + mockAdapter.hasClass.withArgs(cssClasses.HELPER_TEXT_VALIDATION_MSG) |
| 157 | + .and.returnValue(true); |
| 158 | + foundation.setValidity(inputIsValid); |
| 159 | + expect(mockAdapter.setAttr).toHaveBeenCalledWith('aria-hidden', 'true'); |
| 160 | + }); |
| 161 | +}); |
0 commit comments