|
| 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 isUndefined = require( '@stdlib/assert/is-undefined' ); |
| 26 | +var hasProp = require( '@stdlib/assert/has-property' ); |
| 27 | +var isCollection = require( '@stdlib/assert/is-collection' ); |
| 28 | +var Scale = require( './../lib' ); |
| 29 | + |
| 30 | + |
| 31 | +// TESTS // |
| 32 | + |
| 33 | +tape( 'main export is a function', function test( t ) { |
| 34 | + t.ok( true, __filename ); |
| 35 | + t.strictEqual( typeof Scale, 'function', 'main export is a function' ); |
| 36 | + t.end(); |
| 37 | +}); |
| 38 | + |
| 39 | +tape( 'the constructor returns an instance which has a `domain` property', function test( t ) { |
| 40 | + var v; |
| 41 | + |
| 42 | + v = new Scale({ |
| 43 | + 'name': 'xScale' |
| 44 | + }); |
| 45 | + t.strictEqual( hasOwnProp( v, 'domain' ), false, 'returns expected value' ); |
| 46 | + t.strictEqual( hasProp( v, 'domain' ), true, 'returns expected value' ); |
| 47 | + t.strictEqual( isUndefined( v.domain ), true, 'returns expected value' ); |
| 48 | + |
| 49 | + v = new Scale({ |
| 50 | + 'name': 'xScale', |
| 51 | + 'domain': [ 0, 10 ] |
| 52 | + }); |
| 53 | + t.strictEqual( isCollection( v.domain ), true, 'returns expected value' ); |
| 54 | + |
| 55 | + t.end(); |
| 56 | +}); |
| 57 | + |
| 58 | +tape( 'the constructor returns an instance having a `domain` property which throws an error if set to an invalid value', function test( t ) { |
| 59 | + var values; |
| 60 | + var i; |
| 61 | + var v; |
| 62 | + |
| 63 | + v = new Scale({ |
| 64 | + 'name': 'xScale' |
| 65 | + }); |
| 66 | + |
| 67 | + values = [ |
| 68 | + '5', |
| 69 | + 5, |
| 70 | + NaN, |
| 71 | + true, |
| 72 | + false, |
| 73 | + function noop() {} |
| 74 | + ]; |
| 75 | + for ( i = 0; i < values.length; i++ ) { |
| 76 | + t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] ); |
| 77 | + } |
| 78 | + t.end(); |
| 79 | + |
| 80 | + function badValue( value ) { |
| 81 | + return function badValue() { |
| 82 | + v.domain = value; |
| 83 | + }; |
| 84 | + } |
| 85 | +}); |
| 86 | + |
| 87 | +tape( 'the constructor returns an instance having a `domain` property', function test( t ) { |
| 88 | + var scale; |
| 89 | + |
| 90 | + scale = new Scale({ |
| 91 | + 'name': 'xScale' |
| 92 | + }); |
| 93 | + t.strictEqual( isUndefined( scale.domain ), true, 'returns expected value' ); |
| 94 | + |
| 95 | + scale = new Scale({ |
| 96 | + 'name': 'xScale', |
| 97 | + 'domain': [ 0, 10 ] |
| 98 | + }); |
| 99 | + t.deepEqual( scale.domain, [ 0, 10 ], 'returns expected value' ); |
| 100 | + |
| 101 | + scale = new Scale({ |
| 102 | + 'name': 'xScale', |
| 103 | + 'domain': [ 0, 20 ] |
| 104 | + }); |
| 105 | + t.deepEqual( scale.domain, [ 0, 20 ], 'returns expected value' ); |
| 106 | + |
| 107 | + t.end(); |
| 108 | +}); |
| 109 | + |
| 110 | +tape( 'the constructor returns an instance having a `domain` property which can be set to a valid value', function test( t ) { |
| 111 | + var scale; |
| 112 | + |
| 113 | + scale = new Scale({ |
| 114 | + 'name': 'xScale' |
| 115 | + }); |
| 116 | + |
| 117 | + scale.domain = [ 0, 10 ]; |
| 118 | + t.deepEqual( scale.domain, [ 0, 10 ], 'returns expected value' ); |
| 119 | + |
| 120 | + scale.domain = [ 0, 20 ]; |
| 121 | + t.deepEqual( scale.domain, [ 0, 20 ], 'returns expected value' ); |
| 122 | + |
| 123 | + t.end(); |
| 124 | +}); |
| 125 | + |
| 126 | +tape( 'the constructor returns an instance which emits an event when the `domain` property is set to a new value', function test( t ) { |
| 127 | + var scale; |
| 128 | + var count; |
| 129 | + |
| 130 | + scale = new Scale({ |
| 131 | + 'name': 'xScale' |
| 132 | + }); |
| 133 | + count = 0; |
| 134 | + |
| 135 | + scale.on( 'change', onChange ); |
| 136 | + |
| 137 | + scale.domain = [ 0, 10 ]; |
| 138 | + t.strictEqual( count, 1, 'returns expected value' ); |
| 139 | + |
| 140 | + scale.domain = [ 0, 20 ]; |
| 141 | + t.strictEqual( count, 2, 'returns expected value' ); |
| 142 | + |
| 143 | + // Setting to the same value should not emit an event: |
| 144 | + scale.domain = [ 0, 20 ]; |
| 145 | + t.strictEqual( count, 2, 'returns expected value' ); |
| 146 | + |
| 147 | + t.end(); |
| 148 | + |
| 149 | + function onChange() { |
| 150 | + count += 1; |
| 151 | + } |
| 152 | +}); |
0 commit comments