Search code examples
apolloapollo-clientapollo-angular

Is it possible to reset the cache using ApolloTestingModule?


I have a component that uses the Apollo Angular client to fetch some data. I'm trying to write tests for it to check how it responds to different types of data.

However there does seem to be a way to change the response data after the first time (I'm assuming due to caching). Is there a way to clear the cache while testing or force it to not use the cache?

My code looks something like this

describe('TestComponent', () => {
    let apollo: ApolloTestingController;
    let component: UserDetailsComponent;
    let fixture: ComponentFixture<UserDetailsComponent>;

    beforeEach(async(() => {
        TestBed.configureTestingModule({
            imports: [
                ApolloTestingModule,
            ],
            declarations: [TestComponent],
        })
        .compileComponents();

        apollo = TestBed.get(ApolloTestingController);

        fixture = TestBed.createComponent(TestComponent);
        component = fixture.componentInstance;
    }));

    it('should work with data type 1', () => {
        apollo.expectOne('TestQuery').flush({
            data: {
                something: 'test 1',
            },
        });
    });

    it('should work with data type 2', () => {
        // Fails to match here
        apollo.expectOne('TestQuery').flush({
            data: {
                something: 'test 2',
            },
        });
    });
});

Solution

  • I found that you can do something like this

    const apolloClient = TestBed.get(Apollo).getClient();
    await apolloClient.clearStore();