Skip to content

Commit 4ddc398

Browse files
committed
test: add tests for Scale constructor and properties
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent da890f3 commit 4ddc398

File tree

13 files changed

+2269
-0
lines changed

13 files changed

+2269
-0
lines changed
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
});
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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 isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
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 `domainMax` property', function test( t ) {
40+
var v;
41+
42+
v = new Scale({
43+
'name': 'xScale'
44+
});
45+
t.strictEqual( hasOwnProp( v, 'domainMax' ), false, 'returns expected value' );
46+
t.strictEqual( hasProp( v, 'domainMax' ), true, 'returns expected value' );
47+
t.strictEqual( isUndefined( v.domainMax ), true, 'returns expected value' );
48+
49+
v = new Scale({
50+
'name': 'xScale',
51+
'domainMax': 10
52+
});
53+
t.strictEqual( isNumber( v.domainMax ), true, 'returns expected value' );
54+
55+
t.end();
56+
});
57+
58+
tape( 'the constructor returns an instance having a `domainMax` 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+
null,
70+
true,
71+
false,
72+
[],
73+
{},
74+
function noop() {}
75+
];
76+
for ( i = 0; i < values.length; i++ ) {
77+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
78+
}
79+
t.end();
80+
81+
function badValue( value ) {
82+
return function badValue() {
83+
v.domainMax = value;
84+
};
85+
}
86+
});
87+
88+
tape( 'the constructor returns an instance having a `domainMax` property', function test( t ) {
89+
var scale;
90+
91+
scale = new Scale({
92+
'name': 'xScale'
93+
});
94+
t.strictEqual( isUndefined( scale.domainMax ), true, 'returns expected value' );
95+
96+
scale = new Scale({
97+
'name': 'xScale',
98+
'domainMax': 10
99+
});
100+
t.strictEqual( scale.domainMax, 10, 'returns expected value' );
101+
102+
scale = new Scale({
103+
'name': 'xScale',
104+
'domainMax': 20
105+
});
106+
t.strictEqual( scale.domainMax, 20, 'returns expected value' );
107+
108+
t.end();
109+
});
110+
111+
tape( 'the constructor returns an instance having a `domainMax` property which can be set to a valid value', function test( t ) {
112+
var scale;
113+
114+
scale = new Scale({
115+
'name': 'xScale'
116+
});
117+
118+
scale.domainMax = 10;
119+
t.strictEqual( scale.domainMax, 10, 'returns expected value' );
120+
121+
scale.domainMax = 20;
122+
t.strictEqual( scale.domainMax, 20, 'returns expected value' );
123+
124+
t.end();
125+
});
126+
127+
tape( 'the constructor returns an instance which emits an event when the `domainMax` property is set to a new value', function test( t ) {
128+
var scale;
129+
var count;
130+
131+
scale = new Scale({
132+
'name': 'xScale'
133+
});
134+
count = 0;
135+
136+
scale.on( 'change', onChange );
137+
138+
scale.domainMax = 10;
139+
t.strictEqual( count, 1, 'returns expected value' );
140+
141+
scale.domainMax = 20;
142+
t.strictEqual( count, 2, 'returns expected value' );
143+
144+
// Setting to the same value should not emit an event:
145+
scale.domainMax = 20;
146+
t.strictEqual( count, 2, 'returns expected value' );
147+
148+
t.end();
149+
150+
function onChange() {
151+
count += 1;
152+
}
153+
});

0 commit comments

Comments
 (0)