{"id":15106972,"url":"https://github.com/normandy72/testing-directives","last_synced_at":"2026-02-16T18:34:56.527Z","repository":{"id":65528371,"uuid":"594009672","full_name":"Normandy72/Testing-Directives","owner":"Normandy72","description":"Testing AngularJS Directives. Coursera course \"Single Page Web Applications with AngularJS\" by Yaakov Chaikin.","archived":false,"fork":false,"pushed_at":"2023-01-27T12:17:52.000Z","size":130,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-10T20:39:04.178Z","etag":null,"topics":["angular","angularjs","html","html5","jasmine","jasmine-tests","javascript","js","testing"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Normandy72.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2023-01-27T11:36:19.000Z","updated_at":"2023-01-27T12:07:11.000Z","dependencies_parsed_at":"2023-02-15T09:01:33.181Z","dependency_job_id":null,"html_url":"https://github.com/Normandy72/Testing-Directives","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FTesting-Directives","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FTesting-Directives/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FTesting-Directives/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Normandy72%2FTesting-Directives/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Normandy72","download_url":"https://codeload.github.com/Normandy72/Testing-Directives/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247345703,"owners_count":20924098,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["angular","angularjs","html","html5","jasmine","jasmine-tests","javascript","js","testing"],"created_at":"2024-09-25T21:03:38.741Z","updated_at":"2025-10-06T12:16:26.949Z","avatar_url":"https://github.com/Normandy72.png","language":"JavaScript","readme":"# Testing AngularJS Directives\n### beforeEach Setup\n```\nvar $compile;\nvar $rootScope;\nvar expectedHtml = 'some html';\n\nbeforeEach(module('MyApp'));\n\nbeforeEach(inject(function(_$rootScope_, _$compile_){\n    $rootScope = _$rootScope_;\n    $compile = _$compile_;\n}));\n```\n### beforeEach Setup For Using templateUrl\n```\nbeforeEach(inject(function($templateCache){\n    var template = null;\n\n    var req = new XMLHttpRequest();\n    req.onload = function(){\n        template = this.responseText;\n    };\n\n    req.open(\"get\", \"template.thml\", false);\n    req.send();\n\n    $templateCache.put(\"template.html\", template);\n}));\n```\n### Test Method\n```\nit('should property insert content', function(){\n    var item = {name: \"John\"};\n    $rootScope.item = item;\n\n    var html = \"\u003cmy-directive item='item'\u003e\u003c/my-directive\u003e\";\n\n    var elem = $compile(html)($rootScope);\n\n    $rootScope.$digest();\n\n    expect(elem.html()).toContain(expectedHtml);\n});\n```\n***\n#### _Summary_\n* Use mock inject to inject `$rootScope` and `$compile` services in beforeEach.\n* If directive uses templateUrl, use some method to place the template contents into `$templateCache`.\n* In the test method, place whatever props are needed for the directive on the `$rootScope`.\n* Use `$compile` service to compile directive string and then using the returned function to bind with data by passing in `$rootScope`.\n* Call `$rootScope.digest()` to update the compiled HTML.\n* Retrieve produced HTML with `.html()` method and compare.\n***","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnormandy72%2Ftesting-directives","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnormandy72%2Ftesting-directives","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnormandy72%2Ftesting-directives/lists"}