npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mojito-markup-test

v0.1.1

Published

Utility for view markup validation in mojito unit tests.

Downloads

6

Readme

Mojito markup test utility

Build Status

Build Status

Overview

The markup layout test utility will render the mojit view with mock data and expose the resulting markup to be validated using YUI Node module.

Markup tests written with this utility will be run as regular mojito unit tests using mojito test command

Usage

Three easy steps:

  1. Require mojito-markup-test.
  2. Add a mojit spec.
  3. Render view and validate markup.

Mojit spec

You must specify the mojit and view to render, along with the mock data to use.

You can mock any data, including parameters, configuration, request and headers.

//describe the mojit to render
spec = {
    //mojit type (dir name)
    type: 'SimpleMojit',

    //action to test
    action: 'index',

    //mojit params
    params: {
        url: {
            p: 'Really!'
        }
    },

    //mojit config (to use with ac.config.get() )
    appConfig: {
        foo: 'bar'
    },

    //mock request/response and headers data
    req: {
        p: 'flowers'
    },
    res: {},
    headers: {
         'Content-Type': 'text/html'
    }
};

Rendering views

Once you have the mojit spec, you can render the view invoking the utility's render method.

render (spec, callback)

Receives the mojit spec and a callback function which will get the error (if any), the rendered markup string, the mojit view meta object and the node (YUI Node) for the resulting markup.

MarkupTest.render(spec, function (err, markup, meta, node) {

    //rendering done, resume paused test and verify
    test.resume(function () {
        var div, em, i, a, text;

        A.isNull(err);
        //we have markup
        A.areNotEqual('', markup, 'Markup');
        //we have meta
        A.isNotNull(meta, 'Meta');

        //we have a node
        A.isNotUndefined(node, 'Parent node');
        A.isNotNull(node, 'Parent node');

        //use YUI Node methods to access rendered elements
        div = node.one('.msg');
        A.isNotUndefined(div, 'msg <div>');
        A.isNotNull(div, 'msg <div>');
    });

Full example

YUI.add('CompositeMojit_view-tests', function(Y) {

    var suite = new YUITest.TestSuite('CompositeMojit'),
        A = YUITest.Assert,
        MarkupTest = Y.mojito.MarkupTest,

        //Timeout for async testing
        TIMEOUT = 1000;

    suite.add(new YUITest.TestCase({

        name: 'view',

        'test index': function () {

            var test = this,

                //describe the mojit to render
                spec = {
                    //mojit type (dir name)
                    type: 'CompositeMojit',

                    //action to test
                    action: 'index',

                    //mojit params
                    params: {
                        url: {
                            p: 'Really!'
                        }
                    },

                    //mojit config (to use with ac.config.get() )
                    config: {
                        foo: 'bar'
                    },

                    //app level config (to use with ac.app)
                    appConfig: {
                        some: 'config param'
                    },

                    // We need to define the child mojits,
                    // modules should be in the requires: [] as well to be loaded (see below)
                    children: {
                        'child_mojit': {
                            type: 'ChildMojit'
                        }
                    }
                };

            MarkupTest.render(spec, function (err, markup, meta, node) {

                //rendering done, resume paused test and verify
                test.resume(function () {
                    var div, em, i, a, text;

                    A.isNull(err);
                    //we have markup
                    A.areNotEqual('', markup, 'Markup');
                    //we have meta
                    A.isNotNull(meta, 'Meta');

                    //we have a node
                    A.isNotUndefined(node, 'Parent node');
                    A.isNotNull(node, 'Parent node');

                    div = node.one('.msg');
                    A.isNotUndefined(div, 'msg <div>');
                    A.isNotNull(div, 'msg <div>');

                    em = div.one('em');
                    A.isNotUndefined(em, '<em>');
                    A.isNotNull(em, '<em>');

                    text = em.getContent();
                    A.areEqual('Mojito is working.', text, 'Rendered message <em>');

                    i = div.one('i');
                    A.isNotUndefined(i, '<i>');
                    A.isNotNull(i, '<i>');

                    text = i.getContent();
                    A.areEqual(spec.params.url.p, text, 'Rendered message <i>');

                    a = div.one('a');
                    A.isNotUndefined(a, '<a>');
                    A.isNotNull(a, '<a>');

                    text = a.getContent();
                    A.areEqual(spec.config.foo, text, 'Rendered message <a>');
                });
            });

            //we need this for async tests, wait till rendering done or timeout
            test.wait(TIMEOUT);
        }

    }));

    YUITest.TestRunner.add(suite);

}, '0.0.1', {requires: [
    'mojito-test',

    //test rendering utility
    'mojito-markup-test'

    //parent mojit
    'CompositeMojit',

    //child mojit
    'ChildMojit'
]});

See more examples.