apollo-angular icon indicating copy to clipboard operation
apollo-angular copied to clipboard

Not able to flush multiple times to test subscriptions

Open fdub opened this issue 4 years ago • 4 comments

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.

fdub avatar Sep 15 '20 07:09 fdub

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.).

MikaStark avatar Oct 29 '20 14:10 MikaStark

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.

rmckeel avatar Jul 21 '21 20:07 rmckeel

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

Maximaximum avatar Nov 05 '21 17:11 Maximaximum

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.

Killerbear avatar Apr 08 '22 14:04 Killerbear