Skip to content

Commit d13f655

Browse files
committed
Add Date tests
1 parent 787b64c commit d13f655

File tree

4 files changed

+257
-0
lines changed

4 files changed

+257
-0
lines changed

.github/workflows/js_interop.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: package:js_interop
2+
permissions: read-all
3+
4+
on:
5+
# Run CI on pushes to the main branch and on PRs.
6+
push:
7+
branches: [ main ]
8+
paths:
9+
- '.github/workflows/js_interop.yaml'
10+
- 'js_interop/**'
11+
pull_request:
12+
paths:
13+
- '.github/workflows/js_interop.yaml'
14+
- 'js_interop/**'
15+
schedule:
16+
- cron: "0 0 * * 0"
17+
18+
defaults:
19+
run:
20+
working-directory: js_interop/
21+
22+
jobs:
23+
build:
24+
runs-on: ubuntu-latest
25+
strategy:
26+
fail-fast: false
27+
matrix:
28+
sdk: [3.9, beta, dev]
29+
test_config: ['-p chrome', '-p chrome -c dart2wasm', '-p node']
30+
31+
steps:
32+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
33+
- uses: dart-lang/setup-dart@e51d8e571e22473a2ddebf0ef8a2123f0ab2c02c
34+
with:
35+
sdk: ${{ matrix.sdk }}
36+
37+
- run: dart pub get
38+
- run: dart format --output=none --set-exit-if-changed .
39+
if: ${{ matrix.sdk == 'dev' }}
40+
- run: dart analyze --fatal-infos
41+
- run: dart test ${{ matrix.test_config }}

js_interop/dart_test.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
platforms: [chrome, node]

js_interop/pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ repository: https://github.com/dart-lang/web
55

66
environment:
77
sdk: ^3.9.0
8+
9+
dev_dependencies:
10+
test: ^1.26.0

js_interop/test/date_test.dart

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.p
4+
5+
import 'dart:js_interop';
6+
7+
import 'package:test/test.dart';
8+
9+
import 'package:js_interop/js_interop.dart';
10+
11+
final Matcher _isDate = predicate(
12+
(jsDate) => jsDate is JSObject && jsDate.isA<JSDate>(),
13+
"is a JSDate",
14+
);
15+
16+
void main() {
17+
group('constructors', () {
18+
test('nowAsDate', () => expect(JSDate.nowAsDate(), _isDate));
19+
20+
test(
21+
'fromMillisecondsSinceEpoch',
22+
() => expect(JSDate.fromMillisecondsSinceEpoch(1234567890), _isDate),
23+
);
24+
25+
test(
26+
'parseAsDate',
27+
() => expect(JSDate.parseAsDate("11 Nov 2025"), _isDate),
28+
);
29+
30+
test('from', () => expect(JSDate.from(JSDate.nowAsDate()), _isDate));
31+
32+
group('localDate', () {
33+
test(
34+
'with no optional args',
35+
() => expect(JSDate.localDate(2025, 11), _isDate),
36+
);
37+
38+
test(
39+
'with all optional args',
40+
() => expect(JSDate.localDate(2025, 11, 11, 12, 2, 15, 120), _isDate),
41+
);
42+
});
43+
44+
group('utcDate', () {
45+
test(
46+
'with no optional args',
47+
() => expect(JSDate.utcDate(2025, 11), _isDate),
48+
);
49+
50+
test(
51+
'with one optional arg',
52+
() => expect(JSDate.utcDate(2025, 11, 11), _isDate),
53+
);
54+
55+
test(
56+
'with two optional args',
57+
() => expect(JSDate.utcDate(2025, 11, 11, 12), _isDate),
58+
);
59+
60+
test(
61+
'with three optional args',
62+
() => expect(JSDate.utcDate(2025, 11, 11, 12, 2), _isDate),
63+
);
64+
65+
test(
66+
'with four optional arg',
67+
() => expect(JSDate.utcDate(2025, 11, 11, 12, 2, 15), _isDate),
68+
);
69+
70+
test(
71+
'with all optional args',
72+
() => expect(JSDate.utcDate(2025, 11, 11, 12, 2, 15, 120), _isDate),
73+
);
74+
});
75+
});
76+
77+
group('static', () {
78+
test(
79+
'nowAsMillisecondsSinceEpoch',
80+
() => expect(JSDate.nowAsMillisecondsSinceEpoch(), isA<int>()),
81+
);
82+
83+
test(
84+
'parseAsMillisecondsSinceEpoch',
85+
() => expect(
86+
JSDate.parseAsMillisecondsSinceEpoch("11 Nov 2025"),
87+
isA<int>(),
88+
),
89+
);
90+
91+
group('utcAsMillisecondsSinceEpoch', () {
92+
test(
93+
'with no optional args',
94+
() => expect(JSDate.utcAsMillisecondsSinceEpoch(2025, 11), isA<int>()),
95+
);
96+
97+
test(
98+
'with all optional args',
99+
() => expect(
100+
JSDate.utcAsMillisecondsSinceEpoch(2025, 11, 11, 12, 2, 15, 120),
101+
isA<int>(),
102+
),
103+
);
104+
});
105+
});
106+
107+
group('instance', () {
108+
late JSDate date;
109+
setUp(() => date = JSDate.nowAsDate());
110+
111+
test('date', () {
112+
date.date = 12;
113+
expect(date.date, equals(12));
114+
});
115+
116+
test('day', () => expect(date.day, isA<int>()));
117+
118+
test('fullYear', () {
119+
date.fullYear = 2026;
120+
expect(date.fullYear, equals(2026));
121+
});
122+
123+
test('hours', () {
124+
date.hours = 13;
125+
expect(date.hours, equals(13));
126+
});
127+
128+
test('milliseconds', () {
129+
date.milliseconds = 123;
130+
expect(date.milliseconds, equals(123));
131+
});
132+
133+
test('minutes', () {
134+
date.minutes = 5;
135+
expect(date.minutes, equals(5));
136+
});
137+
138+
test('month', () {
139+
date.month = 1;
140+
expect(date.month, equals(1));
141+
});
142+
143+
test('seconds', () {
144+
date.seconds = 5;
145+
expect(date.seconds, equals(5));
146+
});
147+
148+
test('time', () {
149+
date.time = 1234567890;
150+
expect(date.time, equals(1234567890));
151+
});
152+
153+
test('timezoneOffset', () => expect(date.timezoneOffset, isA<int>()));
154+
155+
test('utcDate', () {
156+
date.utcDate = 12;
157+
expect(date.utcDate, equals(12));
158+
});
159+
160+
test('utcFullYear', () {
161+
date.utcFullYear = 2026;
162+
expect(date.utcFullYear, equals(2026));
163+
});
164+
165+
test('utcHours', () {
166+
date.utcHours = 13;
167+
expect(date.utcHours, equals(13));
168+
});
169+
170+
test('utcMilliseconds', () {
171+
date.utcMilliseconds = 123;
172+
expect(date.utcMilliseconds, equals(123));
173+
});
174+
175+
test('utcMinutes', () {
176+
date.utcMinutes = 5;
177+
expect(date.utcMinutes, equals(5));
178+
});
179+
180+
test('utcMonth', () {
181+
date.utcMonth = 1;
182+
expect(date.utcMonth, equals(1));
183+
});
184+
185+
test('utcSeconds', () {
186+
date.utcSeconds = 5;
187+
expect(date.utcSeconds, equals(5));
188+
});
189+
190+
test('toDateString', () => expect(date.toDateString(), isA<String>()));
191+
192+
test('toIsoString:', () => expect(date.toIsoString(), isA<String>()));
193+
194+
test('toTimeString:', () => expect(date.toTimeString(), isA<String>()));
195+
196+
test('toUtcString:', () => expect(date.toUtcString(), isA<String>()));
197+
198+
test('toDart', () {
199+
var dartDate = date.toDart;
200+
expect(dartDate, isA<DateTime>());
201+
expect(dartDate.timeZoneName, equals(DateTime.now().timeZoneName));
202+
});
203+
204+
test('toDartUtc', () {
205+
var dartDate = date.toDartUtc;
206+
expect(dartDate, isA<DateTime>());
207+
expect(dartDate.timeZoneName, equals("UTC"));
208+
});
209+
210+
test('from Dart', () => expect(DateTime.now().toJS, _isDate));
211+
});
212+
}

0 commit comments

Comments
 (0)