Skip to content

Commit fce5d6d

Browse files
gururaj1512kgryte
andauthored
test: add tests for Title constructor and properties
PR-URL: #10031 (review) Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 61608b7 commit fce5d6d

31 files changed

+4955
-2
lines changed

lib/node_modules/@stdlib/plot/vega/title/ctor/lib/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* limitations under the License.
1717
*/
1818

19-
/* eslint-disable no-restricted-syntax, no-invalid-this */
19+
/* eslint-disable no-restricted-syntax, no-invalid-this, stdlib/no-empty-lines-between-requires */
2020

2121
'use strict';
2222

@@ -396,7 +396,7 @@ setReadWriteAccessor( Title.prototype, 'font', getFont, setFont );
396396
*
397397
* @name fontSize
398398
* @memberof Title.prototype
399-
* @type {(string|void)}
399+
* @type {(number|void)}
400400
*
401401
* @example
402402
* var title = new Title({
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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 isString = require( '@stdlib/assert/is-string' ).isPrimitive;
28+
var Title = 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 Title, 'function', 'main export is a function' );
36+
t.end();
37+
});
38+
39+
tape( 'the constructor returns an instance which has an `align` property', function test( t ) {
40+
var v = new Title();
41+
t.strictEqual( hasOwnProp( v, 'align' ), false, 'returns expected value' );
42+
t.strictEqual( hasProp( v, 'align' ), true, 'returns expected value' );
43+
t.strictEqual( isUndefined( v.align ), true, 'returns expected value' );
44+
45+
v = new Title({
46+
'align': 'left'
47+
});
48+
t.strictEqual( isString( v.align ), true, 'returns expected value' );
49+
50+
t.end();
51+
});
52+
53+
tape( 'the constructor returns an instance having an `align` property which throws an error if set to an invalid value', function test( t ) {
54+
var values;
55+
var i;
56+
57+
values = [
58+
5,
59+
NaN,
60+
null,
61+
true,
62+
false,
63+
[],
64+
{},
65+
function noop() {}
66+
];
67+
for ( i = 0; i < values.length; i++ ) {
68+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
69+
}
70+
t.end();
71+
72+
function badValue( value ) {
73+
return function badValue() {
74+
var title = new Title();
75+
title.align = value;
76+
};
77+
}
78+
});
79+
80+
tape( 'the constructor returns an instance having an `align` property', function test( t ) {
81+
var title = new Title();
82+
t.strictEqual( isUndefined( title.align ), true, 'returns expected value' );
83+
84+
title = new Title({
85+
'align': 'left'
86+
});
87+
t.strictEqual( title.align, 'left', 'returns expected value' );
88+
89+
title = new Title({
90+
'align': 'top'
91+
});
92+
t.strictEqual( title.align, 'top', 'returns expected value' );
93+
94+
t.end();
95+
});
96+
97+
tape( 'the constructor returns an instance having an `align` property which can be set to a valid value', function test( t ) {
98+
var title = new Title();
99+
100+
title.align = 'left';
101+
t.strictEqual( title.align, 'left', 'returns expected value' );
102+
103+
title.align = 'top';
104+
t.strictEqual( title.align, 'top', 'returns expected value' );
105+
106+
t.end();
107+
});
108+
109+
tape( 'the constructor returns an instance which emits an event when the `align` property is set to a new value', function test( t ) {
110+
var title;
111+
var count;
112+
113+
title = new Title();
114+
count = 0;
115+
116+
title.on( 'change', onChange );
117+
118+
title.align = 'left';
119+
t.strictEqual( count, 1, 'returns expected value' );
120+
121+
title.align = 'top';
122+
t.strictEqual( count, 2, 'returns expected value' );
123+
124+
// Setting to the same value should not emit an event:
125+
title.align = 'top';
126+
t.strictEqual( count, 2, 'returns expected value' );
127+
128+
t.end();
129+
130+
function onChange() {
131+
count += 1;
132+
}
133+
});
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 Title = 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 Title, 'function', 'main export is a function' );
35+
t.end();
36+
});
37+
38+
tape( 'the constructor returns an instance which has an `anchor` property', function test( t ) {
39+
var v = new Title();
40+
t.strictEqual( hasOwnProp( v, 'anchor' ), false, 'returns expected value' );
41+
t.strictEqual( hasProp( v, 'anchor' ), true, 'returns expected value' );
42+
t.strictEqual( isString( v.anchor ), true, 'returns expected value' );
43+
44+
t.end();
45+
});
46+
47+
tape( 'the constructor returns an instance having an `anchor` property which throws an error if set to an invalid value', function test( t ) {
48+
var values;
49+
var i;
50+
51+
values = [
52+
'beep',
53+
'boop',
54+
'START',
55+
5,
56+
NaN,
57+
null,
58+
true,
59+
false,
60+
[],
61+
{},
62+
function noop() {}
63+
];
64+
for ( i = 0; i < values.length; i++ ) {
65+
t.throws( badValue( values[ i ] ), TypeError, 'throws an error when provided ' + values[ i ] );
66+
}
67+
t.end();
68+
69+
function badValue( value ) {
70+
return function badValue() {
71+
var title = new Title();
72+
title.anchor = value;
73+
};
74+
}
75+
});
76+
77+
tape( 'the constructor returns an instance having an `anchor` property', function test( t ) {
78+
var title = new Title();
79+
t.strictEqual( title.anchor, 'middle', 'returns expected value' );
80+
81+
title = new Title({
82+
'anchor': 'start'
83+
});
84+
t.strictEqual( title.anchor, 'start', 'returns expected value' );
85+
86+
title = new Title({
87+
'anchor': 'end'
88+
});
89+
t.strictEqual( title.anchor, 'end', 'returns expected value' );
90+
91+
t.end();
92+
});
93+
94+
tape( 'the constructor returns an instance having an `anchor` property which can be set to a valid value', function test( t ) {
95+
var title = new Title();
96+
97+
title.anchor = 'start';
98+
t.strictEqual( title.anchor, 'start', 'returns expected value' );
99+
100+
title.anchor = 'end';
101+
t.strictEqual( title.anchor, 'end', 'returns expected value' );
102+
103+
t.end();
104+
});
105+
106+
tape( 'the constructor returns an instance which emits an event when the `anchor` property is set to a new value', function test( t ) {
107+
var title;
108+
var count;
109+
110+
title = new Title();
111+
count = 0;
112+
113+
title.on( 'change', onChange );
114+
115+
title.anchor = 'start';
116+
t.strictEqual( count, 1, 'returns expected value' );
117+
118+
title.anchor = 'end';
119+
t.strictEqual( count, 2, 'returns expected value' );
120+
121+
// Setting to the same value should not emit an event:
122+
title.anchor = 'end';
123+
t.strictEqual( count, 2, 'returns expected value' );
124+
125+
t.end();
126+
127+
function onChange() {
128+
count += 1;
129+
}
130+
});

0 commit comments

Comments
 (0)