Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
HamiiidAsgarian committed Sep 21, 2024
1 parent 7e6a693 commit dcc4783
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test/features/auth/data/models/signup_res_model_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:flutter_showcase/core/errors/exceptions.dart';
import 'package:flutter_showcase/features/auth/data/models/signup_res_model.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('SignUpResModel', () {
test('should throw JsonParseException when data is missing', () {
// Arrange
final json = <String, dynamic>{};

// Act and Assert
expect(
() => SignUpResModel.fromJson(json),
throwsA(isA<JsonParseException>()),
);
});

test('should return SignUpResModel when data is valid', () {
// Arrange
final json = <String, dynamic>{
'success': true,
'message': 'sign up success',
};

// Act
final result = SignUpResModel.fromJson(json);

// Assert
expect(result, isA<SignUpResModel>());
expect(result.success, true);
expect(result.message, 'sign up success');
});
});
}
49 changes: 49 additions & 0 deletions test/features/auth/data/models/token_model_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import 'package:flutter_showcase/core/errors/exceptions.dart';
import 'package:flutter_showcase/features/auth/data/models/token_model.dart';
import 'package:flutter_showcase/features/auth/domain/models/token.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
test('test token model with valid json', () {
// Arrange
final json = <String, dynamic>{
'access_token': 'access_token',
'refresh_token': 'refresh_token',
};

// Act
final tokenModel = TokenModel.fromJson(json);

// Assert
expect(tokenModel, isA<TokenModel>());
expect(tokenModel.accessToken, 'access_token');
expect(tokenModel.refreshToken, 'refresh_token');
});

test('test token model with invalid json', () {
// Arrange
final json = <String, dynamic>{};

// Assert
expect(
() => TokenModel.fromJson(json),
throwsA(isA<JsonParseException>()),
);
});

test('test token model to json', () {
// Arrange
final tokenModel = TokenModel(
accessToken: 'access_token',
refreshToken: 'refresh_token',
);

// Act
final json = tokenModel.toJson();

// Assert
expect(json, isA<Map<String, dynamic>>());
expect(json[TokensType.access_token.name], 'access_token');
expect(json[TokensType.refresh_token.name], 'refresh_token');
});
}
34 changes: 34 additions & 0 deletions test/features/auth/data/models/token_validation_model_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import 'package:flutter_showcase/core/errors/exceptions.dart';
import 'package:flutter_showcase/features/auth/data/models/token_validation_model.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('TokenValidationResponseModel', () {
test('should throw JsonParseException when data is missing', () {
// Arrange
final json = <String, dynamic>{};

// Act and Assert
expect(
() => TokenValidationResponseModel.fromJson(json),
throwsA(isA<JsonParseException>()),
);
});

test('should return TokenValidationResponseModel when data is valid', () {
// Arrange
final json = <String, dynamic>{
'success': true,
'message': 'token is valid',
};

// Act
final result = TokenValidationResponseModel.fromJson(json);

// Assert
expect(result, isA<TokenValidationResponseModel>());
expect(result.isValid, true);
expect(result.message, 'token is valid');
});
});
}
36 changes: 36 additions & 0 deletions test/features/auth/data/models/user_model_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'package:flutter_showcase/core/errors/exceptions.dart';
import 'package:flutter_showcase/features/auth/data/models/user_model.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
group('UserModel', () {
test('should throw JsonParseException when data is missing', () {
// Arrange
final json = <String, dynamic>{};

// Act and Assert
expect(
() => UserModel.fromJson(json),
throwsA(isA<JsonParseException>()),
);
});

test('should return UserModel when data is valid', () {
// Arrange
final json = <String, dynamic>{
'id': 1,
'email': 'email',
'password': 'password',
};

// Act
final result = UserModel.fromJson(json);

// Assert
expect(result, isA<UserModel>());
expect(result.id, 1);
expect(result.email, 'email');
expect(result.password, 'password');
});
});
}

0 comments on commit dcc4783

Please sign in to comment.