jeudi 19 septembre 2019

window.open() returns null in unit test

I'm writing a unit test for a method that launches a browser window when a user clicks a button.

The test fails because null is returned.

When running the test no browser window is opened.

When using the app it works as expected.

This is the method:

import 'package:flutter_web/material.dart';
import 'dart:html' as html;
import 'package:web_flutter/data/tenant_view_model.dart';

class PrintReportsCard extends StatelessWidget {
  final TenantViewModel tenantVM;

  const PrintReportsCard({Key key, this.tenantVM}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var textTheme = Theme.of(context).textTheme;
    return Column(
      children: _buildReportButtons(context, textTheme),
    );
  }

   [ . . . other code not involved in this test . . . ]

  Future launchURL(String download_url) 
  async {
    try {
      await html.window.open(download_url, "Get_Submission");
    } catch (e) {
      print(e);
    }
  }
}

This is the test:

import 'dart:html';

import 'package:test/test.dart';
import 'package:http/http.dart';
import 'package:http/testing.dart';
import 'package:web_flutter/pages/tenants/print_reports_card.dart';
import 'dart:convert';
import 'package:web_flutter/services/service.dart';
import 'package:web_flutter/model/model.dart';
import 'package:web_flutter/data/tenant_view_model.dart';

void main(){
  test("_launchURL launches browser at correct URL", () async {
    // Arrange: setup the test
    final _service = Service();
    Tenant tenant = Tenant();
    tenant.id = 42;

    TenantViewModel tenantVM = TenantViewModel(tenant, _service);
    PrintReportsCard printReportsCard = PrintReportsCard(tenantVM: tenantVM);

    // Act
    WindowBase window = await printReportsCard.launchURL("https://someurl.com");

    // Assert
    expect(window, WindowBase);
  });
}

To run the test I'm using this terminal command: pub run test test/print_reports_card_test.dart -p chrome.

The current test results are:

00:14 +2 -1: _launchURL launches browser at correct URL [E]                                                                                      
  Expected: Type:<WindowBase>
    Actual: <null>

[ . . . I can post this redacted output if you think it might be relevant . . . ]

00:14 +2 -1: Some tests failed. 

Aucun commentaire:

Enregistrer un commentaire