Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor tests
  • Loading branch information
tugorez committed Jan 12, 2021
commit 94293f5ad4763ec0245d7af0296dba777678059e
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:file_selector_web/src/dom_helper.dart';
import 'package:pedantic/pedantic.dart';
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';

void main() {
group('FileSelectorWeb', () {
Expand All @@ -33,25 +34,34 @@ void main() {
});

group('getFiles', () {
final mockFile1 = File([], 'file1.txt');
final mockFile1 = File(['123456'], 'file1.txt');
final mockFile2 = File([], 'file2.txt');

testWidgets('works', (_) async {
final futureFile = domHelper.getFiles(input: input);
final Future<List<XFile>> futureFiles = domHelper.getFiles(
input: input,
);

setFilesAndTriggerChange([mockFile1, mockFile2]);

final files = await futureFile;
final List<XFile> files = await futureFiles;

expect(files.length, 2);

expect(files[0], mockFile1);
expect(files[1], mockFile2);
expect(files[0].name, 'file1.txt');
expect(await files[0].length(), 6);
expect(await files[0].readAsString(), '123456');
expect(await files[0].lastModified(), isNotNull);

expect(files[1].name, 'file2.txt');
expect(await files[1].length(), 0);
expect(await files[1].readAsString(), '');
expect(await files[1].lastModified(), isNotNull);
});

testWidgets('works multiple times', (_) async {
Future<List<File>> futureFiles;
List<File> files;
Future<List<XFile>> futureFiles;
List<XFile> files;

// It should work the first time
futureFiles = domHelper.getFiles(input: input);
Expand All @@ -60,7 +70,7 @@ void main() {
files = await futureFiles;

expect(files.length, 1);
expect(files.first, mockFile1);
expect(files.first.name, mockFile1.name);

// The same input should work more than once
futureFiles = domHelper.getFiles(input: input);
Expand All @@ -69,7 +79,7 @@ void main() {
files = await futureFiles;

expect(files.length, 1);
expect(files.first, mockFile2);
expect(files.first.name, mockFile2.name);
});

testWidgets('sets the <input /> attributes and clicks it', (_) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// @dart = 2.9

import 'dart:html';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:integration_test/integration_test.dart';
Expand All @@ -24,7 +24,7 @@ void main() {
});

group('openFile', () {
final mockFile = File(['1001'], 'identity.png');
final mockFile = createXFile('1001', 'identity.png');

testWidgets('works', (WidgetTester _) async {
final typeGroup = XTypeGroup(
Expand All @@ -49,8 +49,8 @@ void main() {
});

group('openFiles', () {
final mockFile1 = File(['123456'], 'file1.txt');
final mockFile2 = File([''], 'file2.txt');
final mockFile1 = createXFile('123456', 'file1.txt');
final mockFile2 = createXFile('', 'file2.txt');

testWidgets('works', (WidgetTester _) async {
final typeGroup = XTypeGroup(
Expand Down Expand Up @@ -82,3 +82,8 @@ void main() {
}

class MockDomHelper extends Mock implements DomHelper {}

XFile createXFile(String content, String name) {
final data = Uint8List.fromList(content.codeUnits);
return XFile.fromData(data, name: name, lastModified: DateTime.now());
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// found in the LICENSE file.

import 'dart:async';
import 'dart:html';
import 'package:meta/meta.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';
Expand Down Expand Up @@ -67,10 +66,9 @@ class FileSelectorWeb extends FileSelectorPlatform {
bool multiple = false,
}) async {
final accept = acceptedTypesToString(acceptedTypeGroups);
final List<File> files = await _domHelper.getFiles(
return _domHelper.getFiles(
accept: accept,
multiple: multiple,
);
return files.map(convertFileToXFile).toList();
}
}
14 changes: 11 additions & 3 deletions packages/file_selector/file_selector_web/lib/src/dom_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';
import 'dart:html';
import 'package:meta/meta.dart';
import 'package:flutter/services.dart';
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';

/// Class to manipulate the DOM with the intention of reading files from it.
class DomHelper {
Expand All @@ -14,12 +15,12 @@ class DomHelper {
}

/// Sets the <input /> attributes and waits for a file to be selected.
Future<List<File>> getFiles({
Future<List<XFile>> getFiles({
String accept = '',
bool multiple = false,
@visibleForTesting FileUploadInputElement input,
}) {
final Completer<List<File>> _completer = Completer();
final Completer<List<XFile>> _completer = Completer();
input = input ?? FileUploadInputElement();

_container.children.add(
Expand All @@ -29,7 +30,7 @@ class DomHelper {
);

input.onChange.first.then((_) {
final List<File> files = input.files;
final List<XFile> files = input.files.map(_convertFileToXFile).toList();
input.remove();
_completer.complete(files);
});
Expand All @@ -48,4 +49,11 @@ class DomHelper {

return _completer.future;
}

XFile _convertFileToXFile(File file) => XFile(
Url.createObjectUrl(file),
name: file.name,
length: file.size,
lastModified: DateTime.fromMillisecondsSinceEpoch(file.lastModified),
);
}
9 changes: 0 additions & 9 deletions packages/file_selector/file_selector_web/lib/src/utils.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'dart:html';
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';

/// Convert list of XTypeGroups to a comma-separated string
Expand All @@ -20,14 +19,6 @@ String acceptedTypesToString(List<XTypeGroup> acceptedTypes) {
return allTypes.join(',');
}

/// Helper to convert an html.File to an XFile
XFile convertFileToXFile(File file) => XFile(
Url.createObjectUrl(file),
name: file.name,
length: file.size,
lastModified: DateTime.fromMillisecondsSinceEpoch(file.lastModified),
);

/// Make sure that at least one of its fields is populated.
void _assertTypeGroupIsValid(XTypeGroup group) {
assert(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@

// @dart = 2.9

import 'dart:html';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:file_selector_web/src/utils.dart';
import 'package:file_selector_platform_interface/file_selector_platform_interface.dart';

void main() {
group('FileSelectorWeb utils', () {
IntegrationTestWidgetsFlutterBinding.ensureInitialized();

group('acceptedTypesToString', () {
testWidgets('works', (_) async {
test('works', () {
final List<XTypeGroup> acceptedTypes = [
XTypeGroup(label: 'images', webWildCards: ['images/*']),
XTypeGroup(label: 'jpgs', extensions: ['jpg', 'jpeg']),
Expand All @@ -25,13 +21,13 @@ void main() {
expect(accepts, 'images/*,.jpg,.jpeg,image/png');
});

testWidgets('works with an empty list', (_) async {
test('works with an empty list', () {
final List<XTypeGroup> acceptedTypes = [];
final accepts = acceptedTypesToString(acceptedTypes);
expect(accepts, '');
});

testWidgets('works with extensions', (_) async {
test('works with extensions', () {
final List<XTypeGroup> acceptedTypes = [
XTypeGroup(label: 'jpgs', extensions: ['jpeg', 'jpg']),
XTypeGroup(label: 'pngs', extensions: ['png']),
Expand All @@ -40,7 +36,7 @@ void main() {
expect(accepts, '.jpeg,.jpg,.png');
});

testWidgets('works with mime types', (_) async {
test('works with mime types', () {
final List<XTypeGroup> acceptedTypes = [
XTypeGroup(label: 'jpgs', mimeTypes: ['image/jpeg', 'image/jpg']),
XTypeGroup(label: 'pngs', mimeTypes: ['image/png']),
Expand All @@ -49,7 +45,7 @@ void main() {
expect(accepts, 'image/jpeg,image/jpg,image/png');
});

testWidgets('works with web wild cards', (_) async {
test('works with web wild cards', () {
final List<XTypeGroup> acceptedTypes = [
XTypeGroup(label: 'images', webWildCards: ['image/*']),
XTypeGroup(label: 'audios', webWildCards: ['audio/*']),
Expand All @@ -59,16 +55,5 @@ void main() {
expect(accepts, 'image/*,audio/*,video/*');
});
});

group('convertFileToXFile', () {
testWidgets('works', (_) async {
final file = convertFileToXFile(File(['123456'], 'numbers.txt'));

expect(file.name, 'numbers.txt');
expect(await file.length(), 6);
expect(await file.readAsString(), '123456');
expect(await file.lastModified(), isNotNull);
});
});
});
}