是否可以使用ApolloTestingModule重置缓存?

时间:2019-07-03 22:43:25

标签: apollo apollo-client apollo-angular

我有一个使用Apollo Angular客户端来获取一些数据的组件。我正在尝试为其编写测试,以检查它如何响应不同类型的数据。

但是,似乎确实有一种方法可以在第一次之后更改响应数据(我认为是由于缓存)。有没有办法在测试时清除缓存或强制其不使用缓存?

我的代码看起来像这样

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',
            },
        });
    });
});

1 个答案:

答案 0 :(得分:0)

我发现您可以做类似的事情

const apolloClient = TestBed.get(Apollo).getClient();
await apolloClient.clearStore();
相关问题