3

I created Component I was wanting to test with Angular Dart's Framework.

When I create my test file, it seems though that I am having issues creating a sample file to test against.

In my hirerarchy, I have:

./bird.jpg
./image_uploader_po.dart
./image_uploaders_test.dart

and then in the code I have

test("testing against valid image upload", () async {
  File testImage = new File("./bird.jpg");  //improper constructor.
  fixture.update((Component com){
    com.imageFile = testImage;
  });
  uploaderPO = await fixture.resolvePageObject(Component);
});

The issue I have is that this is not the dart/io implementation of File, but instead the Html Implementation.

I was trying to determine what the best course of action would be for this, to open a file for read access to apply to the Component's implementation of File.

I was looking up filereader, but that relates to a file which exists, which is what I am having issues with.

Right now, File constructor is: File(List<Object> fileBits, String filename, [Map<String, Dynamic> options]); and ultimately didn't know what "fileBits" should be.

You can find the File Class I am using at: https://api.dartlang.org/stable/1.24.3/dart-html/File-class.html

My desired end state is to use a sample image file to apply it to an Image Uploader Component.

Thank you.

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129

1 Answers1

2

The test context you are running in is for the web so only dart:html is available and not dart:io.

As you can imagine in a web script allowing a file to be loaded arbitrarily from the processes file system could be a massive security breach. Thus the web has a good amount of restrictions on that.

A lot of times the contents of the file doesn't matter for our tests so I would do something like this:

File testImage = new File([], "bird.jpg"); // Mock contents don't matter

If the contents are important to the test you could use an XHR to get the image, or try to inline the bytes.

var response = await HttpRequest.request("bird.jpg", responseType: "blob");
fixture.update((Component com){
  com.imageFile = response.response;
});

Hopefully your component can read a Blob itself. If not you can try to create a File using the 'arraybuffer' responseType instead but that is a bit more convoluted.

Ted Sander
  • 1,899
  • 8
  • 11