测试链式承诺(Jasmine,React,Karma)

时间:2017-05-10 00:03:38

标签: javascript unit-testing typescript jasmine

我在目前的项目中遇到过几种情况,我有一连串的承诺,我不确定如何处理。

以下是相关的代码块:

    return this.axios.get(path, requestOpts)
        .then((response) => {console.log('did authorize: ', response); return response})
        .then((response) => {
            if (response.data.ok) {
                window.localStorage.setItem(path, JSON.stringify(response.data));
                console.log("Setting localStorage item ", path, response.data);

                return response.data.payloadUrl;
            } else {
                console.error("Non-ok response for ", path, response.data);
                const resp: DisplayTokenResponse = response.data;

                //TODO: reject promise?
                if (resp.status === "AUTHENTICATION_REQUIRED") {
                    this.axiosService.goToLoginPage(window.location + '');
                }

                Promise.reject(response.data.message);
            }
        });

我的测试(到目前为止)看起来像这样:

describe('.authorize()', () => {
    let axiosSpy: jasmine.Spy;

    beforeEach((done) => {
        spyOn(svc, 'keyPath').and.returnValue(path);
        spyOn(svc, 'storedToken').and.returnValue(stored);

        let response = {
            data: {
                ok: true,
                message: 'test-response',
                payloadUrl: 'http://payload-url.com'
            }
        }



        spyOn(svc.axios, 'get').and.callFake(
            (path:string, reqOpts:AxiosRequestConfig) => {
                return new Promise(() => {
                    response
                });
            }, (e) => {
                console.log(`failed`);
            });
    });

    describe('should authorize user', () => {
        it('when supplied a STRING', () => {
            clientId = clientId_string;
        });
        it('when supplied a NUMBER', () => {
            clientId = clientId_number;
        });

        afterEach((done) => {
            svc.authorize(clientId, locationId, screenId).then((result) => {
                console.log(`result ${result}`);
                done();
            }, (e) => {
                console.log(`failed with error ${e}`);
                done();
            });
        });
    });
});

我可以测试一个级别的承诺,但是如何设置我的测试以便能够处理这样的情况?

1 个答案:

答案 0 :(得分:0)

终于搞清楚了。我认为这源于创建Promise实例与其解析器之间的混淆。

新的beforeEach块看起来像这样:

    beforeEach(() => {
        spyOn(svc, 'keyPath').and.returnValue(path);
        spyOn(svc, 'storedToken').and.returnValue(stored);

        let axiosPromise = new Promise((resolve, reject) => {
            var responseData = {
                data: {
                    ok: true,
                    message: 'test-response',
                    payloadUrl: 'http://payload-url.com'
                }
            };

            resolve(responseData);
        });

        spyOn(svc.axios, 'get').and.callFake(
            ()=>{
                return axiosPromise;
            }
        );
    });

我的测试现在通过了。