From 2367899b541e0354084f5adbe709d67c29ebe4fc Mon Sep 17 00:00:00 2001 From: llsajjad Date: Mon, 6 Oct 2025 08:28:58 +0330 Subject: [PATCH] test for response data --- test/widget_test.dart | 144 +++++++++++++++++++++++++++++++++++------- 1 file changed, 121 insertions(+), 23 deletions(-) diff --git a/test/widget_test.dart b/test/widget_test.dart index f9f4717..6f1f400 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -1,30 +1,128 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility in the flutter_test package. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. - -import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; - -import 'package:tm_app/main.dart'; +import 'package:tm_app/data/services/model/company_profile_data/company_profile_data.dart'; +import 'package:tm_app/data/services/model/company_profile_response/company_profile_response.dart'; +import 'package:tm_app/data/services/model/error/error_model.dart'; void main() { - testWidgets('Counter increments smoke test', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget(const MyApp()); + group('CompanyProfileData Model Tests', () { + test('fromJson should parse JSON correctly', () { + final json = { + 'id': '1', + 'name': 'OpenAI', + 'registration_number': '12345', + 'industry': 'AI Research', + 'founded_year': 2015, + 'created_at': 1700000000, + }; - // Verify that our counter starts at 0. - expect(find.text('0'), findsOneWidget); - expect(find.text('1'), findsNothing); + final data = CompanyProfileData.fromJson(json); - // Tap the '+' icon and trigger a frame. - await tester.tap(find.byIcon(Icons.add)); - await tester.pump(); + expect(data.id, '1'); + expect(data.name, 'OpenAI'); + expect(data.registrationNumber, '12345'); + expect(data.foundedYear, 2015); + expect(data.createdAt, 1700000000); + }); - // Verify that our counter has incremented. - expect(find.text('0'), findsNothing); - expect(find.text('1'), findsOneWidget); + test('toJson should return valid JSON', () { + final data = const CompanyProfileData( + id: '2', + name: 'Flutter Inc', + registrationNumber: '98765', + industry: 'Software', + foundedYear: 2017, + createdAt: 1800000000, + ); + + final json = data.toJson(); + + expect(json['id'], '2'); + expect(json['registration_number'], '98765'); + expect(json['industry'], 'Software'); + }); + + test('copyWith should return a modified object', () { + final data = const CompanyProfileData( + id: '1', + name: 'OldName', + registrationNumber: '123', + industry: 'Tech', + foundedYear: 2020, + createdAt: 1600000000, + ); + + final updated = data.copyWith(name: 'NewName'); + + expect(updated.name, 'NewName'); + expect(updated.industry, 'Tech'); + }); }); -} + + group('CompanyProfileResponse Model Tests', () { + test('fromJson should parse nested JSON correctly', () { + final json = { + 'message': 'Success', + 'success': true, + 'error': {'code': '0', 'message': 'No error', 'details': 'details'}, + 'data': { + 'id': '10', + 'name': 'AI Labs', + 'registration_number': 'A123', + 'industry': 'AI', + 'founded_year': 2020, + 'created_at': 1700000000, + }, + }; + + final response = CompanyProfileResponse.fromJson(json); + + expect(response.message, 'Success'); + expect(response.success, true); + expect(response.data?.name, 'AI Labs'); + expect(response.data?.foundedYear, 2020); + }); + + test('toJson should return valid JSON map', () { + final data = const CompanyProfileData( + id: '3', + name: 'NeuralNet', + registrationNumber: 'B456', + industry: 'ML', + foundedYear: 2019, + createdAt: 1750000000, + ); + + final response = CompanyProfileResponse( + message: 'Success', + success: true, + error: const ErrorModel( + code: '0', + message: 'No error', + details: 'details', + ), + data: data, + ); + + final json = response.toJson(); + + expect(json['message'], 'Success'); + expect(json['success'], true); + expect(response.data?.name, 'NeuralNet'); + }); + + test('copyWith should modify only specific fields', () { + final response = const CompanyProfileResponse( + message: 'Old message', + success: false, + error: null, + data: null, + ); + + final updated = response.copyWith(message: 'New message', success: true); + + expect(updated.message, 'New message'); + expect(updated.success, true); + expect(updated.data, null); + }); + }); +} \ No newline at end of file