Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and test Leaderboard #443

Merged
merged 14 commits into from
May 17, 2023
12 changes: 6 additions & 6 deletions test/core/network/network_request_executor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,32 @@ void main() {

test('should return [ServerFailure] if api call fails', () async {
// arrange
final tResponse = responseFromStatusCode(500, body: '');
final testResponse = responseFromStatusCode(500, body: '');
fremartini marked this conversation as resolved.
Show resolved Hide resolved

// act
final actual = await executor(() async => tResponse);
final actual = await executor(() async => testResponse);

// assert
expect(actual, const Left(ServerFailure('')));
});

test('should return response body if api call succeeds', () async {
// arrange
final tResponse = responseFromStatusCode(200, body: 'some string');
final testResponse = responseFromStatusCode(200, body: 'some string');

// act
final actual = await executor(() async => tResponse);
final actual = await executor(() async => testResponse);

// assert
expect(actual, const Right('some string'));
});

test('should return [ServerFailure] if call throws [Exception]', () async {
// arrange
final tException = Exception('some error');
final testException = Exception('some error');

// act
final actual = await executor(() async => throw tException);
final actual = await executor(() async => throw testException);

// assert
expect(actual, const Left(ConnectionFailure()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void main() {
group(
'getContributors',
() {
const tContributors = [
const testContributors = [
Contributor(
name: 'name',
avatarUrl: 'avatarUrl',
Expand All @@ -38,10 +38,10 @@ void main() {
'should emit [Loaded] with data when use case succeeds',
build: () => cubit,
setUp: () => when(fetchContributors(any))
.thenAnswer((_) async => const Right(tContributors)),
.thenAnswer((_) async => const Right(testContributors)),
act: (_) => cubit.getContributors(),
expect: () => [
const ContributorLoaded(tContributors),
const ContributorLoaded(testContributors),
],
);

Expand Down
20 changes: 10 additions & 10 deletions test/features/leaderboard/domain/usecases/get_leaderboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void main() {
});

group('call', () {
const tUserLeaderboard = LeaderboardUser(
const testUserLeaderboard = LeaderboardUser(
id: 0,
rank: 0,
score: 0,
Expand All @@ -44,7 +44,7 @@ void main() {
// arrange
when(remoteDataSource.getLeaderboardUser(any)).thenAnswer(
(_) async => const Right(
tUserLeaderboard,
testUserLeaderboard,
),
);
when(remoteDataSource.getLeaderboard(any, any))
Expand All @@ -59,7 +59,7 @@ void main() {
test('should return [Right] if api calls succeed', () async {
// arrange
when(remoteDataSource.getLeaderboardUser(any)).thenAnswer(
(_) async => const Right(tUserLeaderboard),
(_) async => const Right(testUserLeaderboard),
);
when(remoteDataSource.getLeaderboard(any, any))
.thenAnswer((_) async => const Right([]));
Expand All @@ -72,7 +72,7 @@ void main() {
actual.map(
(response) => expect(
response,
[tUserLeaderboard],
[testUserLeaderboard],
),
);
});
Expand All @@ -81,18 +81,18 @@ void main() {
group('buildLeaderboard', () {
test('should highlight user if present in leaderboard', () {
// arrange
const tUser = LeaderboardUser(
const testUser = LeaderboardUser(
id: 0,
rank: 0,
score: 0,
name: 'name',
highlight: false,
);

final tLeaderboard = [tUser];
final testLeaderboard = [testUser];

// act
final actual = usecase.buildLeaderboard(tLeaderboard, tUser);
final actual = usecase.buildLeaderboard(testLeaderboard, testUser);

// assert
expect(actual, [
Expand All @@ -108,18 +108,18 @@ void main() {

test('should append user if not present in leaderboard', () {
// arrange
const tUser = LeaderboardUser(
const testUser = LeaderboardUser(
id: 0,
rank: 0,
score: 0,
name: 'name',
highlight: false,
);

final List<LeaderboardUser> tLeaderboard = [];
final List<LeaderboardUser> testLeaderboard = [];

// act
final actual = usecase.buildLeaderboard(tLeaderboard, tUser);
final actual = usecase.buildLeaderboard(testLeaderboard, testUser);

// assert
expect(actual, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
cubit = LeaderboardCubit(getLeaderboard: getLeaderboard);
});

const tLeaderboard = [
const testLeaderboard = [
LeaderboardUserModel(
id: 0,
name: 'name',
Expand All @@ -34,13 +34,13 @@ void main() {
blocTest(
'should emit [LeaderboardLoading, LeaderboardLoaded] with correct filter',
setUp: () => when(getLeaderboard(any))
.thenAnswer((_) async => const Right(tLeaderboard)),
.thenAnswer((_) async => const Right(testLeaderboard)),
build: () => cubit,
act: (_) => cubit.setFilter(LeaderboardFilter.semester),
expect: () => [
const LeaderboardLoading(filter: LeaderboardFilter.semester),
const LeaderboardLoaded(
tLeaderboard,
testLeaderboard,
filter: LeaderboardFilter.semester,
),
],
Expand All @@ -64,12 +64,12 @@ void main() {
blocTest(
'should emit [LeaderboardLoaded] when usecase succeeds',
setUp: () => when(getLeaderboard(any))
.thenAnswer((_) async => const Right(tLeaderboard)),
.thenAnswer((_) async => const Right(testLeaderboard)),
build: () => cubit,
act: (_) => cubit.loadLeaderboard(),
expect: () => [
const LeaderboardLoaded(
tLeaderboard,
testLeaderboard,
filter: LeaderboardFilter.month,
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ void main() {
group('getOpeningHours', () {
test('should call executor', () async {
// arrange
final List<OpeningHoursDTO> tOpeningHours = [];
final List<OpeningHoursDTO> testOpeningHours = [];

when(executor<List<OpeningHoursDTO>>(any)).thenAnswer(
(_) async => Right(tOpeningHours),
(_) async => Right(testOpeningHours),
);

// act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ void main() {
'should return [Right<List<Receipt>>] if api calls succeed',
() async {
// arrange
final tSwipedReceipt = SwipeReceipt(
final testSwipedReceipt = SwipeReceipt(
productName: 'productName',
timeUsed: DateTime.parse('2023-04-23'),
id: 0,
);

final tPurchasedReceipt = PurchaseReceipt(
final testPurchasedReceipt = PurchaseReceipt(
productName: 'productName',
timeUsed: DateTime.parse('2023-04-24'), // note this is a day later
id: 0,
Expand All @@ -68,12 +68,12 @@ void main() {

when(remoteDataSource.getUsersUsedTicketsReceipts()).thenAnswer(
(_) async => Right([
tSwipedReceipt,
testSwipedReceipt,
]),
);
when(remoteDataSource.getUserPurchasesReceipts()).thenAnswer(
(_) async => Right([
tPurchasedReceipt,
testPurchasedReceipt,
]),
);

Expand All @@ -85,8 +85,8 @@ void main() {
(response) => expect(
response,
[
tPurchasedReceipt,
tSwipedReceipt,
testPurchasedReceipt,
testSwipedReceipt,
], // note that it is sorted from oldest --> newest
),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void main() {
});

group('filterReceipts', () {
final tReceipts = [
final testReceipts = [
SwipeReceipt(
id: 1,
productName: 'Coffee',
Expand All @@ -71,11 +71,11 @@ void main() {
blocTest<ReceiptCubit, ReceiptState>(
'should emit new state with filter applied',
build: () => cubit,
seed: () => ReceiptState(receipts: tReceipts),
seed: () => ReceiptState(receipts: testReceipts),
act: (_) => cubit.filterReceipts(ReceiptFilterCategory.purchases),
expect: () => [
ReceiptState(
receipts: tReceipts,
receipts: testReceipts,
filteredReceipts: const [],
filterBy: ReceiptFilterCategory.purchases,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ void main() {
);
});
group('useTicket', () {
final tReceipt = PlaceholderReceipt();
final testReceipt = PlaceholderReceipt();

blocTest<TicketsCubit, TicketsState>(
'should not emit new state when state is not [Loaded]',
build: () => cubit,
setUp: () {
when(loadTickets(any)).thenAnswer((_) async => const Right([]));
when(consumeTicket(any)).thenAnswer((_) async => Right(tReceipt));
when(consumeTicket(any)).thenAnswer((_) async => Right(testReceipt));
},
act: (cubit) => cubit.useTicket(0),
expect: () => [],
Expand All @@ -71,7 +71,7 @@ void main() {
build: () => cubit,
setUp: () {
when(loadTickets(any)).thenAnswer((_) async => const Right([]));
when(consumeTicket(any)).thenAnswer((_) async => Right(tReceipt));
when(consumeTicket(any)).thenAnswer((_) async => Right(testReceipt));
},
act: (_) async {
await cubit.getTickets();
Expand All @@ -81,7 +81,7 @@ void main() {
skip: 2,
expect: () => [
const TicketUsing([]),
TicketUsed(tReceipt, const []),
TicketUsed(testReceipt, const []),
const TicketsLoaded([]),
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void main() {
);
});

const tUserModel = UserModel(
const testUserModel = UserModel(
id: 0,
name: 'name',
email: 'email',
Expand Down Expand Up @@ -84,7 +84,7 @@ void main() {
final actual = await dataSource.getUser();

// assert
expect(actual, const Right(tUserModel));
expect(actual, const Right(testUserModel));
});
});

Expand Down Expand Up @@ -126,7 +126,7 @@ void main() {
final actual = await dataSource.updateUserDetails(const UpdateUser());

// assert
expect(actual, const Right(tUserModel));
expect(actual, const Right(testUserModel));
});
});

Expand Down
20 changes: 10 additions & 10 deletions test/features/user/presentation/cubit/user_cubit_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void main() {
);
});

const tUser = User(
const testUser = User(
id: 0,
name: 'name',
email: 'email',
Expand Down Expand Up @@ -65,12 +65,12 @@ void main() {
'should emit [Loading, Loaded] when use case succeeds',
build: () => cubit,
setUp: () => when(getUser(any)).thenAnswer(
(_) async => const Right(tUser),
(_) async => const Right(testUser),
),
act: (_) => cubit.fetchUserDetails(),
expect: () => [
UserLoading(),
UserLoaded(user: tUser),
UserLoaded(user: testUser),
],
);
});
Expand All @@ -96,7 +96,7 @@ void main() {
'should not update state if state is [Updating]',
build: () => cubit,
act: (_) => cubit.updateUser(const UpdateUser()),
seed: () => UserUpdating(user: tUser),
seed: () => UserUpdating(user: testUser),
expect: () => [],
);

Expand All @@ -109,9 +109,9 @@ void main() {
),
),
act: (_) => cubit.updateUser(const UpdateUser()),
seed: () => UserLoaded(user: tUser),
seed: () => UserLoaded(user: testUser),
expect: () => [
UserUpdating(user: tUser),
UserUpdating(user: testUser),
UserError('some error'),
],
);
Expand All @@ -120,13 +120,13 @@ void main() {
'should emit [Updating, Loaded] if use case succeeds',
build: () => cubit,
setUp: () => when(updateUserDetails(any)).thenAnswer(
(_) async => const Right(tUser),
(_) async => const Right(testUser),
),
act: (_) => cubit.updateUser(const UpdateUser()),
seed: () => UserLoaded(user: tUser),
seed: () => UserLoaded(user: testUser),
expect: () => [
UserUpdating(user: tUser),
UserLoaded(user: tUser),
UserUpdating(user: testUser),
UserLoaded(user: testUser),
],
);
});
Expand Down