Skip to content

Commit f779434

Browse files
committed
Add Date tests
1 parent b7a9e6e commit f779434

File tree

4 files changed

+257
-0
lines changed

4 files changed

+257
-0
lines changed

.github/workflows/js_interop.yaml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 }}
42+
43+
# Validate the 'dart fix' metadata.
44+
- run: dart fix --compare-to-golden test_fixes

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: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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('parseAsDate', () => expect(JSDate.parseAsDate("11 Nov 2025"), _isDate));
26+
27+
test('from', () => expect(JSDate.from(JSDate.nowAsDate()), _isDate));
28+
29+
group('localDate', () {
30+
test(
31+
'with no optional args',
32+
() => expect(JSDate.localDate(2025, 11), _isDate),
33+
);
34+
35+
test(
36+
'with all optional args',
37+
() => expect(JSDate.localDate(2025, 11, 11, 12, 2, 15, 120), _isDate),
38+
);
39+
});
40+
41+
group('utcDate', () {
42+
test(
43+
'with no optional args',
44+
() => expect(JSDate.utcDate(2025, 11), _isDate),
45+
);
46+
47+
test(
48+
'with one optional arg',
49+
() => expect(JSDate.utcDate(2025, 11, 11), _isDate),
50+
);
51+
52+
test(
53+
'with two optional args',
54+
() => expect(JSDate.utcDate(2025, 11, 11, 12), _isDate),
55+
);
56+
57+
test(
58+
'with three optional args',
59+
() => expect(JSDate.utcDate(2025, 11, 11, 12, 2), _isDate),
60+
);
61+
62+
test(
63+
'with four optional arg',
64+
() => expect(JSDate.utcDate(2025, 11, 11, 12, 2, 15), _isDate),
65+
);
66+
67+
test(
68+
'with all optional args',
69+
() => expect(JSDate.utcDate(2025, 11, 11, 12, 2, 15, 120), _isDate),
70+
);
71+
});
72+
});
73+
74+
group('static', () {
75+
test(
76+
'nowAsMillisecondsSinceEpoch',
77+
() => expect(JSDate.nowAsMillisecondsSinceEpoch(), isA<int>()),
78+
);
79+
80+
test(
81+
'parseAsMillisecondsSinceEpoch',
82+
() => expect(
83+
JSDate.parseAsMillisecondsSinceEpoch("11 Nov 2025"),
84+
isA<int>(),
85+
),
86+
);
87+
88+
group('utcAsMillisecondsSinceEpoch', () {
89+
test(
90+
'with no optional args',
91+
() => expect(JSDate.utcAsMillisecondsSinceEpoch(2025, 11), isA<int>()),
92+
);
93+
94+
test(
95+
'with all optional args',
96+
() => expect(
97+
JSDate.utcAsMillisecondsSinceEpoch(2025, 11, 11, 12, 2, 15, 120),
98+
isA<int>(),
99+
),
100+
);
101+
});
102+
});
103+
104+
group('instance', () {
105+
late JSDate date;
106+
setUp(() => date = JSDate.nowAsDate());
107+
108+
test('date', () {
109+
date.date = 12;
110+
expect(date.date, equals(12));
111+
});
112+
113+
test('day', () => expect(date.day, isA<int>()));
114+
115+
test('fullYear', () {
116+
date.fullYear = 2026;
117+
expect(date.fullYear, equals(2026));
118+
});
119+
120+
test('hours', () {
121+
date.hours = 13;
122+
expect(date.hours, equals(13));
123+
});
124+
125+
test('milliseconds', () {
126+
date.milliseconds = 123;
127+
expect(date.milliseconds, equals(123));
128+
});
129+
130+
test('minutes', () {
131+
date.minutes = 5;
132+
expect(date.minutes, equals(5));
133+
});
134+
135+
test('month', () {
136+
date.month = 1;
137+
expect(date.month, equals(1));
138+
});
139+
140+
test('seconds', () {
141+
date.seconds = 5;
142+
expect(date.seconds, equals(5));
143+
});
144+
145+
test('time', () {
146+
date.time = 1234567890;
147+
expect(date.time, equals(1234567890));
148+
});
149+
150+
test('timezoneOffset', () => expect(date.timezoneOffset, isA<int>()));
151+
152+
test('utcDate', () {
153+
date.utcDate = 12;
154+
expect(date.utcDate, equals(12));
155+
});
156+
157+
test('utcFullYear', () {
158+
date.utcFullYear = 2026;
159+
expect(date.utcFullYear, equals(2026));
160+
});
161+
162+
test('utcHours', () {
163+
date.utcHours = 13;
164+
expect(date.utcHours, equals(13));
165+
});
166+
167+
test('utcMilliseconds', () {
168+
date.utcMilliseconds = 123;
169+
expect(date.utcMilliseconds, equals(123));
170+
});
171+
172+
test('utcMinutes', () {
173+
date.utcMinutes = 5;
174+
expect(date.utcMinutes, equals(5));
175+
});
176+
177+
test('utcMonth', () {
178+
date.utcMonth = 1;
179+
expect(date.utcMonth, equals(1));
180+
});
181+
182+
test('utcSeconds', () {
183+
date.utcSeconds = 5;
184+
expect(date.utcSeconds, equals(5));
185+
});
186+
187+
test('toDateString', () => expect(date.toDateString(), isA<String>()));
188+
189+
test('toIsoString:', () => expect(date.toIsoString(), isA<String>()));
190+
191+
test('toTimeString:', () => expect(date.toTimeString(), isA<String>()));
192+
193+
test('toUtcString:', () => expect(date.toUtcString(), isA<String>()));
194+
195+
test('toDart', () {
196+
var dartDate = date.toDart;
197+
expect(dartDate, isA<DateTime>());
198+
expect(dartDate.timeZoneName, equals(DateTime.now().timeZoneName));
199+
});
200+
201+
test('toDartUtc', () {
202+
var dartDate = date.toDartUtc;
203+
expect(dartDate, isA<DateTime>());
204+
expect(dartDate.timeZoneName, equals("UTC"));
205+
});
206+
207+
test('from Dart', () => expect(DateTime.now().toJS, _isDate));
208+
});
209+
}

0 commit comments

Comments
 (0)