|
| 1 | +import { Component, ElementRef, NgZone, NO_ERRORS_SCHEMA, ViewChild } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import { EVENT_MANAGER_PLUGINS, EventManager, EventManagerPlugin } from '@angular/platform-browser'; |
| 4 | +import { NativeScriptCommonModule, NativeScriptEventManagerPlugin, NativeScriptRendererHelperService, PREVENT_CHANGE_EVENTS_DURING_CD } from '@nativescript/angular'; |
| 5 | +import { StackLayout, View } from '@nativescript/core'; |
| 6 | + |
| 7 | +describe('NativeScriptEventManagerPlugin', () => { |
| 8 | + it('supports every event name', () => { |
| 9 | + const plugin = new NativeScriptEventManagerPlugin(); |
| 10 | + expect(plugin.supports('tap')).toBe(true); |
| 11 | + expect(plugin.supports('custom.debounce.500')).toBe(true); |
| 12 | + }); |
| 13 | + |
| 14 | + it('attaches and detaches handlers through on/off', () => { |
| 15 | + const plugin = new NativeScriptEventManagerPlugin(); |
| 16 | + const view = new StackLayout(); |
| 17 | + let count = 0; |
| 18 | + const remove = plugin.addEventListener(view, 'myEvent', () => count++); |
| 19 | + view.notify({ eventName: 'myEvent', object: view }); |
| 20 | + expect(count).toBe(1); |
| 21 | + remove(); |
| 22 | + view.notify({ eventName: 'myEvent', object: view }); |
| 23 | + expect(count).toBe(1); |
| 24 | + }); |
| 25 | + |
| 26 | + it('replays the loaded event when the target is already loaded', () => { |
| 27 | + const plugin = new NativeScriptEventManagerPlugin(); |
| 28 | + const target: any = { isLoaded: true, on() {}, off() {} }; |
| 29 | + let fired = 0; |
| 30 | + plugin.addEventListener(target, View.loadedEvent, () => fired++); |
| 31 | + expect(fired).toBe(1); |
| 32 | + }); |
| 33 | + |
| 34 | + it('does not replay the loaded event when the target is not loaded', () => { |
| 35 | + const plugin = new NativeScriptEventManagerPlugin(); |
| 36 | + const target: any = { isLoaded: false, on() {}, off() {} }; |
| 37 | + let fired = 0; |
| 38 | + plugin.addEventListener(target, View.loadedEvent, () => fired++); |
| 39 | + expect(fired).toBe(0); |
| 40 | + }); |
| 41 | + |
| 42 | + it('delivers events in the zone that registered them', () => { |
| 43 | + const plugin = new NativeScriptEventManagerPlugin(); |
| 44 | + const view = new StackLayout(); |
| 45 | + let whichZone: string; |
| 46 | + Zone.root.fork({ name: 'registration-zone' }).run(() => { |
| 47 | + plugin.addEventListener(view, 'myEvent', () => (whichZone = Zone.current.name)); |
| 48 | + }); |
| 49 | + Zone.root.run(() => { |
| 50 | + view.notify({ eventName: 'myEvent', object: view }); |
| 51 | + }); |
| 52 | + expect(whichZone).toBe('registration-zone'); |
| 53 | + }); |
| 54 | +}); |
| 55 | + |
| 56 | +class TestEventPlugin extends EventManagerPlugin { |
| 57 | + calls: string[] = []; |
| 58 | + |
| 59 | + constructor() { |
| 60 | + super(null); |
| 61 | + } |
| 62 | + |
| 63 | + supports(eventName: string): boolean { |
| 64 | + return eventName.startsWith('custom.'); |
| 65 | + } |
| 66 | + |
| 67 | + addEventListener(element: any, eventName: string, handler: Function): Function { |
| 68 | + this.calls.push(eventName); |
| 69 | + const view = element as View; |
| 70 | + view.on('myCustomEvent', handler as any); |
| 71 | + return () => view.off('myCustomEvent', handler as any); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +@Component({ |
| 76 | + template: `<StackLayout #el (custom.debounce.500)="hits = hits + 1" (myPlainEvent)="plainHits = plainHits + 1"></StackLayout>`, |
| 77 | + imports: [NativeScriptCommonModule], |
| 78 | + schemas: [NO_ERRORS_SCHEMA], |
| 79 | +}) |
| 80 | +class PluginHostComponent { |
| 81 | + @ViewChild('el', { read: ElementRef, static: true }) el: ElementRef<View>; |
| 82 | + hits = 0; |
| 83 | + plainHits = 0; |
| 84 | +} |
| 85 | + |
| 86 | +describe('EVENT_MANAGER_PLUGINS integration', () => { |
| 87 | + let testPlugin: TestEventPlugin; |
| 88 | + |
| 89 | + beforeEach(() => { |
| 90 | + testPlugin = new TestEventPlugin(); |
| 91 | + return TestBed.configureTestingModule({ |
| 92 | + imports: [PluginHostComponent], |
| 93 | + providers: [{ provide: EVENT_MANAGER_PLUGINS, useValue: testPlugin, multi: true }], |
| 94 | + }).compileComponents(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('provides an EventManager bound to the app NgZone', () => { |
| 98 | + expect(TestBed.inject(EventManager).getZone()).toBe(TestBed.inject(NgZone)); |
| 99 | + }); |
| 100 | + |
| 101 | + it('registers the NativeScript plugin as the default fallback', () => { |
| 102 | + const plugins = TestBed.inject(EVENT_MANAGER_PLUGINS); |
| 103 | + expect(plugins.some((p) => p instanceof NativeScriptEventManagerPlugin)).toBe(true); |
| 104 | + }); |
| 105 | + |
| 106 | + it('routes sugared event names to the custom plugin', () => { |
| 107 | + const fixture = TestBed.createComponent(PluginHostComponent); |
| 108 | + fixture.detectChanges(); |
| 109 | + expect(testPlugin.calls).toContain('custom.debounce.500'); |
| 110 | + |
| 111 | + const view = fixture.componentInstance.el.nativeElement; |
| 112 | + view.notify({ eventName: 'myCustomEvent', object: view }); |
| 113 | + expect(fixture.componentInstance.hits).toBe(1); |
| 114 | + }); |
| 115 | + |
| 116 | + it('routes plain events through the NativeScript fallback plugin', () => { |
| 117 | + const fixture = TestBed.createComponent(PluginHostComponent); |
| 118 | + fixture.detectChanges(); |
| 119 | + expect(testPlugin.calls).not.toContain('myPlainEvent'); |
| 120 | + |
| 121 | + const view = fixture.componentInstance.el.nativeElement; |
| 122 | + view.notify({ eventName: 'myPlainEvent', object: view }); |
| 123 | + expect(fixture.componentInstance.plainHits).toBe(1); |
| 124 | + }); |
| 125 | + |
| 126 | + it('stops delivering events after the listener is removed', () => { |
| 127 | + const fixture = TestBed.createComponent(PluginHostComponent); |
| 128 | + fixture.detectChanges(); |
| 129 | + const view = fixture.componentInstance.el.nativeElement; |
| 130 | + fixture.destroy(); |
| 131 | + view.notify({ eventName: 'myCustomEvent', object: view }); |
| 132 | + view.notify({ eventName: 'myPlainEvent', object: view }); |
| 133 | + expect(fixture.componentInstance.hits).toBe(0); |
| 134 | + expect(fixture.componentInstance.plainHits).toBe(0); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +@Component({ |
| 139 | + template: `<StackLayout #el (somePropChange)="changes = changes + 1"></StackLayout>`, |
| 140 | + imports: [NativeScriptCommonModule], |
| 141 | + schemas: [NO_ERRORS_SCHEMA], |
| 142 | +}) |
| 143 | +class ChangeEventHostComponent { |
| 144 | + @ViewChild('el', { read: ElementRef, static: true }) el: ElementRef<View>; |
| 145 | + changes = 0; |
| 146 | +} |
| 147 | + |
| 148 | +describe('prevent change events during CD', () => { |
| 149 | + beforeEach(() => { |
| 150 | + return TestBed.configureTestingModule({ |
| 151 | + imports: [ChangeEventHostComponent], |
| 152 | + providers: [{ provide: PREVENT_CHANGE_EVENTS_DURING_CD, useValue: true }], |
| 153 | + }).compileComponents(); |
| 154 | + }); |
| 155 | + |
| 156 | + it('suppresses *Change events while DOM changes are executing', () => { |
| 157 | + const fixture = TestBed.createComponent(ChangeEventHostComponent); |
| 158 | + fixture.detectChanges(); |
| 159 | + const view = fixture.componentInstance.el.nativeElement; |
| 160 | + const helper = TestBed.inject(NativeScriptRendererHelperService); |
| 161 | + |
| 162 | + helper.beginDomChanges(); |
| 163 | + view.notify({ eventName: 'somePropChange', object: view }); |
| 164 | + helper.endDomChanges(); |
| 165 | + expect(fixture.componentInstance.changes).toBe(0); |
| 166 | + |
| 167 | + view.notify({ eventName: 'somePropChange', object: view }); |
| 168 | + expect(fixture.componentInstance.changes).toBe(1); |
| 169 | + }); |
| 170 | +}); |
0 commit comments