Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

How to write test cases for angular js directive with template url and link function

I need to write a unit test case for angular js directive with template url and link function below. i have tried but not working out. Can anyone please give me an idea to write a unit testcase to complete it. I am using Angular JS and Jasmine for testing.

HTML file

<!-- adPage.html --> <!DOCTYPE html> <html> <head> <link rel='stylesheet' href='style.css'/> <script src='script.js'></script> </head> <body> <h1><!-- @Title --></h1>

<p>Promoted post by <!-- @Author --> on <!-- @Datetime --></p>

<article> <!-- @Content --> </article> </body> </html>

<!-- Directive Used Page --> <div id="content" adDirective> </div>

Script file.js

// Script for Directive

(function () { 'use strict';

angular.module('adModule', [
]).directive('addirective', ['$timeout', '$document', function ($timeout, $document) {
    return {
        restrict: 'A',
        link: link,
        templateUrl: 'adpage.html'
    };

    function link (scope, element, attrs) {
        var document = $document[0];
        var head = document.head;

        var meta, script;

        var timeoutId = $timeout(function () {
            meta = document.createElement('meta');
            meta.setAttribute('name', 'robots');
            meta.setAttribute('content', 'noindex, nofollow');

            script = document.createElement('script');
            script.setAttribute('type', 'text/javascript');
            script.setAttribute('async', 'async');
            script.setAttribute('defer', 'defer');
            script.setAttribute('crossorigin', 'anonymous');
            script.setAttribute('src', '//adyoulike.omnitagjs.com');

            document.head.appendChild(meta);
            document.head.appendChild(script);
        }, 200);

        element.on('$destroy', function () {
            $timeout.cancel(timeoutId);
            head.removeChild(meta);
            head.removeChild(script);
        });
    }
}]);

})();