forked from ubuntu/app-center
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: ratings service and model (ubuntu#1439)
* test: add ratings_service_test * test: add ratings_model_test * test(snap_page): add missing ratings model mocks
- Loading branch information
Showing
9 changed files
with
739 additions
and
370 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import 'package:app_center/ratings.dart'; | ||
import 'package:clock/clock.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
|
||
import 'test_utils.dart'; | ||
|
||
void main() { | ||
test('init', () async { | ||
final mockService = createMockRatingsService( | ||
rating: const Rating( | ||
snapId: 'firefox', | ||
totalVotes: 1337, | ||
ratingsBand: RatingsBand.veryGood, | ||
), | ||
snapVotes: [ | ||
Vote( | ||
snapId: 'firefox', | ||
snapRevision: 42, | ||
voteUp: true, | ||
dateTime: DateTime(1970), | ||
), | ||
], | ||
); | ||
final model = RatingsModel( | ||
ratings: mockService, | ||
snapId: 'firefox', | ||
snapRevision: '42', | ||
); | ||
|
||
await model.init(); | ||
expect(model.state.hasValue, isTrue); | ||
expect( | ||
model.snapRating, | ||
equals( | ||
const Rating( | ||
snapId: 'firefox', | ||
totalVotes: 1337, | ||
ratingsBand: RatingsBand.veryGood, | ||
), | ||
), | ||
); | ||
expect(model.vote, equals(VoteStatus.up)); | ||
}); | ||
|
||
test('cast vote', () async { | ||
final mockService = createMockRatingsService(); | ||
final model = RatingsModel( | ||
ratings: mockService, | ||
snapId: 'firefox', | ||
snapRevision: '42', | ||
); | ||
|
||
await model.init(); | ||
await withClock( | ||
Clock.fixed(DateTime(1984)), | ||
() => model.castVote(VoteStatus.up), | ||
); | ||
verify( | ||
mockService.vote( | ||
Vote( | ||
dateTime: DateTime(1984), | ||
snapId: 'firefox', | ||
snapRevision: 42, | ||
voteUp: true, | ||
), | ||
), | ||
).called(1); | ||
}); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import 'package:app_center/src/ratings/exports.dart'; | ||
import 'package:app_center/src/ratings/ratings_service.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mockito/mockito.dart'; | ||
|
||
import 'test_utils.dart'; | ||
|
||
void main() { | ||
test('get rating', () async { | ||
final mockClient = createMockRatingsClient( | ||
token: 'jwt', | ||
rating: const Rating( | ||
snapId: 'firefox', | ||
totalVotes: 1337, | ||
ratingsBand: RatingsBand.veryGood, | ||
), | ||
); | ||
final service = RatingsService(mockClient, id: 'myId'); | ||
|
||
final rating = await service.getRating('firefox'); | ||
verify(mockClient.authenticate('myId')).called(1); | ||
expect( | ||
rating, | ||
equals( | ||
const Rating( | ||
snapId: 'firefox', | ||
totalVotes: 1337, | ||
ratingsBand: RatingsBand.veryGood, | ||
), | ||
), | ||
); | ||
}); | ||
|
||
test('vote', () async { | ||
final mockClient = createMockRatingsClient(token: 'jwt'); | ||
final service = RatingsService(mockClient, id: 'myId'); | ||
|
||
await service.vote( | ||
Vote( | ||
snapId: 'thunderbird', | ||
snapRevision: 42, | ||
voteUp: true, | ||
dateTime: DateTime(1970), | ||
), | ||
); | ||
verify(mockClient.authenticate('myId')).called(1); | ||
verify(mockClient.vote('thunderbird', 42, true, 'jwt')).called(1); | ||
}); | ||
|
||
test('delete', () async { | ||
final mockClient = createMockRatingsClient(token: 'jwt'); | ||
final service = RatingsService(mockClient, id: 'myId'); | ||
|
||
await service.delete(); | ||
verify(mockClient.authenticate('myId')).called(1); | ||
verify(mockClient.delete('jwt')).called(1); | ||
}); | ||
|
||
test('list my votes', () async { | ||
final mockClient = createMockRatingsClient( | ||
token: 'jwt', | ||
myVotes: [ | ||
Vote( | ||
snapId: 'testsnap', | ||
snapRevision: 1, | ||
voteUp: false, | ||
dateTime: DateTime(1984), | ||
), | ||
], | ||
); | ||
final service = RatingsService(mockClient, id: 'myId'); | ||
|
||
final votes = await service.listMyVotes('testsnap'); | ||
verify(mockClient.authenticate('myId')).called(1); | ||
expect( | ||
votes, | ||
equals( | ||
[ | ||
Vote( | ||
snapId: 'testsnap', | ||
snapRevision: 1, | ||
voteUp: false, | ||
dateTime: DateTime(1984), | ||
), | ||
], | ||
), | ||
); | ||
}); | ||
|
||
test('snap votes', () async { | ||
final mockClient = createMockRatingsClient( | ||
token: 'jwt', | ||
snapVotes: [ | ||
Vote( | ||
snapId: 'testsnap2', | ||
snapRevision: 2, | ||
voteUp: true, | ||
dateTime: DateTime(1999), | ||
), | ||
], | ||
); | ||
final service = RatingsService(mockClient, id: 'myId'); | ||
|
||
final votes = await service.getSnapVotes('testsnap2'); | ||
verify(mockClient.authenticate('myId')).called(1); | ||
expect( | ||
votes, | ||
equals( | ||
[ | ||
Vote( | ||
snapId: 'testsnap2', | ||
snapRevision: 2, | ||
voteUp: true, | ||
dateTime: DateTime(1999), | ||
), | ||
], | ||
), | ||
); | ||
}); | ||
} |
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
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
Oops, something went wrong.