本文整理匯總了TypeScript中@cycle/run.setup函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript setup函數的具體用法?TypeScript setup怎麽用?TypeScript setup使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了setup函數的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: it
it('should create a location from pathname', function(done) {
function main(_sources: {history: Stream<Location>}) {
return {
history: xs.of('/test'),
};
}
const {sources, run} = setup(main, {history: makeHistoryDriver()});
sources.history.drop(1).subscribe({
next(location: Location) {
assert.strictEqual(location.pathname, '/test');
done();
},
error: done,
complete: () => {
done('complete should not be called');
},
});
dispose = run();
});
示例2: it
it('should setup click detection on a ready DOM element (e.g. from server)', function(
done,
) {
function app(sources: {DOM: DOMSource}) {
return {
DOM: xs.never(),
};
}
const containerElement = createRenderTarget();
const headerElement = document.createElement('H3');
headerElement.className = 'myelementclass';
headerElement.textContent = 'Foobar';
containerElement.appendChild(headerElement);
const {sinks, sources, run} = setup(app, {
DOM: makeDOMDriver(containerElement),
});
const dispose = run();
sources.DOM.select('.myelementclass').events('click').addListener({
next: (ev: Event) => {
assert.strictEqual(ev.type, 'click');
assert.strictEqual((ev.target as HTMLElement).textContent, 'Foobar');
dispose();
done();
},
});
// Make assertions
setTimeout(() => {
const myElement = containerElement.querySelector(
'.myelementclass',
) as HTMLElement;
assert.notStrictEqual(myElement, null);
assert.notStrictEqual(typeof myElement, 'undefined');
assert.strictEqual(myElement.tagName, 'H3');
assert.doesNotThrow(function() {
setTimeout(() => myElement.click());
});
}, 200);
});
示例3: it
it('should catch interaction events without prior select()', function(done) {
function app(_sources: {DOM: DOMSource}) {
return {
DOM: xs.of(div('.parent', [h3('.myelementclass', 'Foobar')])),
};
}
const {sinks, sources, run} = setup(app, {
DOM: makeDOMDriver(createRenderTarget()),
});
let dispose: any;
sources.DOM.events('click').addListener({
next: (ev: Event) => {
assert.strictEqual(ev.type, 'click');
assert.strictEqual((ev.target as HTMLElement).textContent, 'Foobar');
dispose();
done();
},
});
sources.DOM.select(':root')
.element()
.drop(1)
.take(1)
.addListener({
next: (root: Element) => {
const myElement = root.querySelector(
'.myelementclass'
) as HTMLElement;
assert.notStrictEqual(myElement, null);
assert.notStrictEqual(typeof myElement, 'undefined');
assert.strictEqual(myElement.tagName, 'H3');
assert.doesNotThrow(function() {
setTimeout(() => myElement.click());
});
},
});
dispose = run();
});
示例4: it
it('should allow going back/forwards with `go`, `goBack`, `goForward`', function(
done,
) {
function main(sources: {history: Stream<Location>}) {
return {
history: xs
.periodic(100)
.take(6)
.map(
i =>
[
'/test',
'/other',
{type: 'go', amount: -1},
{type: 'go', amount: +1},
{type: 'goBack'},
{type: 'goForward'},
][i],
),
};
}
const {sources, run} = setup(main, {history: makeHistoryDriver()});
const expected = ['/test', '/other', '/test', '/other', '/test', '/other'];
sources.history.drop(1).subscribe({
next(location: Location) {
assert.strictEqual(location.pathname, expected.shift());
if (expected.length === 0) {
done();
}
},
error: done,
complete: () => {
done('complete should not be called');
},
});
dispose = run();
});
示例5: it
it('should not select element outside the given scope', function(done) {
function app(sources: {DOM: MainDOMSource}) {
return {
DOM: xs.of(
h3('.top-most', [
h2('.bar', 'Wrong'),
div('.foo', [h4('.bar', 'Correct')]),
]),
),
};
}
const {sinks, sources, run} = setup(app, {
DOM: makeDOMDriver(createRenderTarget()),
});
let dispose: any;
// Make assertions
sources.DOM
.select('.foo')
.select('.bar')
.elements()
.drop(1)
.take(1)
.addListener({
next: (elements: Array<Element>) => {
assert.strictEqual(elements.length, 1);
const element = elements[0];
assert.notStrictEqual(element, null);
assert.notStrictEqual(typeof element, 'undefined');
assert.strictEqual(element.tagName, 'H4');
assert.strictEqual(element.textContent, 'Correct');
setTimeout(() => {
dispose();
done();
});
},
});
dispose = run();
});
示例6: it
it('should allow DOM.select()ing its own root without classname or id', function (done) {
function app(sources: {DOM: MainDOMSource}) {
const child$ = sources.DOM.isolateSink(xs.of(
span([
h4('.bar', 'Wrong'),
]),
), 'ISOLATION');
return {
DOM: child$.map(child =>
h3('.top-most', [
child,
]),
),
};
}
const {sinks, sources, run} = setup(app, {
DOM: makeDOMDriver(createRenderTarget()),
});
const {isolateSource} = sources.DOM;
isolateSource(sources.DOM, 'ISOLATION')
.select('span').elements()
.drop(1).take(1)
.addListener({
next: (elements: Array<Element>) => {
assert.strictEqual(Array.isArray(elements), true);
assert.strictEqual(elements.length, 1);
const correctElement = elements[0];
assert.notStrictEqual(correctElement, null);
assert.notStrictEqual(typeof correctElement, 'undefined');
assert.strictEqual(correctElement.tagName, 'SPAN');
done();
},
});
run();
});
示例7: it
it('should catch a blur event by default (no options)', function (done) {
function app(sources: {DOM: DOMSource}) {
return {
DOM: xs.of(div('.parent', [
input('.correct', {type: 'text'}, []),
input('.wrong', {type: 'text'}, []),
input('.dummy', {type: 'text'}),
])),
};
}
if (!document.hasFocus()) {
done();
} else {
const {sinks, sources, run} = setup(app, {
DOM: makeDOMDriver(createRenderTarget()),
});
sources.DOM.select('.correct').events('blur').addListener({
next: (ev: Event) => {
assert.strictEqual(ev.type, 'blur');
assert.strictEqual((ev.target as HTMLElement).className, 'correct');
done();
},
});
sources.DOM.select(':root').elements().drop(1).take(1).addListener({
next: (root: Element) => {
const correct = root.querySelector('.correct') as HTMLElement;
const wrong = root.querySelector('.wrong') as HTMLElement;
const dummy = root.querySelector('.dummy') as HTMLElement;
setTimeout(() => wrong.focus(), 50);
setTimeout(() => dummy.focus(), 100);
setTimeout(() => correct.focus(), 150);
setTimeout(() => dummy.focus(), 200);
},
});
run();
}
});
示例8: it
it('should allow isolatedDOMSource.events() to work without crashing', function(
done,
) {
function app(sources: {DOM: MainDOMSource}) {
return {
DOM: xs.of(
h3('.top-most', [div({isolate: 'foo'}, [h4('.bar', 'Hello')])]),
),
};
}
const {sinks, sources, run} = setup(app, {
DOM: makeDOMDriver(createRenderTarget()),
});
let dispose: any;
const isolatedDOMSource = sources.DOM.isolateSource(sources.DOM, 'foo');
isolatedDOMSource.events('click').addListener({
next: (ev: Event) => {
dispose();
done();
},
});
// Make assertions
isolatedDOMSource.select('div').elements().drop(1).take(1).addListener({
next: (elements: Array<Element>) => {
assert.strictEqual(elements.length, 1);
const correctElement = elements[0] as HTMLElement;
assert.notStrictEqual(correctElement, null);
assert.notStrictEqual(typeof correctElement, 'undefined');
assert.strictEqual(correctElement.tagName, 'DIV');
assert.strictEqual(correctElement.textContent, 'Hello');
setTimeout(() => {
correctElement.click();
});
},
});
dispose = run();
});
示例9: it
it('should output HTMLSource as an adapted stream', function(done) {
type MySources = {
html: HTMLSource;
};
type MySinks = {
html: Stream<VNode>;
};
function app(sources: MySources): MySinks {
return {
html: xs.of(div('.test-element', ['Foobar'])),
};
}
const {sources} = setup(app, {
html: makeHTMLDriver((html: string) => {}),
});
assert.strictEqual(
typeof (sources.html.elements() as any).imitate,
'function',
);
done();
});