|
| 1 | +import { Angular } from "../../angular.js"; |
| 2 | +import { dealoc } from "../../shared/dom.js"; |
| 3 | +import { wait } from "../../shared/test-utils.js"; |
| 4 | + |
| 5 | +describe("ngListenerDirective", () => { |
| 6 | + let $compile, $scope, element, angular, app; |
| 7 | + |
| 8 | + beforeEach(async () => { |
| 9 | + app = document.getElementById("app"); |
| 10 | + dealoc(app); |
| 11 | + |
| 12 | + angular = new Angular(); |
| 13 | + angular.module("myModule", ["ng"]); |
| 14 | + |
| 15 | + angular.bootstrap(app, ["myModule"]).invoke((_$compile_, _$rootScope_) => { |
| 16 | + $compile = _$compile_; |
| 17 | + $scope = _$rootScope_; |
| 18 | + }); |
| 19 | + |
| 20 | + await wait(); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(() => { |
| 24 | + dealoc(app); |
| 25 | + }); |
| 26 | + |
| 27 | + it("registers and unregisters an event listener", async () => { |
| 28 | + spyOn(angular, "addEventListener").and.callThrough(); |
| 29 | + spyOn(angular, "removeEventListener").and.callThrough(); |
| 30 | + |
| 31 | + element = $compile(`<div ng-listener="test:event"></div>`)($scope); |
| 32 | + await wait(); |
| 33 | + |
| 34 | + expect(angular.addEventListener).toHaveBeenCalledWith( |
| 35 | + "test:event", |
| 36 | + jasmine.any(Function), |
| 37 | + ); |
| 38 | + |
| 39 | + $scope.$destroy(); |
| 40 | + await wait(); |
| 41 | + |
| 42 | + expect(angular.removeEventListener).toHaveBeenCalledWith( |
| 43 | + "test:event", |
| 44 | + jasmine.any(Function), |
| 45 | + ); |
| 46 | + }); |
| 47 | + |
| 48 | + it("merges object detail into scope when template content exists", async () => { |
| 49 | + $scope.foo = "initial"; |
| 50 | + |
| 51 | + element = $compile(` |
| 52 | + <div ng-listener="update"> |
| 53 | + <span>{{ foo }}</span> |
| 54 | + </div> |
| 55 | + `)($scope); |
| 56 | + |
| 57 | + await wait(); |
| 58 | + |
| 59 | + angular.dispatchEvent( |
| 60 | + new CustomEvent("update", { |
| 61 | + detail: { foo: "updated", bar: 123 }, |
| 62 | + }), |
| 63 | + ); |
| 64 | + |
| 65 | + await wait(); |
| 66 | + |
| 67 | + expect($scope.foo).toBe("updated"); |
| 68 | + expect($scope.bar).toBe(123); |
| 69 | + }); |
| 70 | + |
| 71 | + it("updates innerHTML when detail is a string and no template content exists", async () => { |
| 72 | + element = $compile(`<div ng-listener="html"></div>`)($scope); |
| 73 | + await wait(); |
| 74 | + |
| 75 | + angular.dispatchEvent( |
| 76 | + new CustomEvent("html", { |
| 77 | + detail: "<span>Injected</span>", |
| 78 | + }), |
| 79 | + ); |
| 80 | + |
| 81 | + await wait(); |
| 82 | + |
| 83 | + expect(element.innerHTML).toBe("<span>Injected</span>"); |
| 84 | + }); |
| 85 | + |
| 86 | + it("ignores non-object detail when template content exists", async () => { |
| 87 | + $scope.foo = "unchanged"; |
| 88 | + |
| 89 | + element = $compile(` |
| 90 | + <div ng-listener="noop"> |
| 91 | + <span>{{ foo }}</span> |
| 92 | + </div> |
| 93 | + `)($scope); |
| 94 | + |
| 95 | + await wait(); |
| 96 | + |
| 97 | + angular.dispatchEvent( |
| 98 | + new CustomEvent("noop", { |
| 99 | + detail: "should not apply", |
| 100 | + }), |
| 101 | + ); |
| 102 | + |
| 103 | + await wait(); |
| 104 | + |
| 105 | + expect($scope.foo).toBe("unchanged"); |
| 106 | + }); |
| 107 | +}); |
0 commit comments