Giter Club home page Giter Club logo

Comments (5)

budde377 avatar budde377 commented on August 26, 2024 1

I think you need to mock the link. Take a look at how we test the underlying client https://github.com/zino-hofmann/graphql-flutter/blob/main/packages/graphql/test/graphql_client_test.dart

from graphql_codegen.

budde377 avatar budde377 commented on August 26, 2024 1

Under the hood, the generated method calls the basic query method, so it should be the same.

from graphql_codegen.

budde377 avatar budde377 commented on August 26, 2024 1

Amazing! Thanks for sharing your solution.

from graphql_codegen.

Kiruel avatar Kiruel commented on August 26, 2024

But here https://github.com/zino-hofmann/graphql-flutter/blob/main/packages/graphql/test/graphql_client_test.dart#L128

It's more for a basic query without the generated method.

For my method I need to pass a OptionsQueryGetEmailVerified? (the option generated by the lib). I don't think I can do the same of mock the link etc...

Btw if I resolve this I will probably make an example mock test for this. Could be really useful for others.

from graphql_codegen.

Kiruel avatar Kiruel commented on August 26, 2024

I finally find the way to test it ! Thanks again @budde377 !
So if I took my example the code should be:

  @override
  Future<bool> checkEmailVerified() async {
    final result = await graphQLClient.queryGetEmailVerified();

    if (result.parsedData != null) {
      return Future.value(result.parsedData?.me.emailVerified);
    } else {
      throw MyCustomServerException();
    }
  }
And the test:
  group('checkEmailVerified', () {
    test(
      'should check if email verified and return boolean true when success',
      () async {
        // arrange
        final tResponseData = 'get_email_verified_valid.json'.toFixture();
        when(
          () => link.request(any()),
        ).thenAnswer(
          (_) => Stream.fromIterable([
            Response(
              data: tResponseData,
              context: const Context().withEntry(
                const HttpLinkResponseContext(
                  statusCode: 200,
                  headers: {'foo': 'bar'},
                ),
              ),
              response: const {},
            ),
          ]),
        );
        // act
        final result = await datasource.checkEmailVerified();
        // assert
        expect(result, true);
      },
    );

    test(
      'should return false when checkEmailVerified return false',
      () async {
        // arrange
        final tResponseData = 'get_email_verified_wrong.json'.toFixture();
        when(
          () => link.request(any()),
        ).thenAnswer(
          (_) => Stream.fromIterable([
            Response(
              data: tResponseData,
              context: const Context().withEntry(
                const HttpLinkResponseContext(
                  statusCode: 200,
                  headers: {'foo': 'bar'},
                ),
              ),
              response: const {},
            ),
          ]),
        );
        // act
        final result = await datasource.checkEmailVerified();
        // assert
        expect(result, false);
      },
    );

    test(
      'should throw MyCustomException when no coming from server',
      () async {
        // arrange
        when(
          () => link.request(any()),
        ).thenAnswer(
          (_) => Stream.fromIterable([
            Response(
              context: const Context().withEntry(
                const HttpLinkResponseContext(
                  statusCode: 200,
                  headers: {'foo': 'bar'},
                ),
              ),
              response: const {},
            ),
          ]),
        );
        // act
        final call = datasource.checkEmailVerified;
        // assert
        expect(
          () => call(),
          throwsA(isA<MyCustomServerException>()),
        );
      },
    );

    test(
      'should throw a MyCustomServerException when a OperationException occur on graphql',
      () async {
        // arrange
        when(
          () => link.request(any()),
        ).thenThrow(
          HttpLinkParserException(
            originalException: null,
            response: http.Response('', 400),
          ),
        );
        // act
        final call = datasource.checkEmailVerified;
        // assert
        expect(
          () => call(),
          throwsA(isA<MyCustomServerException>()),
        );
      },
    );
  });

With get_email_verified_wrong.json:

{
    "me": {
        "emailVerified": false,
        "__typename": "Me"
    },
    "__typename": "GetEmailVerified"
}

And the get_email_verified_valid.json:

{
    "me": {
        "emailVerified": true,
        "__typename": "Me"
    },
    "__typename": "GetEmailVerified"
}

from graphql_codegen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.