Created
September 22, 2023 13:22
-
-
Save easylive1989/ff7b8e6dc989d816a4d4456f0fac1c94 to your computer and use it in GitHub Desktop.
2023鐵人賽_D8_3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
import 'new_product.dart'; | |
class MyFavorites { | |
final SharedPreferences _preferences; | |
MyFavorites(SharedPreferences preference) : _preferences = preference; | |
Future<void> add(Product product) async { | |
var favorites = getAll(); | |
favorites.add(product); | |
await _preferences.setStringList("favorites", | |
favorites.map((product) => jsonEncode(product.toJson())).toList()); | |
} | |
List<Product> getAll() { | |
return _preferences | |
.getStringList("favorites") | |
?.map((json) => Product.fromJson(jsonDecode(json))) | |
.toList() ?? | |
[]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:mockito/annotations.dart'; | |
import 'package:mockito/mockito.dart'; | |
import 'package:shared_preferences/shared_preferences.dart'; | |
import 'new_my_favorites.dart'; | |
import 'new_product.dart'; | |
import 'my_favorites_test.mocks.dart'; | |
@GenerateNiceMocks([MockSpec<SharedPreferences>()]) | |
main() { | |
test("getAll", () { | |
var mockSharedPreferences = MockSharedPreferences(); | |
when(mockSharedPreferences.getStringList("favorites")).thenReturn(['{"id":1,"type":"book"}']); | |
var myFavorites = MyFavorites(mockSharedPreferences); | |
expect(myFavorites.getAll(), [const Product(1, "book")]); | |
}); | |
test("add favorite", () { | |
var mockSharedPreferences = MockSharedPreferences(); | |
var myFavorites = MyFavorites(mockSharedPreferences); | |
myFavorites.add(const Product(1, "book")); | |
verify(mockSharedPreferences.setStringList("favorites", ['{"id":1,"type":"book"}'])); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:equatable/equatable.dart'; | |
class Product extends Equatable { | |
final int id; | |
final String type; | |
const Product(this.id, this.type); | |
factory Product.fromJson(Map<String, dynamic> json) { | |
return Product( | |
json['id'], | |
json['type'], | |
); | |
} | |
Map<String, dynamic> toJson() { | |
return { | |
'id': id, | |
'type': type, | |
}; | |
} | |
@override | |
List<Object?> get props => [id, type]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment