SankethBK / diaryvault

A personal diary application written in Flutter
https://play.google.com/store/apps/details?id=me.sankethbk.dairyapp
MIT License
86 stars 59 forks source link

Integration test #57 #90

Closed PratyushChauhan closed 10 months ago

PratyushChauhan commented 11 months ago

What type of PR is this? (check all applicable)

Description

Add integration tests

Featured Covered in this PR

Related Tickets & Documents

Screenshots, Recordings

Please replace this line with instructions on how to test your changes, or any screenshots or recording you can attach here.

Tested Feature??

SankethBK commented 11 months ago

This works, you can use this starting point

import 'package:dairy_app/app/view/app.dart';
import 'package:dairy_app/core/pages/home_page.dart';
import 'package:dairy_app/features/auth/presentation/pages/auth_page.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:dairy_app/core/dependency_injection/injection_container.dart'
    as di;

Future<void> main() async {
  setUpAll(() async {
    IntegrationTestWidgetsFlutterBinding.ensureInitialized();
    await dotenv.load();
    await di.init();
  });

  testWidgets(
      'Guest login test, click on sign up button to move reveal guest login button'
      'click the guest login button to enter HomePage',
      (WidgetTester tester) async {
    await tester.pumpWidget(const App());
    await tester.pumpAndSettle();
    await tester.tap(find.text('Continue as guest'));
    await tester.pumpAndSettle();
    expect(find.byType(HomePage), findsOneWidget);
  });
}

you can click on FloatingActionButton, create a note with some title and body and check if it appears in home page after saving

PratyushChauhan commented 11 months ago

This works for login but we get the following error if we try to make a note and save it in guest mode. image

SankethBK commented 11 months ago

This works for login but we get the following error if we try to make a note and save it in guest mode. image

Can you push the code, I'll try running locally

PratyushChauhan commented 11 months ago

On it. One more this, these logs were shown after I manually tried to create the note since the test kept failing.

SankethBK commented 10 months ago

Hi, this test will pass, i think the issue occurs when we try to add text into the rich text editor. So let's add only title for now

import 'package:dairy_app/core/pages/home_page.dart';
import 'package:dairy_app/core/widgets/glassmorphism_cover.dart';
import 'package:dairy_app/features/notes/presentation/pages/note_create_page.dart';
import 'package:dairy_app/features/notes/presentation/widgets/note_save_button.dart';
import 'package:dairy_app/features/notes/presentation/widgets/note_title_input_field.dart';
import 'package:dairy_app/features/notes/presentation/widgets/notes_close_button.dart';
import 'package:dairy_app/features/notes/presentation/widgets/rich_text_editor.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_quill/flutter_quill.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:dairy_app/core/dependency_injection/injection_container.dart'
    as di;

Future<void> main() async {
  setUpAll(() async {
    IntegrationTestWidgetsFlutterBinding.ensureInitialized();
    await dotenv.load();
    await di.init();
  });

  testWidgets('...', (WidgetTester tester) async {
    await tester.pumpWidget(const App());
    await tester.pumpAndSettle();
    await tester.tap(find.text('Continue as guest'));
    await tester.pumpAndSettle();
    expect(find.byType(HomePage), findsOneWidget);
    await tester.pumpAndSettle();
    await tester.tap(find.byType(FloatingActionButton));
    await tester.pumpAndSettle();
    // input some text in title field
    await tester.enterText(find.byType(NoteTitleInputField), 'Test Title');
    await tester.pumpAndSettle();

    expect(find.byType(QuillEditor), findsOneWidget);

    expect(
        find.descendant(
          of: find.byType(QuillEditor),
          matching: find.byType(TextFieldTapRegion),
        ),
        findsOneWidget);

    await tester.tap(find.byType(NoteSaveButton));
    await tester.pumpAndSettle();

    await Future.delayed(const Duration(seconds: 5));
    await tester.pumpAndSettle();
    expect(find.text('Test Title'), findsOneWidget);
    expect(find.byType(HomePage), findsOneWidget);
    expect(find.byType(NoteCreatePage), findsNothing);
  });
}

Also there was a bug during saving the note I have fixed it in latest master commit

PratyushChauhan commented 10 months ago

Makes sense, since we have to format strings before we pass them into the rich text editor. Thanks for your guidance.

PratyushChauhan commented 10 months ago

Will work on updation and deletion next.

SankethBK commented 10 months ago

Makes sense, since we have to format strings before we pass them into the rich text editor.

Actually we should be able to enter raw text into the rich text editor since QuillEditor has TextFieldTapRegion as one of its child. It should handle the conversion to json internally. Not sure why its not working. Anyway we can consider only title for this issue.

SankethBK commented 10 months ago

Found some reference for how they are doing it in widget tests of flutter_quill. It looks a bit complicated, since we don't have access to RawEditor outside of the package. So its better to ignore this for now https://github.com/SankethBK/dairy_app/blob/f62ea47d797507477ef412c93232e17e0f02b6c7/packages/flutter_quill/lib/src/test/widget_tester_extension.dart

PratyushChauhan commented 10 months ago

Alright

PratyushChauhan commented 10 months ago

Add note updation test.

PratyushChauhan commented 10 months ago

Add integration test for note deletion. Please review and let me know what other tests I should add.

SankethBK commented 10 months ago

Add integration test for note deletion. Please review and let me know what other tests I should add.

Thank you, will review it by today

SankethBK commented 10 months ago

Let's have all testWidgets in a single file note_crud_tests.dart and create a group like this


void main() {
  group('MyWidget Tests', () {
    testWidgets('Test Case 1', (WidgetTester tester) async {
      // Your test case code here
    });

    testWidgets('Test Case 2', (WidgetTester tester) async {
      // Your test case code here
    });

}

It would be easier to segregate if we add more tests in future

PratyushChauhan commented 10 months ago

@SankethBK should I make helper functions for widgets tests and store them somewhere else for better readability?

eg:

void main() {
  group('MyWidget Tests', () {
    testWidgets('Test Case 1', (WidgetTester tester) async {
      await testTestCase1(tester);
    });

    testWidgets('Test Case 2', (WidgetTester tester) async {
      await testTestCase2(tester);
    });
  });
}
SankethBK commented 10 months ago

@SankethBK should I make helper functions for widgets tests and store them somewhere else for better readability?

eg:

void main() {
  group('MyWidget Tests', () {
    testWidgets('Test Case 1', (WidgetTester tester) async {
      await testTestCase1(tester);
    });

    testWidgets('Test Case 2', (WidgetTester tester) async {
      await testTestCase2(tester);
    });
  });
}

@PratyushChauhan not required, we can do that in future if all tests have lot of repeated code

PratyushChauhan commented 10 months ago

@SankethBK tried to merge tests into one file, encountered some problems which i think might be relating to dependecy initialization. Pushed changes for your reference.

Logs: image

SankethBK commented 10 months ago

Second test is starting right where first test was stopped, so its failing. Because its trying to initialize everything again, but they are already initalized

SankethBK commented 10 months ago

Let's have them in different files only for now, like it was earlier.

PratyushChauhan commented 10 months ago

pushed refactorings.

SankethBK commented 10 months ago

Looks good, there is some linter error related to formatting, can you run this command

dart format --line-length 80 .

It will format the files, you can push those changes

PratyushChauhan commented 10 months ago

got it

PratyushChauhan commented 10 months ago

checks completed

PratyushChauhan commented 10 months ago

Always a pleasure.