首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在CycleJs测试中在模拟的DOM源中流事件

无法在CycleJs测试中在模拟的DOM源中流事件
EN

Stack Overflow用户
提问于 2018-12-11 12:06:26
回答 1查看 44关注 0票数 1

给定使用CycleJs构建的独立组件(该组件工作良好):

代码语言:javascript
复制
import isolate from '@cycle/isolate';
import { div, ul, li, a } from '@cycle/dom';

function intent(domSource){
    const changeString$ =  domSource
        .select('.string').events('click')
        .map( ev => ev.target.dataset.position);

    return { changeString$ };
}

function model(actions, props$){

    const { changeString$ } = actions;

    return props$.map( props => {

        return changeString$
            .startWith(props.initialString)
            .map( activePosition => ({ activePosition, preset : props.preset }));

    }).flatten().remember();
}

function view(state$){
    return state$.map( state => (
        div(
            ul(
                state.preset.map( (string, position) => (
                    li(
                        a({
                            attrs : {
                                href : '#',
                                'data-pitch' : string.pitch,
                                'data-frequency' : string.frequency,
                                'data-position' : position,
                                class : ['string', parseInt(state.activePosition, 10) === position ? 'active' : ''].join(' ')
                            }
                        },
                        string.name)
                    )
                ))
            )
        )
    ));
}

function stringSelector(sources){
    const actions = intent(sources.DOM);
    const state$  = model(actions, sources.props);
    const vdom$   = view(state$);

    return {
        DOM: vdom$,
        value: state$
    };
}

export default isolate(stringSelector, '.string-selector');

我尝试使用@cycle/time测试这种行为:

代码语言:javascript
复制
import test from 'tape';
import xs   from 'xstream';
import { mockDOMSource } from '@cycle/dom';
import { mockTimeSource } from '@cycle/time';
import stringSelector from '../../../src/js/component/stringSelector/index.js';

test('string selector', t => {

    t.plan(1);

    const Time = mockTimeSource();

    const e2Click$       = Time.diagram('-------x-------|');
    const a2Click$       = Time.diagram('---x------x----|');
    const expectedState$ = Time.diagram('0--1---0--1----|');

    const DOM = mockDOMSource({
        '.string[data-picth=e2]': {
            click: e2Click$
        },
        '.string[data-picth=a2]': {
            click: a2Click$
        },
    });

    const selector = stringSelector({
        DOM,
        props: xs.of({
            preset: {
                strings: [{
                    name: 'E',
                    pitch: 'e2',
                    frequency: 82.41
                }, {
                    name: 'A',
                    pitch: 'a2',
                    frequency: 110.0
                }]
            },
            initialString: 0
        })
    });

    const activePosition$ = selector.value.map( state => state.activePosition );

    Time.assertEqual(activePosition$, expectedState$);

    Time.run(t.end.bind(t));
});

但是activePosition$流直接结束。我不知道它是来自于模拟DOM的方式(事件似乎没有被触发)还是我构建activePosition$流的方式?

在运行我的测试时,我收到以下消息:

代码语言:javascript
复制
Expected

0--1---0--1----|

Got

(0|)

Failed because:

 * Length of actual and expected differs
 * Expected type next at time 0 but got complete
 * Expected stream to complete at 60 but completed at 0
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-11 12:16:30

我想我发现了问题。

问题是,使用模拟的DOM驱动程序,您需要为与DOM.select('...').events中使用的选择器完全相同的选择器创建事件。

在您的示例中,您选择了.string,但您在.string[data-pitch=..]上模拟了一个事件,该事件在应用程序的一侧将不匹配。

请记住,测试端不涉及真正的DOM。在您的示例中,当您伪造对选择器.string的单击时,无论您的组件呈现什么,它只会生成一个事件。

我认为你想在这里实现的是伪造事件中的数据。

你也许可以这样做:

代码语言:javascript
复制
const clicks = Time.diagram('---0---1----0---|').map(position => {
    // this will create a fake DOM event that contains the properties you are reading
    return {target: {dataset: {position: position }}}
})

const DOM = mockDOMSource({
    '.string': {
        click: clicks
    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53723851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档