initial commit (migrated)
This commit is contained in:
commit
b594facb51
143 changed files with 11057 additions and 0 deletions
|
|
@ -0,0 +1,95 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:kuwot/core/domain/no_params.dart';
|
||||
import 'package:kuwot/core/error/failure.dart';
|
||||
import 'package:kuwot/features/quote/domain/entities/background_image.dart';
|
||||
import 'package:kuwot/features/quote/domain/use_cases/get_background_images.dart';
|
||||
import 'package:kuwot/features/quote/presentation/bloc/background_images_bloc.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockGetBackgroundImages extends Mock implements GetBackgroundImages {}
|
||||
|
||||
class FakeNoParams extends Fake implements NoParams {}
|
||||
|
||||
void main() {
|
||||
late MockGetBackgroundImages mockGetBackgroundImages;
|
||||
late BackgroundImagesBloc backgroundImagesBloc;
|
||||
|
||||
setUpAll(() {
|
||||
registerFallbackValue(FakeNoParams());
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
mockGetBackgroundImages = MockGetBackgroundImages();
|
||||
backgroundImagesBloc = BackgroundImagesBloc(
|
||||
getBackgroundImages: mockGetBackgroundImages,
|
||||
);
|
||||
});
|
||||
|
||||
test('initial state is BackgroundImagesInitial', () {
|
||||
// assert
|
||||
expect(backgroundImagesBloc.state, const BackgroundImagesInitialState());
|
||||
});
|
||||
|
||||
group('GetBackgroundImages', () {
|
||||
test(
|
||||
'should get background photos from GetBackgroundImages use case',
|
||||
() async {
|
||||
// arrange
|
||||
when(
|
||||
() => mockGetBackgroundImages(any()),
|
||||
).thenAnswer((_) async => right(const <BackgroundImage>[]));
|
||||
|
||||
// act
|
||||
backgroundImagesBloc.add(const GetBackgroundImagesEvent());
|
||||
await untilCalled(() => mockGetBackgroundImages(any()));
|
||||
|
||||
// assert
|
||||
verify(() => mockGetBackgroundImages(any())).called(1);
|
||||
verifyNoMoreInteractions(mockGetBackgroundImages);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'should emit [BackgroundImagesLoading, BackgroundImagesLoaded] when data is gotten successfully',
|
||||
() async {
|
||||
// arrange
|
||||
const tBackgroundImages = <BackgroundImage>[];
|
||||
when(
|
||||
() => mockGetBackgroundImages(any()),
|
||||
).thenAnswer((_) async => right(tBackgroundImages));
|
||||
|
||||
// assert later
|
||||
final expected = [
|
||||
const BackgroundImagesLoadingState(),
|
||||
const BackgroundImagesLoadedState(tBackgroundImages),
|
||||
];
|
||||
expectLater(backgroundImagesBloc.stream, emitsInOrder(expected));
|
||||
|
||||
// act
|
||||
backgroundImagesBloc.add(const GetBackgroundImagesEvent());
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'should emit [BackgroundImagesLoading, BackgroundImagesError] when getting data fails',
|
||||
() async {
|
||||
// arrange
|
||||
const tFailure = UnknownFailure(message: 'Unknown Failure');
|
||||
when(
|
||||
() => mockGetBackgroundImages(any()),
|
||||
).thenAnswer((_) async => left(tFailure));
|
||||
|
||||
// assert later
|
||||
final expected = [
|
||||
const BackgroundImagesLoadingState(),
|
||||
BackgroundImagesErrorState(message: tFailure.message),
|
||||
];
|
||||
expectLater(backgroundImagesBloc.stream, emitsInOrder(expected));
|
||||
|
||||
// act
|
||||
backgroundImagesBloc.add(const GetBackgroundImagesEvent());
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
180
test/features/quote/presentation/bloc/quote_bloc_test.dart
Normal file
180
test/features/quote/presentation/bloc/quote_bloc_test.dart
Normal file
|
|
@ -0,0 +1,180 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:kuwot/core/data/local/config.dart';
|
||||
import 'package:kuwot/core/data/local/translation_target_config.dart';
|
||||
import 'package:kuwot/core/error/failure.dart';
|
||||
import 'package:kuwot/features/quote/domain/entities/quote.dart';
|
||||
import 'package:kuwot/features/quote/domain/use_cases/get_quote.dart';
|
||||
import 'package:kuwot/features/quote/domain/use_cases/get_translated_quote.dart';
|
||||
import 'package:kuwot/features/quote/presentation/bloc/quote_bloc.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockGetQuote extends Mock implements GetQuote {}
|
||||
|
||||
class MockGetTranslatedQuote extends Mock implements GetTranslatedQuote {}
|
||||
|
||||
class MockTranslationTargetConfig extends Mock
|
||||
implements Config<TranslationTarget> {}
|
||||
|
||||
class FakeQuoteParams extends Fake implements GetQuoteParams {}
|
||||
|
||||
class FakeTranslatedQuoteParams extends Fake
|
||||
implements GetTranslatedQuoteParams {}
|
||||
|
||||
void main() {
|
||||
late MockGetQuote mockGetQuote;
|
||||
late MockGetTranslatedQuote mockGetTranslatedQuote;
|
||||
late MockTranslationTargetConfig mockTranslationTargetConfig;
|
||||
late QuoteBloc quoteBloc;
|
||||
|
||||
setUpAll(() {
|
||||
registerFallbackValue(FakeQuoteParams());
|
||||
registerFallbackValue(FakeTranslatedQuoteParams());
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
mockGetQuote = MockGetQuote();
|
||||
mockGetTranslatedQuote = MockGetTranslatedQuote();
|
||||
mockTranslationTargetConfig = MockTranslationTargetConfig();
|
||||
|
||||
quoteBloc = QuoteBloc(
|
||||
getQuote: mockGetQuote,
|
||||
getTranslatedQuote: mockGetTranslatedQuote,
|
||||
translationTargetConfig: mockTranslationTargetConfig,
|
||||
);
|
||||
});
|
||||
|
||||
const tTranslationTarget = TranslationTarget(id: 'en', name: 'English');
|
||||
const tQuote = Quote(id: 1, author: 'author', body: 'text');
|
||||
const tFailure = UnknownFailure(message: 'Unknown Failure');
|
||||
|
||||
test('initial state is QuoteInitial', () {
|
||||
// assert
|
||||
expect(quoteBloc.state, const QuoteInitialState());
|
||||
});
|
||||
|
||||
group('GetQuote', () {
|
||||
test('should get quote from GetQuote use case', () async {
|
||||
// arrange
|
||||
when(() => mockGetQuote(any())).thenAnswer((_) async => right(tQuote));
|
||||
when(
|
||||
() => mockTranslationTargetConfig.get(),
|
||||
).thenAnswer((_) async => tTranslationTarget);
|
||||
|
||||
// act
|
||||
quoteBloc.add(const GetQuoteEvent());
|
||||
await untilCalled(() => mockTranslationTargetConfig.get());
|
||||
await untilCalled(() => mockGetQuote(any()));
|
||||
|
||||
// assert
|
||||
verify(() => mockGetQuote(any()));
|
||||
verifyNoMoreInteractions(mockGetQuote);
|
||||
});
|
||||
|
||||
test(
|
||||
'should emit [QuoteLoading, QuoteLoaded] when data is gotten successfully',
|
||||
() async {
|
||||
// arrange
|
||||
when(() => mockGetQuote(any())).thenAnswer((_) async => right(tQuote));
|
||||
when(
|
||||
() => mockTranslationTargetConfig.get(),
|
||||
).thenAnswer((_) async => tTranslationTarget);
|
||||
|
||||
// assert later
|
||||
const expected = [QuoteLoadingState(), QuoteLoadedState(quote: tQuote)];
|
||||
expectLater(quoteBloc.stream, emitsInOrder(expected));
|
||||
|
||||
// act
|
||||
quoteBloc.add(const GetQuoteEvent());
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'should emit [QuoteLoading, QuoteError] when getting data fails',
|
||||
() async {
|
||||
// arrange
|
||||
when(() => mockGetQuote(any())).thenAnswer((_) async => left(tFailure));
|
||||
when(
|
||||
() => mockTranslationTargetConfig.get(),
|
||||
).thenAnswer((_) async => tTranslationTarget);
|
||||
|
||||
// assert later
|
||||
final expected = [
|
||||
const QuoteLoadingState(),
|
||||
QuoteErrorState(message: tFailure.message),
|
||||
];
|
||||
expectLater(quoteBloc.stream, emitsInOrder(expected));
|
||||
|
||||
// act
|
||||
quoteBloc.add(const GetQuoteEvent());
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
group('GetTranslatedQuote', () {
|
||||
test(
|
||||
'should get translated quote from GetTranslatedQuote use case',
|
||||
() async {
|
||||
// arrange
|
||||
when(
|
||||
() => mockGetTranslatedQuote(any()),
|
||||
).thenAnswer((_) async => right(tQuote));
|
||||
when(
|
||||
() => mockTranslationTargetConfig.get(),
|
||||
).thenAnswer((_) async => tTranslationTarget);
|
||||
|
||||
// act
|
||||
quoteBloc.add(const GetTranslatedQuoteEvent(tTranslationTarget));
|
||||
await untilCalled(() => mockTranslationTargetConfig.get());
|
||||
await untilCalled(() => mockGetTranslatedQuote(any()));
|
||||
|
||||
// assert
|
||||
verify(() => mockGetTranslatedQuote(any()));
|
||||
verifyNoMoreInteractions(mockGetTranslatedQuote);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'should emit [QuoteLoading, QuoteLoaded] when data is gotten successfully',
|
||||
() async {
|
||||
// arrange
|
||||
when(
|
||||
() => mockGetTranslatedQuote(any()),
|
||||
).thenAnswer((_) async => right(tQuote));
|
||||
when(
|
||||
() => mockTranslationTargetConfig.get(),
|
||||
).thenAnswer((_) async => tTranslationTarget);
|
||||
|
||||
// assert later
|
||||
const expected = [QuoteLoadingState(), QuoteLoadedState(quote: tQuote)];
|
||||
expectLater(quoteBloc.stream, emitsInOrder(expected));
|
||||
|
||||
// act
|
||||
quoteBloc.add(const GetTranslatedQuoteEvent(tTranslationTarget));
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
'should emit [QuoteLoading, QuoteError] when getting data fails',
|
||||
() async {
|
||||
// arrange
|
||||
when(
|
||||
() => mockGetTranslatedQuote(any()),
|
||||
).thenAnswer((_) async => left(tFailure));
|
||||
when(
|
||||
() => mockTranslationTargetConfig.get(),
|
||||
).thenAnswer((_) async => tTranslationTarget);
|
||||
|
||||
// assert later
|
||||
final expected = [
|
||||
const QuoteLoadingState(),
|
||||
QuoteErrorState(message: tFailure.message),
|
||||
];
|
||||
expectLater(quoteBloc.stream, emitsInOrder(expected));
|
||||
|
||||
// act
|
||||
quoteBloc.add(const GetTranslatedQuoteEvent(tTranslationTarget));
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,72 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:fpdart/fpdart.dart';
|
||||
import 'package:kuwot/core/domain/no_params.dart';
|
||||
import 'package:kuwot/core/error/failure.dart';
|
||||
import 'package:kuwot/features/quote/domain/use_cases/get_translations.dart';
|
||||
import 'package:kuwot/features/quote/presentation/bloc/translations_bloc.dart';
|
||||
import 'package:mocktail/mocktail.dart';
|
||||
|
||||
class MockGetTranslations extends Mock implements GetTranslations {}
|
||||
|
||||
class FakeNoParams extends Fake implements NoParams {}
|
||||
|
||||
void main() {
|
||||
late MockGetTranslations mockGetTranslations;
|
||||
late TranslationsBloc translationsBloc;
|
||||
|
||||
setUpAll(() {
|
||||
registerFallbackValue(FakeNoParams());
|
||||
});
|
||||
|
||||
setUp(() {
|
||||
mockGetTranslations = MockGetTranslations();
|
||||
translationsBloc = TranslationsBloc(getTranslations: mockGetTranslations);
|
||||
});
|
||||
|
||||
group('GetTranslations', () {
|
||||
test('should get translations from GetTranslations use case', () async {
|
||||
// arrange
|
||||
when(() => mockGetTranslations(any())).thenAnswer((_) async => right([]));
|
||||
|
||||
// act
|
||||
translationsBloc.add(const GetTranslationsEvent());
|
||||
await untilCalled(() => mockGetTranslations(any()));
|
||||
|
||||
// assert
|
||||
verify(() => mockGetTranslations(any()));
|
||||
verifyNoMoreInteractions(mockGetTranslations);
|
||||
});
|
||||
|
||||
test('should emit [Loading, Loaded] when successful', () async {
|
||||
// arrange
|
||||
when(() => mockGetTranslations(any())).thenAnswer((_) async => right([]));
|
||||
|
||||
// assert later
|
||||
final expected = [
|
||||
const TranslationsLoadingState(),
|
||||
const TranslationsLoadedState(translations: []),
|
||||
];
|
||||
expectLater(translationsBloc.stream, emitsInOrder(expected));
|
||||
|
||||
// act
|
||||
translationsBloc.add(const GetTranslationsEvent());
|
||||
});
|
||||
|
||||
test('should emit [Loading, Error] when unsuccessful', () async {
|
||||
// arrange
|
||||
when(() => mockGetTranslations(any())).thenAnswer(
|
||||
(_) async => left(const UnknownFailure(message: 'Unknown Failure')),
|
||||
);
|
||||
|
||||
// assert later
|
||||
final expected = [
|
||||
const TranslationsLoadingState(),
|
||||
const TranslationsErrorState(message: 'Unknown Failure'),
|
||||
];
|
||||
expectLater(translationsBloc.stream, emitsInOrder(expected));
|
||||
|
||||
// act
|
||||
translationsBloc.add(const GetTranslationsEvent());
|
||||
});
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue