Skip to content

Commit dc54472

Browse files
committed
Added unit tests
1 parent 11b9be8 commit dc54472

File tree

5 files changed

+137
-11
lines changed

5 files changed

+137
-11
lines changed

pkgs/dartpad_ui/lib/local_storage.dart

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,12 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:web/web.dart' as web;
5+
import 'local_storage/stub.dart'
6+
if (dart.library.js_util) 'local_storage/web.dart';
67

7-
const _userInputKey = 'user_';
8+
abstract class LocalStorage {
9+
static LocalStorage instance = LocalStorageImpl();
810

9-
class LocalStorage {
10-
static final instance = LocalStorage._();
11-
12-
LocalStorage._();
13-
14-
void saveUserCode(String code) =>
15-
web.window.localStorage.setItem(_userInputKey, code);
16-
17-
String? getUserCode() => web.window.localStorage.getItem(_userInputKey);
11+
void saveUserCode(String code);
12+
String? getUserCode();
1813
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2024, 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.
4+
5+
import '../local_storage.dart';
6+
import '../utils.dart';
7+
8+
class LocalStorageImpl extends LocalStorage {
9+
String? _code;
10+
11+
@override
12+
void saveUserCode(String code) => _code = code;
13+
14+
@override
15+
String? getUserCode() => _code?.nullIfEmpty;
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (c) 2024, 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.
4+
5+
import 'package:web/web.dart' as web;
6+
7+
import '../local_storage.dart';
8+
import '../utils.dart';
9+
10+
const _userInputKey = 'user_';
11+
12+
class LocalStorageImpl extends LocalStorage {
13+
@override
14+
void saveUserCode(String code) =>
15+
web.window.localStorage.setItem(_userInputKey, code);
16+
17+
@override
18+
String? getUserCode() =>
19+
web.window.localStorage.getItem(_userInputKey)?.nullIfEmpty;
20+
}

pkgs/dartpad_ui/lib/utils.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,7 @@ enum MessageState {
184184
showing,
185185
closing;
186186
}
187+
188+
extension StringUtils on String {
189+
String? get nullIfEmpty => isEmpty ? null : this;
190+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) 2024, 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.
4+
5+
import 'dart:convert';
6+
7+
import 'package:dartpad_ui/gists.dart';
8+
import 'package:dartpad_ui/local_storage.dart';
9+
import 'package:dartpad_ui/model.dart';
10+
import 'package:dartpad_ui/samples.g.dart';
11+
import 'package:dartpad_ui/utils.dart';
12+
import 'package:test/test.dart';
13+
14+
import 'gists_test.dart';
15+
16+
String getFallback() =>
17+
LocalStorage.instance.getUserCode() ?? Samples.defaultSnippet();
18+
19+
void main() {
20+
const channel = Channel.stable;
21+
group('Autosave:', () {
22+
test('empty content is treated as null', () {
23+
expect(''.nullIfEmpty, isNull);
24+
25+
LocalStorage.instance.saveUserCode('non-empty');
26+
expect(LocalStorage.instance.getUserCode(), isNotNull);
27+
28+
LocalStorage.instance.saveUserCode('');
29+
expect(LocalStorage.instance.getUserCode(), isNull);
30+
});
31+
32+
test('null content means sample snippet is shown', () async {
33+
final model = AppModel();
34+
final services = AppServices(model, channel);
35+
LocalStorage.instance.saveUserCode('');
36+
expect(LocalStorage.instance.getUserCode(), isNull);
37+
38+
await services.performInitialLoad(
39+
fallbackSnippet: getFallback(),
40+
);
41+
expect(model.sourceCodeController.text, equals(Samples.defaultSnippet()));
42+
});
43+
44+
test('non-null content is shown', () async {
45+
const sample = 'Hello, World!';
46+
final model = AppModel();
47+
final services = AppServices(model, channel);
48+
LocalStorage.instance.saveUserCode(sample);
49+
expect(LocalStorage.instance.getUserCode(), equals(sample));
50+
51+
await services.performInitialLoad(
52+
fallbackSnippet: getFallback(),
53+
);
54+
expect(model.sourceCodeController.text, equals(sample));
55+
});
56+
57+
group('content is not shown with', () {
58+
const sample = 'Hello, World!';
59+
LocalStorage.instance.saveUserCode(sample);
60+
// Not testing flutterSampleId to avoid breaking when the Flutter docs change
61+
62+
test('Gist', () async {
63+
final model = AppModel();
64+
final services = AppServices(model, channel);
65+
expect(LocalStorage.instance.getUserCode(), equals(sample));
66+
67+
// From gists_tests.dart
68+
const gistId = 'd3bd83918d21b6d5f778bdc69c3d36d6';
69+
await services.performInitialLoad(
70+
fallbackSnippet: getFallback(),
71+
gistId: gistId,
72+
);
73+
expect(model.sourceCodeController.text, isNot(equals(sample)));
74+
});
75+
76+
test('sample', () async {
77+
final model = AppModel();
78+
final services = AppServices(model, channel);
79+
expect(LocalStorage.instance.getUserCode(), equals(sample));
80+
81+
// From samples_test.dart
82+
const sampleId = 'dart';
83+
await services.performInitialLoad(
84+
fallbackSnippet: getFallback(),
85+
sampleId: sampleId,
86+
);
87+
expect(model.sourceCodeController.text, isNot(equals(sample)));
88+
});
89+
});
90+
});
91+
}

0 commit comments

Comments
 (0)