mirror of
https://github.com/foss42/apidash.git
synced 2025-12-09 14:40:20 +08:00
Add package json_field_editor
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:json_field_editor/src/error_message_container.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('ErrorMessageContainer shows correct error message', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
// Build our app and trigger a frame.
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ErrorMessageContainer(
|
||||
jsonError: 'Test error message',
|
||||
errorTextStyle: TextStyle(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Verify that our widget shows the correct error message.
|
||||
expect(find.text('Test error message'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'ErrorMessageContainer does not show error message when jsonError is null',
|
||||
(WidgetTester tester) async {
|
||||
// Build our app and trigger a frame.
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(
|
||||
body: ErrorMessageContainer(
|
||||
jsonError: null,
|
||||
errorTextStyle: TextStyle(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Verify that our widget does not show an error message.
|
||||
expect(find.byType(Text), findsNothing);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:json_field_editor/src/json_highlight/json_highlight.dart';
|
||||
|
||||
void main() {
|
||||
group('JsonHighlight', () {
|
||||
test(
|
||||
'build method should create TextSpan with correct number of children',
|
||||
() {
|
||||
// Arrange
|
||||
var jsonHighlight = JsonHighlight(isFormating: true);
|
||||
var data =
|
||||
'{"key": "value", "number": 123, "bool": true, "null": null}';
|
||||
|
||||
// Act
|
||||
TextSpan result = jsonHighlight.build(data);
|
||||
|
||||
// Assert
|
||||
expect(
|
||||
result.children!.length,
|
||||
23,
|
||||
); // adjust this based on your expected result
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:json_field_editor/json_field_editor.dart';
|
||||
|
||||
void main() {
|
||||
testWidgets('JsonTextField Widget Test. Formatting a valid json', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
// Build our app and trigger a frame.
|
||||
final controller = JsonTextFieldController();
|
||||
controller.value = const TextEditingValue(
|
||||
text: '{"key": "value"}',
|
||||
selection: TextSelection.collapsed(offset: 0),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SizedBox(
|
||||
height: 300,
|
||||
width: 300,
|
||||
child: JsonField(isFormatting: true, controller: controller),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Verify that JsonTextField is present.
|
||||
expect(find.byType(JsonField), findsOneWidget);
|
||||
expect(controller.text, equals('{\n "key": "value"\n}'));
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'JsonTextField Widget Test. Formatting a valid json using controller',
|
||||
(WidgetTester tester) async {
|
||||
// Build our app and trigger a frame.
|
||||
final controller = JsonTextFieldController();
|
||||
controller.value = const TextEditingValue(
|
||||
text: '{"key": "value"}',
|
||||
selection: TextSelection.collapsed(offset: 0),
|
||||
);
|
||||
controller.formatJson(sortJson: false);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SizedBox(
|
||||
height: 300,
|
||||
width: 300,
|
||||
child: JsonField(isFormatting: true, controller: controller),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Verify that JsonTextField is present.
|
||||
expect(find.byType(JsonField), findsOneWidget);
|
||||
expect(controller.text, equals('{\n "key": "value"\n}'));
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('JsonTextField Widget Test, invalid Json', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
final controller = JsonTextFieldController();
|
||||
controller.value = const TextEditingValue(
|
||||
text: '{"key": "value"',
|
||||
selection: TextSelection.collapsed(offset: 0),
|
||||
);
|
||||
|
||||
controller.formatJson(sortJson: false);
|
||||
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: JsonField(isFormatting: true, controller: controller),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Verify that JsonTextField is present.
|
||||
expect(find.byType(JsonField), findsOneWidget);
|
||||
expect(controller.text, equals('\n{"key": "value"'));
|
||||
});
|
||||
testWidgets('JsonTextField Widget Test, in a valid Json', (
|
||||
WidgetTester tester,
|
||||
) async {
|
||||
final controller = JsonTextFieldController();
|
||||
controller.value = const TextEditingValue(
|
||||
text: '{"key": "value","anotherKey": "anotherValue","list": [3,2,1]}',
|
||||
selection: TextSelection.collapsed(offset: 0),
|
||||
);
|
||||
|
||||
// Build our app and trigger a frame.
|
||||
controller.formatJson(sortJson: true);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: JsonField(isFormatting: true, controller: controller),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// Verify that JsonTextField is present.
|
||||
expect(find.byType(JsonField), findsOneWidget);
|
||||
expect(
|
||||
controller.text,
|
||||
equals(
|
||||
'{\n "anotherKey": "anotherValue",\n "key": "value",\n "list": [\n 3,\n 2,\n 1\n ]\n}',
|
||||
),
|
||||
);
|
||||
});
|
||||
}
|
||||
44
packages/json_field_editor/test/src/json_utils_test.dart
Normal file
44
packages/json_field_editor/test/src/json_utils_test.dart
Normal file
@@ -0,0 +1,44 @@
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:json_field_editor/src/json_utils.dart';
|
||||
|
||||
void main() {
|
||||
group('getJsonParsingError', () {
|
||||
test('returns error message for invalid JSON', () {
|
||||
String? invalidJson = '{ "name": "John", "age": 30, }';
|
||||
expect(JsonUtils.getJsonParsingError(invalidJson), isNotNull);
|
||||
});
|
||||
|
||||
test('returns null for valid JSON', () {
|
||||
String? validJson = '{ "name": "John", "age": 30 }';
|
||||
expect(JsonUtils.getJsonParsingError(validJson), isNull);
|
||||
});
|
||||
});
|
||||
|
||||
group('validateJson', () {
|
||||
test('calls onError with null for valid JSON', () {
|
||||
String validJson = '{ "name": "John", "age": 30 }';
|
||||
bool onErrorCalled = false;
|
||||
JsonUtils.validateJson(
|
||||
json: validJson,
|
||||
onError: (error) {
|
||||
onErrorCalled = true;
|
||||
expect(error, isNull);
|
||||
},
|
||||
);
|
||||
expect(onErrorCalled, isTrue);
|
||||
});
|
||||
|
||||
test('calls onError with error message for invalid JSON', () {
|
||||
String invalidJson = '{ "name": "John", "age": 30, }';
|
||||
bool onErrorCalled = false;
|
||||
JsonUtils.validateJson(
|
||||
json: invalidJson,
|
||||
onError: (error) {
|
||||
onErrorCalled = true;
|
||||
expect(error, isNotNull);
|
||||
},
|
||||
);
|
||||
expect(onErrorCalled, isTrue);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user