用代理查询对HOC组分的类型进行酶单位测试

时间:2017-12-19 13:45:12

标签: reactjs unit-testing chai enzyme proxyquire

我有一个返回的函数取决于参数HOC componetns。我把这个组件改编成reduxForm HOC。我想用一种使用type()的酶测试它。该函数返回类型为FormValues的HOC组件,但如何在没有HOC的情况下返回测试。如何使用proxyquire进行测试

功能

int count;
    byte data[] = new byte[1024 * 4];
    long fileSize = body.contentLength();
    String type = body.contentType().subtype();
    Log.e("downloadFile: ", type);
    InputStream bis = new BufferedInputStream(body.byteStream(), 1024 * 8);
    File outputFile = DirectoryHelper.getInstance().createSongMp3(songName);
    OutputStream output = new FileOutputStream(outputFile);
    long total = 0;
    long startTime = System.currentTimeMillis();
    int timeCount = 1;
    while ((count = bis.read(data)) != -1) {
        total += count;
        totalFileSize = (int) (fileSize / (Math.pow(1024, 2)));
        double current = Math.round(total / (Math.pow(1024, 2)));
        int progress = (int) ((total * 100) / fileSize);
        long currentTime = System.currentTimeMillis() - startTime;
        if (currentTime > 1000 * timeCount) {
            timeCount++;
        }
        output.write(data, 0, count);
    }
    onDownloadComplete();
    output.flush();
    output.close();
    bis.close();

测试

export const getMiConfiguration = miConfigurationType => {
    switch (miConfigurationType) {
        case MiConfigurationTypes.WanderingDetection :
        case MiConfigurationTypes.MuteWanderingDetection:
            return <WanderingDetection />

        case MiConfigurationTypes.OpenWanderingControl :
        case MiConfigurationTypes.LockedWanderingControl:
            return <WanderingControl />

        default:
            return null
    }
}

组件的例子

describe('getMiConfiguration', () => {
    ;[{id: MiConfigurationTypes.AccessPointOnly, component: null},
        {id: MiConfigurationTypes.WanderingDetection, component: <WanderingDetection/>},
        {id: MiConfigurationTypes.MuteWanderingDetection, component: <WanderingDetection/>},
        {id: MiConfigurationTypes.LockedWanderingControl, component: <WanderingControl/>},
        {id: MiConfigurationTypes.OpenWanderingControl, component: <WanderingControl/>},
    ].forEach(({id, component}) =>
        it(`should render correct ${component} component for ${id} type`, () => {
            const result = getMiConfiguration(id)

            if (component === null)
                expect(result).to.be.null
            else
                result.type.should.be.equal(component.type)
        }))
})

1 个答案:

答案 0 :(得分:0)

我找到了使用proxyquire的解决方案来覆盖导出

const {getMiConfiguration} = proxyquire('../../../../src/components/devices/miConfiguration', {
    './wanderingDetection': {default: <WanderingDetection/>},
    './wanderingControl': {default: <WanderingControl/>},
})