cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              mocha.setup('bdd')
var expect = chai.expect;
var h = maquette.h;
var exports = {};

// Complete sourcecode from maquette-query 0.6.3 (This can be collapsed)
(function(){
// Helper functions
// ----------------
var makeSelectorFunction = function (selector) {
    if (typeof selector === 'string') {
        return function (vNode) {
            var index = vNode.vnodeSelector.indexOf(selector);
            if ((selector[0] === '.' || selector[0] === '#') ? (index > 0) : (index === 0)) {
                var nextChar = vNode.vnodeSelector.charAt(index + selector.length);
                return !nextChar || nextChar === '.' || nextChar === '#';
            }
            return false;
        };
    }
    else if (typeof selector === 'function') {
        return selector;
    }
    else {
        throw new Error('Invalid selector ' + selector);
    }
};
var filterDescendants = function (root, predicate) {
    var results = [];
    var visit = function (vnodeTree) {
        if (vnodeTree.children) {
            vnodeTree.children.forEach(function (child) {
                if (predicate(child)) {
                    results.push(child);
                }
                visit(child);
            });
        }
    };
    visit(root);
    return results;
};
// Simulator
var collectTextContent = function (vnodeTree, results) {
    if (vnodeTree.vnodeSelector === '') {
        results.push(vnodeTree.text);
    }
    else {
        if (vnodeTree.text) {
            results.push(vnodeTree.text);
        }
        if (vnodeTree.children) {
            vnodeTree.children.forEach(function (child) {
                collectTextContent(child, results);
            });
        }
    }
    return results;
};
var createEvent = function (target) {
    var result = {
        defaultPrevented: false,
        propagationStopped: false,
        preventDefault: function () {
            result.defaultPrevented = true;
        },
        stopPropagation: function () {
            result.propagationStopped = true;
        },
        target: target,
        currentTarget: target
    };
    return result;
};
var createKeyEvent = function (which, target) {
    var event = createEvent(target);
    event.which = which;
    event.keyCode = which;
    return event;
};
var createMouseEvent = function (target, parameters) {
    var event = createEvent(target);
    if (parameters) {
        event.pageX = parameters.pageX;
        event.pageY = parameters.pageY;
    }
    return event;
};
var createFocusEvent = function (target) {
    return createEvent(target);
};
var getKeyCode = function (keyCodeOrChar) {
    return typeof keyCodeOrChar === 'number' ? keyCodeOrChar : keyCodeOrChar.charCodeAt(0);
};
var createSimulator = function (vnode, defaultFakeDomNode) {
    var properties = vnode.properties;
    return {
        keyDown: function (keyCode, fakeDomNode) {
            var event = createKeyEvent(getKeyCode(keyCode), fakeDomNode || defaultFakeDomNode);
            properties.onkeydown(event);
            return event;
        },
        keyUp: function (keyCode, fakeDomNode) {
            var event = createKeyEvent(getKeyCode(keyCode), fakeDomNode || defaultFakeDomNode);
            properties.onkeyup(event);
            return event;
        },
        mouseDown: function (fakeDomNode, parameters) {
            var event = createMouseEvent(fakeDomNode || defaultFakeDomNode, parameters);
            properties.onmousedown(event);
            return event;
        },
        mouseUp: function (fakeDomNode, parameters) {
            var event = createMouseEvent(fakeDomNode || defaultFakeDomNode, parameters);
            properties.onmouseup(event);
            return event;
        },
        click: function (fakeDomNode, parameters) {
            var event = createMouseEvent(fakeDomNode || defaultFakeDomNode, parameters);
            properties.onclick(event);
            return event;
        },
        input: function (fakeDomNode) {
            var event = createEvent(fakeDomNode || defaultFakeDomNode);
            properties.oninput(event);
            return event;
        },
        change: function (fakeDomNode) {
            var event = createEvent(fakeDomNode || defaultFakeDomNode);
            properties.onchange(event);
            return event;
        },
        focus: function (fakeDomNode) {
            var event = createFocusEvent(fakeDomNode || defaultFakeDomNode);
            properties.onfocus(event);
            return event;
        },
        blur: function (fakeDomNode) {
            var event = createFocusEvent(fakeDomNode || defaultFakeDomNode);
            properties.onblur(event);
            return event;
        },
        keyPress: function (keyCodeOrChar, valueBefore, valueAfter, fakeDomNode) {
            var target = (fakeDomNode || defaultFakeDomNode || {});
            var keyCode = typeof keyCodeOrChar === 'number' ? keyCodeOrChar : keyCodeOrChar.charCodeAt(0);
            target.value = valueBefore;
            if (properties.onkeydown) {
                properties.onkeydown(createKeyEvent(keyCode, target));
            }
            target.value = valueAfter;
            if (properties.onkeyup) {
                properties.onkeyup(createKeyEvent(keyCode, target));
            }
            if (properties.oninput) {
                properties.oninput(createEvent(target));
            }
        }
    };
};
// The create methods
var createCollectionQuery;
var createQuery = function (getVNode) {
    var query = function (selector, fakeDomNode) {
        var predicate = makeSelectorFunction(selector);
        return createQuery(function () { return filterDescendants(getVNode(), predicate)[0]; });
    };
    var queryAll = function (selector) {
        var predicate = makeSelectorFunction(selector);
        return createCollectionQuery(function () { return filterDescendants(getVNode(), predicate); });
    };
    var getResult = function () {
        var result = getVNode();
        if (!result) {
            throw new Error('Query did not match a VNode');
        }
        return result;
    };
    var targetDomNode;
    return {
        execute: getResult,
        exists: function () { return !!getVNode(); },
        query: query,
        queryAll: queryAll,
        get textContent() {
            return collectTextContent(getResult(), []).join('');
        },
        get vnodeSelector() {
            return getResult().vnodeSelector;
        },
        get properties() {
            return getResult().properties;
        },
        get children() {
            return getResult().children;
        },
        getChild: function (index) {
            return createQuery(function () {
                return getResult().children[index];
            });
        },
        /**
         * A small facade that allows firing of simple events and sequences of events for common usecases.
         * It is not meant to be exhaustive.
         * If you need to simulate something that is not in here, you can simply invoke query(...).properties.on???() yourself.
         */
        get simulate() { return createSimulator(getResult(), targetDomNode); },
        setTargetDomNode: function (target) {
            targetDomNode = target;
        },
        getTargetDomNode: function () {
            return targetDomNode;
        }
    };
};
createCollectionQuery = function (getVNodes) {
    return {
        execute: getVNodes,
        getResult: function (index) {
            return createQuery(function () {
                return getVNodes()[index];
            });
        },
        get length() {
            return getVNodes().length;
        }
    };
};
/**
 * Creates a test projector which implements the Query interface
 * @param renderMaquetteFunction The renderMaquette function that is used to produce the VNode tree.
 * Optional, when not specified, you must use the initialize function to supply the renderMaquetteFunction.
 */
exports.createTestProjector = function (renderMaquetteFunction) {
    var createVNode = function () {
        if (!renderMaquetteFunction) {
            throw new Error('TestProjector was not initialized');
        }
        return renderMaquetteFunction();
    };
    var initialize = function (initializeRenderMaquetteFunction) {
        renderMaquetteFunction = initializeRenderMaquetteFunction;
    };
    var result = createQuery(createVNode);
    result.initialize = initialize;
    return result;
};

//# sourceMappingURL=index.js.map
  
}());

// Creates a button component
function createMyButton(text, onClick) {
  var handleClick = function(evt) {
    evt.preventDefault();
    onClick();
  };

  var myButton = {
    renderMaquette: function() {
      return h("my-button", {key: myButton}, [ 
        h("button", { onclick: handleClick }, [text])
      ]);
    }
  };
  return myButton;
}

describe('myButton', function() {

  var projector = exports.createTestProjector(); // Special projector used in unit tests
  var myButtonComponent = undefined; // We create a fresh instance for each test in the beforeEach
  var button = projector.query('button'); // A query which will find the <button> element
  var clickHandler = sinon.stub(); 
  
  beforeEach(function() {
    clickHandler.reset();
    myButtonComponent = createMyButton('ButtonText', clickHandler);
    projector.initialize(myButtonComponent.renderMaquette);
  });

  it('displays the configured text', function() {
    expect(button.textContent).to.equal('ButtonText')
  });
  
  it('calls the handler when clicked', function() {
    button.simulate.click();
    expect(clickHandler.callCount).to.equal(1);
  });

});

mocha.run();

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console