|
| 1 | +import {Component} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed, async} from '@angular/core/testing'; |
| 3 | +import {By} from '@angular/platform-browser'; |
| 4 | +import {MdInputModule} from './input'; |
| 5 | +import {MdTextareaAutosize} from './autosize'; |
| 6 | + |
| 7 | + |
| 8 | +describe('MdTextareaAutosize', () => { |
| 9 | + let fixture: ComponentFixture<AutosizeTextAreaWithContent>; |
| 10 | + let textarea: HTMLTextAreaElement; |
| 11 | + let autosize: MdTextareaAutosize; |
| 12 | + |
| 13 | + beforeEach(async(() => { |
| 14 | + TestBed.configureTestingModule({ |
| 15 | + imports: [MdInputModule], |
| 16 | + declarations: [AutosizeTextAreaWithContent, AutosizeTextAreaWithValue], |
| 17 | + }); |
| 18 | + |
| 19 | + TestBed.compileComponents(); |
| 20 | + })); |
| 21 | + |
| 22 | + beforeEach(() => { |
| 23 | + fixture = TestBed.createComponent(AutosizeTextAreaWithContent); |
| 24 | + fixture.detectChanges(); |
| 25 | + |
| 26 | + textarea = fixture.nativeElement.querySelector('textarea'); |
| 27 | + autosize = fixture.debugElement.query( |
| 28 | + By.directive(MdTextareaAutosize)).injector.get(MdTextareaAutosize); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should resize the textarea based on its content', () => { |
| 32 | + let previousHeight = textarea.offsetHeight; |
| 33 | + |
| 34 | + fixture.componentInstance.content = ` |
| 35 | + Once upon a midnight dreary, while I pondered, weak and weary, |
| 36 | + Over many a quaint and curious volume of forgotten lore— |
| 37 | + While I nodded, nearly napping, suddenly there came a tapping, |
| 38 | + As of some one gently rapping, rapping at my chamber door. |
| 39 | + “’Tis some visitor,” I muttered, “tapping at my chamber door— |
| 40 | + Only this and nothing more.”`; |
| 41 | + |
| 42 | + // Manually call resizeToFitContent instead of faking an `input` event. |
| 43 | + fixture.detectChanges(); |
| 44 | + autosize.resizeToFitContent(); |
| 45 | + |
| 46 | + expect(textarea.offsetHeight) |
| 47 | + .toBeGreaterThan(previousHeight, 'Expected textarea to have grown with added content.'); |
| 48 | + expect(textarea.offsetHeight) |
| 49 | + .toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight'); |
| 50 | + |
| 51 | + previousHeight = textarea.offsetHeight; |
| 52 | + fixture.componentInstance.content += ` |
| 53 | + Ah, distinctly I remember it was in the bleak December; |
| 54 | + And each separate dying ember wrought its ghost upon the floor. |
| 55 | + Eagerly I wished the morrow;—vainly I had sought to borrow |
| 56 | + From my books surcease of sorrow—sorrow for the lost Lenore— |
| 57 | + For the rare and radiant maiden whom the angels name Lenore— |
| 58 | + Nameless here for evermore.`; |
| 59 | + |
| 60 | + fixture.detectChanges(); |
| 61 | + autosize.resizeToFitContent(); |
| 62 | + |
| 63 | + expect(textarea.offsetHeight) |
| 64 | + .toBeGreaterThan(previousHeight, 'Expected textarea to have grown with added content.'); |
| 65 | + expect(textarea.offsetHeight) |
| 66 | + .toBe(textarea.scrollHeight, 'Expected textarea height to match its scrollHeight'); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should set a min-width based on minRows', () => { |
| 70 | + expect(textarea.style.minHeight).toBeFalsy(); |
| 71 | + |
| 72 | + fixture.componentInstance.minRows = 4; |
| 73 | + fixture.detectChanges(); |
| 74 | + |
| 75 | + expect(textarea.style.minHeight).toBeDefined('Expected a min-height to be set via minRows.'); |
| 76 | + |
| 77 | + let previousMinHeight = parseInt(textarea.style.minHeight); |
| 78 | + fixture.componentInstance.minRows = 6; |
| 79 | + fixture.detectChanges(); |
| 80 | + |
| 81 | + expect(parseInt(textarea.style.minHeight)) |
| 82 | + .toBeGreaterThan(previousMinHeight, 'Expected increased min-height with minRows increase.'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should set a max-width based on maxRows', () => { |
| 86 | + expect(textarea.style.maxHeight).toBeFalsy(); |
| 87 | + |
| 88 | + fixture.componentInstance.maxRows = 4; |
| 89 | + fixture.detectChanges(); |
| 90 | + |
| 91 | + expect(textarea.style.maxHeight).toBeDefined('Expected a max-height to be set via maxRows.'); |
| 92 | + |
| 93 | + let previousMaxHeight = parseInt(textarea.style.maxHeight); |
| 94 | + fixture.componentInstance.maxRows = 6; |
| 95 | + fixture.detectChanges(); |
| 96 | + |
| 97 | + expect(parseInt(textarea.style.maxHeight)) |
| 98 | + .toBeGreaterThan(previousMaxHeight, 'Expected increased max-height with maxRows increase.'); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | + |
| 103 | +// Styles to reset padding and border to make measurement comparisons easier. |
| 104 | +const textareaStyleReset = ` |
| 105 | + textarea { |
| 106 | + padding: 0; |
| 107 | + border: none; |
| 108 | + overflow: auto; |
| 109 | + }`; |
| 110 | + |
| 111 | +@Component({ |
| 112 | + template: `<textarea md-autosize [minRows]="minRows" [maxRows]="maxRows">{{content}}</textarea>`, |
| 113 | + styles: [textareaStyleReset], |
| 114 | +}) |
| 115 | +class AutosizeTextAreaWithContent { |
| 116 | + minRows: number = null; |
| 117 | + maxRows: number = null; |
| 118 | + content: string = ''; |
| 119 | +} |
| 120 | + |
| 121 | +@Component({ |
| 122 | + template: `<textarea md-autosize [value]="value"></textarea>`, |
| 123 | + styles: [textareaStyleReset], |
| 124 | +}) |
| 125 | +class AutosizeTextAreaWithValue { |
| 126 | + value: string = ''; |
| 127 | +} |
0 commit comments