|
| 1 | +/* |
| 2 | + * This program is part of the OpenLMIS logistics management information system platform software. |
| 3 | + * Copyright © 2017 VillageReach |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify it under the terms |
| 6 | + * of the GNU Affero General Public License as published by the Free Software Foundation, either |
| 7 | + * version 3 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 10 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + * See the GNU Affero General Public License for more details. You should have received a copy of |
| 12 | + * the GNU Affero General Public License along with this program. If not, see |
| 13 | + * http://www.gnu.org/licenses. For additional information contact info@OpenLMIS.org. |
| 14 | + */ |
| 15 | + |
| 16 | +describe('openlmisLongText directive', function() { |
| 17 | + |
| 18 | + beforeEach(function() { |
| 19 | + module('openlmis-table-form'); |
| 20 | + |
| 21 | + inject(function($injector) { |
| 22 | + this.$compile = $injector.get('$compile'); |
| 23 | + this.$rootScope = $injector.get('$rootScope'); |
| 24 | + }); |
| 25 | + |
| 26 | + this.$scope = this.$rootScope.$new(); |
| 27 | + |
| 28 | + this.compileMarkup = function(markup) { |
| 29 | + const element = this.$compile(markup)(this.$scope); |
| 30 | + angular.element('body').append(element); |
| 31 | + this.$scope.$apply(); |
| 32 | + return element; |
| 33 | + }; |
| 34 | + }); |
| 35 | + |
| 36 | + it('should set an explicit width and height on a textarea with the class', function() { |
| 37 | + const textarea = this.compileMarkup('<textarea class="openlmis-long-text">short</textarea>'); |
| 38 | + |
| 39 | + expect(Number.parseInt(textarea[0].style.width, 10)).toBeGreaterThan(0); |
| 40 | + expect(Number.parseInt(textarea[0].style.height, 10)).toBeGreaterThan(0); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should grow width when the value becomes longer', function() { |
| 44 | + const textarea = this.compileMarkup('<textarea class="openlmis-long-text">short</textarea>'); |
| 45 | + const previousWidth = Number.parseInt(textarea[0].style.width, 10); |
| 46 | + |
| 47 | + textarea[0].value = 'a value that is clearly longer than the initial one'; |
| 48 | + this.$scope.$apply(); |
| 49 | + |
| 50 | + expect(Number.parseInt(textarea[0].style.width, 10)).toBeGreaterThan(previousWidth); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should grow height when the value spans multiple lines', function() { |
| 54 | + const textarea = this.compileMarkup('<textarea class="openlmis-long-text">short</textarea>'); |
| 55 | + const previousHeight = Number.parseInt(textarea[0].style.height, 10); |
| 56 | + |
| 57 | + textarea[0].value = 'first line\nsecond line\nthird line\nfourth line'; |
| 58 | + this.$scope.$apply(); |
| 59 | + |
| 60 | + expect(Number.parseInt(textarea[0].style.height, 10)).toBeGreaterThan(previousHeight); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should not resize a textarea that does not have the class', function() { |
| 64 | + const textarea = this.compileMarkup('<textarea>plain</textarea>'); |
| 65 | + |
| 66 | + expect(textarea[0].style.width).toEqual(''); |
| 67 | + expect(textarea[0].style.height).toEqual(''); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should not set an inline size on a read-only span with the class', function() { |
| 71 | + const span = this.compileMarkup('<span class="openlmis-long-text">read only</span>'); |
| 72 | + |
| 73 | + expect(span[0].style.width).toEqual(''); |
| 74 | + expect(span[0].style.height).toEqual(''); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should not collapse a textarea that is hidden when linked', function() { |
| 78 | + const textarea = this.compileMarkup( |
| 79 | + '<textarea class="openlmis-long-text" style="display: none;">hidden</textarea>' |
| 80 | + ); |
| 81 | + |
| 82 | + // A hidden textarea reports scrollHeight 0; sizing it then would bake height:0 and |
| 83 | + // leave it collapsed once shown (e.g. an ng-show free-text comment). It must be skipped. |
| 84 | + expect(textarea[0].style.height).not.toEqual('0px'); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should size a pre-filled textarea once it becomes visible after being hidden', function() { |
| 88 | + const textarea = this.compileMarkup( |
| 89 | + '<textarea class="openlmis-long-text" style="display: none;">a pre-filled value</textarea>' |
| 90 | + ); |
| 91 | + |
| 92 | + // Hidden at link time -> skipped, no inline height yet. |
| 93 | + expect(textarea[0].style.height).toEqual(''); |
| 94 | + |
| 95 | + // Shown later without a value change -> the visibility watch must size it. |
| 96 | + textarea[0].style.display = ''; |
| 97 | + this.$scope.$apply(); |
| 98 | + |
| 99 | + expect(Number.parseInt(textarea[0].style.height, 10)).toBeGreaterThan(0); |
| 100 | + expect(Number.parseInt(textarea[0].style.width, 10)).toBeGreaterThan(0); |
| 101 | + }); |
| 102 | +}); |
0 commit comments