Navel Piercing Madness

    karma-nunjucks

    0.2.1 • Public • Published

    karma-nunjucks

    Nunjucks for Karma

    Uses html2js preprocessor ("karma-html2js-preprocessor") or an ordinary template string to compile nunjucks templates using any version of nunjucks.

    Build Status

    Code Climate

    Coverage Status

    Dependencies

    NPM version

    Basic Example

    Nunjucks is now available to your tests.

     
    describe("nunjucks example test", function () {
        var template;
     
        before(function () {
            template = nunjucks.compile('<div class="{{ thing }}"></div>');
        })
     
        it('Thing has class', function () {
            var result = template.render({thing: "something"});
            document.body.innerHTML += result;
            expect(document.querySelector('.something')).to.exist;
        });;    
    });
     

    Example with templates

    A new convenience method is available in nunjucks environments to make templates available called getPreprocessedTemplate.

     
    describe("nunjucks example test", function () {
        var template, env;
     
        before(function () {
            env = new nunjucks.Environment();
            template = env.getPreprocessedTemplate('index.html');
        })
     
        it('Thing has class', function () {
            var result = template.render({thing: "something"});
            document.body.innerHTML += result;
            expect(document.querySelector('.something')).to.exist;
        });
     
        it('Thing has widget class', function () {
            var $el = $(template.render({widget: {items: []}}));
            var thing = new Thing($el);
            expect($el.find('.widget').length).to.equal(1);
        });
     
        it('Thing has widget class', function () {
            var $el = $(template.render({widget: {items: [{label: "Item 1", tag: "itemTag"}]}}));
            var thing = new Thing($el);
            expect($el.find('.widget').find('li[data-filter]').length).to.equal(1);
        });
    });
     

    Example with mocked filters

    Sometimes it's useful to mock filters that access the disk or other resources. Use mockFilter.

     
    describe("nunjucks example test", function () {
        var template, env;
     
        before(function () {
            env = new nunjucks.Environment();
            
            //this has a filter called test
            template = env.getPreprocessedTemplate('index.html'); 
        })
     
        it('Thing has widget class', function () {
            var noop = function () { return 'test string' };
            env.mockFilter('test', noop); //mock the filter
            expect(template.render()).to.contain('test string');
        });
    });
     

    Install

    npm i karma-nunjucks

    DownloadsWeekly Downloads

    0

    Version

    0.2.1

    License

    ISC

    Last publish

    Collaborators

    • danestuckel