|
| 1 | +import _ from 'lodash'; |
| 2 | +import Colors from '../../../style/colors'; |
1 | 3 | import * as uut from '../Presenter';
|
2 | 4 |
|
3 | 5 | describe('TextField:Presenter', () => {
|
| 6 | + describe('get color by state', () => { |
| 7 | + const colorsByState = { |
| 8 | + default: 'purple', |
| 9 | + focus: 'green', |
| 10 | + error: 'orange', |
| 11 | + readonly: 'pink', |
| 12 | + disabled: 'lightblue' |
| 13 | + }; |
| 14 | + |
| 15 | + const defaultContext = { |
| 16 | + isFocused: false, |
| 17 | + hasValue: false, |
| 18 | + isValid: true, |
| 19 | + failingValidatorIndex: undefined, |
| 20 | + disabled: false, |
| 21 | + readonly: false, |
| 22 | + validateField: _.noop, |
| 23 | + checkValidity: () => true, |
| 24 | + isMandatory: false |
| 25 | + }; |
| 26 | + |
| 27 | + it('should return passed color when is a string', () => { |
| 28 | + expect(uut.getColorByState('blue', defaultContext)).toEqual('blue'); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should return passed color when is design token', () => { |
| 32 | + expect(uut.getColorByState(Colors.$backgroundDark, defaultContext)).toEqual(Colors.$backgroundDark); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should return textDefault color when color is an empty object', () => { |
| 36 | + expect(uut.getColorByState({}, defaultContext)).toEqual(Colors.$textDefault); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return textDefault color when no default color passed in color object', () => { |
| 40 | + expect(uut.getColorByState({error: 'red'}, defaultContext)).toEqual(Colors.$textDefault); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should return default color', () => { |
| 44 | + expect(uut.getColorByState(colorsByState, defaultContext)).toEqual(colorsByState.default); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should return focused color', () => { |
| 48 | + const context = {...defaultContext, isFocused: true}; |
| 49 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.focus); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return error color', () => { |
| 53 | + const context = {...defaultContext, isValid: false}; |
| 54 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.error); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should return readonly color', () => { |
| 58 | + const context = {...defaultContext, readonly: true}; |
| 59 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.readonly); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should return disabled color', () => { |
| 63 | + const context = {...defaultContext, disabled: true}; |
| 64 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.disabled); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should return disabled color, over readonly', () => { |
| 68 | + const context = {...defaultContext, disabled: true, readonly: true}; |
| 69 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.disabled); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should return disabled color, over error', () => { |
| 73 | + const context = {...defaultContext, disabled: true, isValid: false}; |
| 74 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.disabled); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should return disabled color, over focused', () => { |
| 78 | + const context = {...defaultContext, disabled: true, isFocused: false}; |
| 79 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.disabled); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return readonly color, over error', () => { |
| 83 | + const context = {...defaultContext, readonly: true, isValid: false}; |
| 84 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.readonly); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return readonly color, over focused', () => { |
| 88 | + const context = {...defaultContext, readonly: true, isFocused: false}; |
| 89 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.readonly); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should return error color, over focused', () => { |
| 93 | + const context = {...defaultContext, isValid: false, isFocused: true}; |
| 94 | + expect(uut.getColorByState(colorsByState, context)).toEqual(colorsByState.error); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
4 | 98 | describe('validate', () => {
|
5 | 99 | it('should return true if validator is undefined', () => {
|
6 | 100 | expect(uut.validate('value', undefined)).toEqual([true, undefined]);
|
@@ -77,4 +171,70 @@ describe('TextField:Presenter', () => {
|
77 | 171 | expect(uut.shouldHidePlaceholder({floatingPlaceholder: true, hint: 'Hint text', floatOnFocus: false}, true)).toBe(true);
|
78 | 172 | });
|
79 | 173 | });
|
| 174 | + |
| 175 | + describe('Should float placeholder', () => { |
| 176 | + it('should not float when isFocused is false', () => { |
| 177 | + const props = {floatOnFocus: true}; |
| 178 | + const isFocused = false; |
| 179 | + const hasValue = undefined; |
| 180 | + const value = undefined; |
| 181 | + expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false); |
| 182 | + }); |
| 183 | + |
| 184 | + it('should not float when isFocused is false', () => { |
| 185 | + const props = {floatOnFocus: false}; |
| 186 | + const isFocused = true; |
| 187 | + const hasValue = undefined; |
| 188 | + const value = undefined; |
| 189 | + expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false); |
| 190 | + }); |
| 191 | + |
| 192 | + it('should float when floatOnFocus and isFocused is true', () => { |
| 193 | + const props = {floatOnFocus: true}; |
| 194 | + const isFocused = true; |
| 195 | + const hasValue = undefined; |
| 196 | + const value = undefined; |
| 197 | + expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(true); |
| 198 | + }); |
| 199 | + |
| 200 | + it('should float when hasValue is true', () => { |
| 201 | + const props = {floatOnFocus: false}; |
| 202 | + const isFocused = false; |
| 203 | + const hasValue = true; |
| 204 | + const value = undefined; |
| 205 | + expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(true); |
| 206 | + }); |
| 207 | + |
| 208 | + it('should not float when hasValue is false', () => { |
| 209 | + const props = {floatOnFocus: false}; |
| 210 | + const isFocused = false; |
| 211 | + const hasValue = false; |
| 212 | + const value = undefined; |
| 213 | + expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should float when defaultValue and value is undefined', () => { |
| 217 | + const props = {floatOnFocus: false, defaultValue: 'default value'}; |
| 218 | + const isFocused = false; |
| 219 | + const hasValue = false; |
| 220 | + const value = undefined; |
| 221 | + expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(true); |
| 222 | + }); |
| 223 | + |
| 224 | + it('should not float when defaultValue and value', () => { |
| 225 | + const props = {floatOnFocus: false, defaultValue: 'default value'}; |
| 226 | + const isFocused = false; |
| 227 | + const hasValue = false; |
| 228 | + const value = 'value'; |
| 229 | + expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false); |
| 230 | + }); |
| 231 | + |
| 232 | + it('should not float when no defaultValue and value', () => { |
| 233 | + const props = {floatOnFocus: false, defaultValue: undefined}; |
| 234 | + const isFocused = false; |
| 235 | + const hasValue = false; |
| 236 | + const value = 'value'; |
| 237 | + expect(uut.shouldPlaceholderFloat(props, isFocused, hasValue, value)).toBe(false); |
| 238 | + }); |
| 239 | + }); |
80 | 240 | });
|
0 commit comments