|
| 1 | +import { Angular } from "../../angular.js"; |
| 2 | +import { dealoc } from "../../shared/dom.js"; |
| 3 | +import { wait } from "../../shared/test-utils.js"; |
| 4 | +import { ngElDirective } from "./el.js"; |
| 5 | + |
| 6 | +describe("ngEl", () => { |
| 7 | + let $compile, $rootScope, el, $log; |
| 8 | + |
| 9 | + beforeEach(() => { |
| 10 | + el = document.getElementById("app"); |
| 11 | + dealoc(el); |
| 12 | + el.innerHTML = ""; |
| 13 | + |
| 14 | + const angular = new Angular(); |
| 15 | + angular.module("default", []); |
| 16 | + |
| 17 | + angular |
| 18 | + .bootstrap(el, ["default"]) |
| 19 | + .invoke((_$compile_, _$rootScope_, _$log_) => { |
| 20 | + $compile = _$compile_; |
| 21 | + $rootScope = _$rootScope_; |
| 22 | + $log = _$log_; |
| 23 | + }); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should attach element to scope.$target by id when no expression is provided", async () => { |
| 27 | + el.innerHTML = `<div id="foo" ng-el></div>`; |
| 28 | + $compile(el)($rootScope); |
| 29 | + await wait(); |
| 30 | + |
| 31 | + expect($rootScope.$target.foo).toBeDefined(); |
| 32 | + expect($rootScope.$target.foo instanceof HTMLElement).toBe(true); |
| 33 | + expect($rootScope.$target.foo.id).toBe("foo"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("should attach element to scope.$target using ng-el value as key", async () => { |
| 37 | + el.innerHTML = `<div id="bar" ng-el="myEl"></div>`; |
| 38 | + $compile(el)($rootScope); |
| 39 | + await wait(); |
| 40 | + |
| 41 | + expect($rootScope.$target.myEl).toBeDefined(); |
| 42 | + expect($rootScope.$target.myEl.id).toBe("bar"); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should support multiple ng-el elements", async () => { |
| 46 | + el.innerHTML = ` |
| 47 | + <div id="a" ng-el="first"></div> |
| 48 | + <div id="b" ng-el="second"></div> |
| 49 | + <div id="c" ng-el></div> |
| 50 | + `; |
| 51 | + |
| 52 | + $compile(el)($rootScope); |
| 53 | + await wait(); |
| 54 | + |
| 55 | + expect(Object.keys($rootScope.$target)).toContain("first"); |
| 56 | + expect(Object.keys($rootScope.$target)).toContain("second"); |
| 57 | + expect(Object.keys($rootScope.$target)).toContain("c"); |
| 58 | + expect($rootScope.$target.first.id).toBe("a"); |
| 59 | + expect($rootScope.$target.second.id).toBe("b"); |
| 60 | + expect($rootScope.$target.c.id).toBe("c"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should not throw if $target is not defined on scope", async () => { |
| 64 | + el.innerHTML = `<div id="noTarget" ng-el="missing"></div>`; |
| 65 | + |
| 66 | + // no $target defined on scope |
| 67 | + expect(() => { |
| 68 | + $compile(el)($rootScope); |
| 69 | + }).not.toThrow(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should override previous entries with the same key", async () => { |
| 73 | + el.innerHTML = ` |
| 74 | + <div id="x1" ng-el="dup"></div> |
| 75 | + <div id="x2" ng-el="dup"></div> |
| 76 | + `; |
| 77 | + $rootScope.$target = {}; |
| 78 | + |
| 79 | + $compile(el)($rootScope); |
| 80 | + await wait(); |
| 81 | + |
| 82 | + expect($rootScope.$target.dup.id).toBe("x2"); |
| 83 | + }); |
| 84 | + |
| 85 | + it("should remove reference from scope.$target when element is removed", async () => { |
| 86 | + el.innerHTML = `<div id="temp" ng-el="tempEl"></div>`; |
| 87 | + $compile(el)($rootScope); |
| 88 | + await wait(); |
| 89 | + |
| 90 | + expect($rootScope.$target.tempEl).toBeDefined(); |
| 91 | + const elem = $rootScope.$target.tempEl; |
| 92 | + |
| 93 | + // simulate element removal and scope destruction |
| 94 | + elem.remove(); |
| 95 | + await wait(); |
| 96 | + |
| 97 | + expect($rootScope.$target.tempEl).toBeUndefined(); |
| 98 | + }); |
| 99 | +}); |
0 commit comments