Skip to content

Instantly share code, notes, and snippets.

@alfianyusufabdullah
Last active June 21, 2019 02:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alfianyusufabdullah/45af7a56bbf104dddae4d41af0df9ad2 to your computer and use it in GitHub Desktop.
Save alfianyusufabdullah/45af7a56bbf104dddae4d41af0df9ad2 to your computer and use it in GitHub Desktop.
Future<List<Meals>> loadMealsFromNetwork(String category) async {
var result = await httpRequest("$ENDPOINT_FILTER$category");
return generateMealList(result);
}
Future<Meals> loadMealsDetailFromNetwork(String id) async {
var result = await httpRequest("$ENDPOINT_LOCKUP$id");
return generateMeal(result);
}
Future<List<Meals>> searchMealFromNetwork(String name) async {
var result = await httpRequest("$ENDPOINT_SEARCH$name");
return generateMealList(result);
}
import 'package:test_api/test_api.dart';
import 'package:mockito/mockito.dart';
import 'package:http/http.dart' as http;
class ApiTest extends Mock implements http.Client {}
main() {
final client = ApiTest();
group("Request Dessert test", () {
test("should request complete", () async {
when(
client.get(
"https://www.themealdb.com/api/json/v1/1/filter.php?c=Dessert"),
).thenAnswer(
(_) async => http.Response(generateMealList([]).toString(), 200),
);
expect(await loadMealsFromNetwork("Dessert"), isA<List<Meals>>());
});
test("should request failed", () async {
when(
client.get(
"https://www.themealdb.com/api/json/v1/1/filter.php?c=Dessert"),
).thenAnswer(
(_) async => http.Response(generateMealList([]).toString(), 404),
);
expect(await loadMealsFromNetwork("Dessert"), isA<List<Meals>>());
});
});
group("Request Seafood test", () {
test("should request complete", () async {
when(
client.get(
"https://www.themealdb.com/api/json/v1/1/filter.php?c=Seafood"),
).thenAnswer(
(_) async => http.Response(generateMealList([]).toString(), 200),
);
expect(await loadMealsFromNetwork("Seafood"), isA<List<Meals>>());
});
test("should request failed", () async {
when(
client.get(
"https://www.themealdb.com/api/json/v1/1/filter.php?c=Seafood"),
).thenAnswer(
(_) async => http.Response(generateMealList([]).toString(), 404),
);
expect(await loadMealsFromNetwork("Seafood"), isA<List<Meals>>());
});
});
group("Request detail meal test", () {
test("should request complete", () async {
when(
client
.get("https://www.themealdb.com/api/json/v1/1/lookup.php?i=52772"),
).thenAnswer(
(_) async => http.Response(generateMeal([]).toString(), 200),
);
expect(await loadMealsDetailFromNetwork("52772"), isA<Meals>());
});
test("should request failed", () async {
when(
client
.get("https://www.themealdb.com/api/json/v1/1/lookup.php?i=52772"),
).thenAnswer(
(_) async => http.Response(generateMeal([]).toString(), 404),
);
expect(await loadMealsDetailFromNetwork("52772"), isA<Meals>());
});
});
group("Request search meal test", () {
test("should request complete", () async {
when(
client.get(
"https://www.themealdb.com/api/json/v1/1/search.php?s=Arrabiata"),
).thenAnswer(
(_) async => http.Response(generateMealList([]).toString(), 200),
);
expect(await loadMealsFromNetwork("Arrabiata"), isA<List<Meals>>());
});
test("should request failed", () async {
when(
client
.get("https://www.themealdb.com/api/json/v1/1/search.php?s=Mantan"),
).thenAnswer(
(_) async => http.Response(generateMealList([]).toString(), 404),
);
expect(await loadMealsFromNetwork("Mantan"), isA<List<Meals>>());
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment