|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2026 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var tape = require( 'tape' ); |
| 24 | +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); |
| 25 | +var hasProp = require( '@stdlib/assert/has-property' ); |
| 26 | +var isString = require( '@stdlib/assert/is-string' ).isPrimitive; |
| 27 | +var Autosize = require( './../lib' ); |
| 28 | + |
| 29 | + |
| 30 | +// TESTS // |
| 31 | + |
| 32 | +tape( 'main export is a function', function test( t ) { |
| 33 | + t.ok( true, __filename ); |
| 34 | + t.strictEqual( typeof Autosize, 'function', 'main export is a function' ); |
| 35 | + t.end(); |
| 36 | +}); |
| 37 | + |
| 38 | +tape( 'the constructor returns an instance which has a `contains` property', function test( t ) { |
| 39 | + var v = new Autosize(); |
| 40 | + t.strictEqual( hasOwnProp( v, 'contains' ), false, 'returns expected value' ); |
| 41 | + t.strictEqual( hasProp( v, 'contains' ), true, 'returns expected value' ); |
| 42 | + t.strictEqual( isString( v.contains ), true, 'returns expected value' ); |
| 43 | + t.end(); |
| 44 | +}); |
| 45 | + |
| 46 | +tape( 'the constructor returns an instance having a `contains` property which throws an error if set to an invalid value', function test( t ) { |
| 47 | + var values; |
| 48 | + var i; |
| 49 | + |
| 50 | + values = [ |
| 51 | + 'beep', |
| 52 | + 'boop', |
| 53 | + 'invalid', |
| 54 | + 'CONTENT', |
| 55 | + 'PADDING', |
| 56 | + 5, |
| 57 | + NaN, |
| 58 | + null, |
| 59 | + void 0, |
| 60 | + true, |
| 61 | + false, |
| 62 | + [], |
| 63 | + {}, |
| 64 | + function noop() {} |
| 65 | + ]; |
| 66 | + for ( i = 0; i < values.length; i++ ) { |
| 67 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 68 | + } |
| 69 | + t.end(); |
| 70 | + |
| 71 | + function badValue( value ) { |
| 72 | + return function badValue() { |
| 73 | + var autosize = new Autosize(); |
| 74 | + autosize.contains = value; |
| 75 | + }; |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +tape( 'the constructor returns an instance having a `contains` property which returns the method for determining how a size calculation should be performed', function test( t ) { |
| 80 | + var autosize = new Autosize(); |
| 81 | + t.strictEqual( autosize.contains, 'content', 'returns expected value' ); |
| 82 | + |
| 83 | + autosize = new Autosize({ |
| 84 | + 'contains': 'padding' |
| 85 | + }); |
| 86 | + t.strictEqual( autosize.contains, 'padding', 'returns expected value' ); |
| 87 | + |
| 88 | + autosize = new Autosize({ |
| 89 | + 'contains': 'content' |
| 90 | + }); |
| 91 | + t.strictEqual( autosize.contains, 'content', 'returns expected value' ); |
| 92 | + |
| 93 | + t.end(); |
| 94 | +}); |
| 95 | + |
| 96 | +tape( 'the constructor returns an instance having a `contains` property which can be set to a valid method', function test( t ) { |
| 97 | + var autosize = new Autosize(); |
| 98 | + |
| 99 | + autosize.contains = 'padding'; |
| 100 | + t.strictEqual( autosize.contains, 'padding', 'returns expected value' ); |
| 101 | + |
| 102 | + autosize.contains = 'content'; |
| 103 | + t.strictEqual( autosize.contains, 'content', 'returns expected value' ); |
| 104 | + |
| 105 | + t.end(); |
| 106 | +}); |
| 107 | + |
| 108 | +tape( 'the constructor returns an instance which emits an event when the `contains` property is set to a new value', function test( t ) { |
| 109 | + var autosize; |
| 110 | + var count; |
| 111 | + |
| 112 | + autosize = new Autosize(); |
| 113 | + count = 0; |
| 114 | + |
| 115 | + autosize.on( 'change', onChange ); |
| 116 | + |
| 117 | + autosize.contains = 'padding'; |
| 118 | + t.strictEqual( count, 1, 'returns expected value' ); |
| 119 | + |
| 120 | + autosize.contains = 'content'; |
| 121 | + t.strictEqual( count, 2, 'returns expected value' ); |
| 122 | + |
| 123 | + // Setting to the same value should not emit an event: |
| 124 | + autosize.contains = 'content'; |
| 125 | + t.strictEqual( count, 2, 'returns expected value' ); |
| 126 | + |
| 127 | + t.end(); |
| 128 | + |
| 129 | + function onChange() { |
| 130 | + count += 1; |
| 131 | + } |
| 132 | +}); |
0 commit comments