Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to flush multiple times to test subscriptions #1563

Open
fdub opened this issue Sep 15, 2020 · 5 comments
Open

Not able to flush multiple times to test subscriptions #1563

fdub opened this issue Sep 15, 2020 · 5 comments
Labels
feature New addition or enhancement to existing solutions help wanted

Comments

@fdub
Copy link

fdub commented Sep 15, 2020

I'm not able to test a component which uses a subscription to receive updates because it's not possible to call flush(...) multiple times on a TestOperation. The contained observer is completed after calling flush for the first time.

Here's an example of a failing test showing this behavior.

    it('should receive updates', (done) => {
        TestBed.configureTestingModule({
            imports: [ApolloTestingModule],
        });
        const apollo = TestBed.inject(Apollo);
        const controller = TestBed.inject(ApolloTestingController);
        
        const query = gql`
            subscription dates {
                date
            }`;
        const data1 = {date: '1970-01-01'};
        const data2 = {date: '1970-01-02'};

        let resultData: any;
        apollo.subscribe<any>({query: query}).subscribe({
            next: (result) => {
                resultData = result.data;
            },
            error: (e) => {
                done.fail(e);
            },
        });

        const op = controller.expectOne(query);
        op.flush({data: data1});
        op.flush({data: data2});

        expect(resultData).toEqual(data2);
        done();
    });

An additional argument keepOpen: bool (with default false) or an additional function like flushAndKeepOpen(...), omitting the call to complete(), could solve this.

@MikaStark
Copy link

In addition, I want to propose to add this possibility to Queries too.
Sometimes you may need to test multiple behaviors dependening on results (nullability, list emptiness, greater/lower, contains, etc.).

@kamilkisiela kamilkisiela added feature New addition or enhancement to existing solutions help wanted labels Mar 30, 2021
@rmckeel
Copy link

rmckeel commented Jul 21, 2021

I fully agree, yes, would love to see this. Our use case is mocking a GraphQL query that has the pollInterval option set. Without being able to flush twice and complete (I'll skip the obvious potty joke here ;) it is hard to test that our software operates properly when the operation is polled again and either changes or stays the same.

@Maximaximum
Copy link

Hm, strange that this has not been implemented yet. It seems to be such a core thing

@Killerbear
Copy link

same problem here. we are using a subscription on valueChanges to update our template accordingly if something was added or removed in the data.
But without the ability to flush multiple times, we can't test this.

@Frozen-byte
Copy link
Contributor

Frozen-byte commented Oct 22, 2024

Is there any workaround for this other than completely mocking the GraphQL Service?
When inspect the actual sourcecode:

The complete statement may be prevented on a subscription document.

My Shoot at this issue: #2316 feel free to test, comment and review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New addition or enhancement to existing solutions help wanted
Projects
None yet
Development

No branches or pull requests

7 participants