|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import 'jasmine'; |
| 8 | + |
| 9 | +import {spyOnAllFunctions} from '../../testing/jasmine'; |
| 10 | +import {FieldFoundation} from '../lib/foundation'; |
| 11 | +import {FieldAdapter, LabelType} from '../lib/state'; |
| 12 | + |
| 13 | +describe('FieldFoundation', () => { |
| 14 | + function setupTest() { |
| 15 | + const adapter: FieldAdapter = { |
| 16 | + state: { |
| 17 | + disabled: false, |
| 18 | + error: false, |
| 19 | + labelText: '', |
| 20 | + focused: false, |
| 21 | + populated: false, |
| 22 | + required: false, |
| 23 | + visibleLabelType: LabelType.RESTING, |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + const foundation = new FieldFoundation(adapter); |
| 28 | + return { |
| 29 | + foundation: spyOnAllFunctions(foundation).and.callThrough(), |
| 30 | + adapter: spyOnAllFunctions(adapter).and.callThrough(), |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + it('#onDisabledChange() unfocuses field when disabled', () => { |
| 35 | + const {foundation, adapter} = setupTest(); |
| 36 | + foundation.init(); |
| 37 | + adapter.state.focused = true; |
| 38 | + adapter.state.disabled = true; |
| 39 | + expect(adapter.state.focused) |
| 40 | + .withContext('focused is false after disabled is set to true') |
| 41 | + .toBe(false); |
| 42 | + }); |
| 43 | + |
| 44 | + it('#onFocusedChange() does not allow focus if disabled', () => { |
| 45 | + const {foundation, adapter} = setupTest(); |
| 46 | + foundation.init(); |
| 47 | + adapter.state.disabled = true; |
| 48 | + adapter.state.focused = true; |
| 49 | + expect(adapter.state.focused) |
| 50 | + .withContext('focused set back to false when disabled') |
| 51 | + .toBe(false); |
| 52 | + }); |
| 53 | + |
| 54 | + it('#updateLayoutAsterisk() sets labelText when label or required changes', |
| 55 | + () => { |
| 56 | + const {foundation, adapter} = setupTest(); |
| 57 | + foundation.init(); |
| 58 | + const labelValue = 'Label'; |
| 59 | + adapter.state.label = labelValue; |
| 60 | + expect(adapter.state.labelText) |
| 61 | + .withContext('labelText should equal label when not required') |
| 62 | + .toBe(labelValue); |
| 63 | + adapter.state.required = true; |
| 64 | + expect(adapter.state.labelText) |
| 65 | + .withContext( |
| 66 | + 'labelText should equal label with asterisk when required') |
| 67 | + .toBe(`${labelValue}*`); |
| 68 | + adapter.state.label = undefined; |
| 69 | + expect(adapter.state.labelText) |
| 70 | + .withContext( |
| 71 | + 'labelText should be empty string if label is not provided, even when required') |
| 72 | + .toBe(''); |
| 73 | + }); |
| 74 | +}); |
0 commit comments