Skip to content

Not able to flush multiple times to test subscriptions  #1563

@fdub

Description

@fdub

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions