Skip to content

Commit

Permalink
test: [#23] 테스트 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Younggun-Kim committed Nov 14, 2024
1 parent 7bcfa27 commit 402db9f
Show file tree
Hide file tree
Showing 18 changed files with 416 additions and 21 deletions.
9 changes: 9 additions & 0 deletions lib/core/utils/mixins/widget_key_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:flutter/cupertino.dart';

mixin WidgetKeyUtils {
String getKey();

Key toKey() {
return Key(getKey());
}
}
2 changes: 1 addition & 1 deletion lib/feature/account/data/data_sources/remote/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class AccountApi {
late final loginPath = '$path/login';

/// 휴대폰 인증 요청 주소
late final sendAuthCodePath = '$path/verify/phone';
late final sendAuthCodePath = '$path/verify/phone_auth.dart';

/// 인증번호 검증 요청
late final verifyAuthCodePath = '$path/verify/code';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ part 'auth_code_verification_entity.freezed.dart';

part 'auth_code_verification_entity.parser.dart';

part 'auth_code_verification_entity.mock.dart';

@freezed
class AuthCodeVerificationEntity with _$AuthCodeVerificationEntity {
factory AuthCodeVerificationEntity({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
part of 'auth_code_verification_entity.dart';

extension AuthCodeVerificationEntityMock on AuthCodeVerificationEntity {
static AuthCodeVerificationEntity validMock() {
return AuthCodeVerificationEntity(
phone: '01012345678',
authCode: '123456',
);
}

static AuthCodeVerificationEntity invalidMock() {
return AuthCodeVerificationEntity(
phone: '01012345678',
authCode: '1234',
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
part of 'send_auth_code_result_entity.dart';

extension SendAuthCodeResultEntityMock on SendAuthCodeResultEntity {
static SendAuthCodeResultEntity success() {
return SendAuthCodeResultEntity(status: true);
}

static SendAuthCodeResultEntity serverError() {
return SendAuthCodeResultEntity(
status: false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:withu_app/core/core.dart';
Expand All @@ -14,7 +15,7 @@ part 'phone_auth_bloc.handler.dart';
part 'phone_auth_bloc.parser.dart';

class PhoneAuthBloc
extends BaseBloc<PhoneAuthEvent, PhoneAuthState> {
extends Bloc<PhoneAuthEvent, PhoneAuthState> {
final AccountUseCase accountUseCase;

PhoneAuthBloc({
Expand All @@ -23,7 +24,7 @@ class PhoneAuthBloc
PhoneAuthState(status: BaseBlocStatus.initial()),
) {
on<PhoneAuthPhoneInputted>(_onPhoneInputted);
on<PhoneAuthAuthCodeRequested>(_onAuthCodeRequested);
on<PhoneAuthAuthCodeSent>(_onAuthCodeRequested);
on<PhoneAuthAuthCodeInputted>(_onAuthCodeInputted);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension PhoneAuthBlocHandler on PhoneAuthBloc {

/// 인증번호 발송 요청 이벤트.
void _onAuthCodeRequested(
PhoneAuthAuthCodeRequested event,
PhoneAuthAuthCodeSent event,
Emitter<PhoneAuthState> emit,
) async {
await accountUseCase.sendAuthCode(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
part of 'phone_auth_bloc.dart';

sealed class PhoneAuthEvent extends BaseBlocEvent {}
abstract class PhoneAuthEvent extends Equatable {}

/// 휴대폰 번호 입력 이벤트.
class PhoneAuthPhoneInputted extends PhoneAuthEvent {
Expand All @@ -9,10 +9,16 @@ class PhoneAuthPhoneInputted extends PhoneAuthEvent {
PhoneAuthPhoneInputted({required this.value});

Phone get phone => Phone(value);

@override
List<Object?> get props => [value];
}

/// 인증 번호 요청 이벤트.
class PhoneAuthAuthCodeRequested extends PhoneAuthEvent {}
class PhoneAuthAuthCodeSent extends PhoneAuthEvent {
@override
List<Object?> get props => ['auth_code_sent'];
}

/// 인증번호 입력 이벤트.
class PhoneAuthAuthCodeInputted extends PhoneAuthEvent {
Expand All @@ -21,4 +27,7 @@ class PhoneAuthAuthCodeInputted extends PhoneAuthEvent {
PhoneAuthAuthCodeInputted({required this.value});

AuthCode get code => AuthCode(value);

@override
List<Object?> get props => [];
}
2 changes: 0 additions & 2 deletions lib/feature/account/presentation/page/login/login_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ class _LoginPageState extends State<LoginPageContent> {
enabled: state.isEnabledLogin,
),
const SizedBox(height: 20),
const PhoneAuthWidget(),
const SizedBox(height: 50),
],
),
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'phone_auth_widget.dart';
export 'phone_auth_widget_key.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,14 @@ class PhoneInput extends StatelessWidget {
return BlocBuilder<PhoneAuthBloc, PhoneAuthState>(
builder: (context, state) {
return BaseInput(
key: PhoneAuthWidgetKey.phoneInput.toKey(),
hintText: StringRes.enterOnlyNumber.tr,
maxLength: 11,
keyboardType: TextInputType.phone,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
],
suffix: const VerificationBtn(),
suffix: const SendAuthBtn(),
onChanged: (String text) {
context
.read<PhoneAuthBloc>()
Expand All @@ -51,21 +52,20 @@ class PhoneInput extends StatelessWidget {
}

/// 인증 버튼
class VerificationBtn extends StatelessWidget {
const VerificationBtn({super.key});
class SendAuthBtn extends StatelessWidget {
const SendAuthBtn({super.key});

@override
Widget build(BuildContext context) {
return BlocBuilder<PhoneAuthBloc, PhoneAuthState>(
builder: (context, state) {
return InkWell(
key: PhoneAuthWidgetKey.sendAuthBtn.toKey(),
onTap: () {
if (!state.canRequestVerification) {
return;
}
context
.read<PhoneAuthBloc>()
.add(PhoneAuthAuthCodeRequested());
context.read<PhoneAuthBloc>().add(PhoneAuthAuthCodeSent());
},
child: Text(
StringRes.verification.tr,
Expand All @@ -86,7 +86,7 @@ class AuthCodeInput extends StatelessWidget {
return BlocBuilder<PhoneAuthBloc, PhoneAuthState>(
builder: (context, state) {
return BaseInput(
key: const Key('auth_code_input'),
key: PhoneAuthWidgetKey.authCodeInput.toKey(),
hintText: StringRes.enterVerificationCode.tr,
errorText: "! ${StringRes.invalidVerificationCode.tr}",
errorVisible: state.isVisibleAuthCodeError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'package:withu_app/core/utils/mixins/widget_key_utils.dart';

enum PhoneAuthWidgetKey with WidgetKeyUtils {
phoneInput,
sendAuthBtn,
authCodeInput;

@override
String getKey() {
return name;
}
}
2 changes: 1 addition & 1 deletion lib/feature/account/presentation/widget/widget.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export 'login_tab/tab.dart';
export 'phone_auth/phone_auth_widget.dart';
export 'phone_auth/phone_auth.dart';
14 changes: 14 additions & 0 deletions test/core/utils/mixin/widget_key_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:withu_app/core/utils/mixins/widget_key_utils.dart';

extension WidgetKeyUtilsExt on WidgetKeyUtils {
Finder toFinder() {
return find.byKey(toKey());
}

/// Widget으로 변경
T toWidget<T extends Widget>(WidgetTester tester) {
return tester.widget<T>(toFinder());
}
}
4 changes: 2 additions & 2 deletions test/feature/account/domain/usecase/usecase_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void main() {
final expectDto = FailResponse.error();

when(
() => mockRepo.sendAuthCode(phone: any(named: 'phone')),
() => mockRepo.sendAuthCode(phone: any(named: 'phone_auth.dart')),
).thenAnswer(
(_) async => ApiResponse.fail(expectDto),
);
Expand All @@ -114,7 +114,7 @@ void main() {
expect(result.status, isFalse);
expect(result.message, StringRes.serverError.tr);
verify(
() => mockRepo.sendAuthCode(phone: any(named: 'phone')),
() => mockRepo.sendAuthCode(phone: any(named: 'phone_auth.dart')),
).called(1);
});
});
Expand Down
Loading

0 comments on commit 402db9f

Please sign in to comment.