utils/data_transfer_flutter/test/transfer_test.dart
Krzysztof kuhy Rudnicki 5be9a22970 data_transfer_flutter 0.1.0: shared JSON export/import
Save-as on mobile (SAF), save dialog on desktop, download on web; versioned envelope helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WzxdWXTUM9dpnCS2wtFkx4
2026-11-01 19:46:05 +01:00

94 lines
2.4 KiB
Dart

import 'dart:convert';
import 'package:data_transfer_flutter/data_transfer_flutter.dart';
import 'package:flutter_test/flutter_test.dart';
import 'support/fake_platform.dart';
void main() {
late FakePlatform fake;
late DataTransferPlatform original;
setUp(() {
original = DataTransfer.platform;
fake = FakePlatform();
DataTransfer.platform = fake;
});
tearDown(() => DataTransfer.platform = original);
group('exportJson', () {
test('saves the map as indented JSON under the given name', () async {
final saved = await exportJson(fileName: 'a.json', json: {'k': 1});
expect(saved, isTrue);
expect(fake.saves.single.$1, 'a.json');
expect(
fake.saves.single.$2,
const JsonEncoder.withIndent(' ').convert({'k': 1}),
);
expect(fake.shares, isEmpty);
});
test('returns false when the dialog was cancelled', () async {
fake.accept = false;
expect(await exportJson(fileName: 'a.json', json: {}), isFalse);
});
});
group('exportJsonText', () {
test('passes an already-encoded string through untouched', () async {
await exportJsonText(fileName: 'b.json', text: '{"x":1}');
expect(fake.saves.single, ('b.json', '{"x":1}'));
});
});
group('shareJson', () {
test('uses the share action, not save', () async {
final shared = await shareJson(fileName: 'c.json', json: {'k': 2});
expect(shared, isTrue);
expect(fake.shares.single.$1, 'c.json');
expect(jsonDecode(fake.shares.single.$2), {'k': 2});
expect(fake.saves, isEmpty);
});
});
group('pickJson', () {
test('decodes the picked file', () async {
fake.picked = '{"a":[1,2]}';
expect(await pickJson(), {
'a': [1, 2],
});
});
test('returns null when the picker was cancelled', () async {
expect(await pickJson(), isNull);
expect(await pickJsonText(), isNull);
});
test('rejects a file that is not JSON', () async {
fake.picked = '{oops';
await expectLater(pickJson, throwsA(isA<EnvelopeInvalidJson>()));
});
test('rejects JSON that is not an object', () async {
fake.picked = '[1]';
await expectLater(pickJson, throwsA(isA<EnvelopeNotObject>()));
});
});
group('pickJsonText', () {
test('returns the raw text without decoding it', () async {
fake.picked = 'not even json';
expect(await pickJsonText(), 'not even json');
});
});
}