|
1 | | -import { Evented, EventAny, utils } from '@'; |
| 1 | +import { AbstractEvent, EventAny, Evented, utils } from '@'; |
2 | 2 |
|
3 | | -describe('Evented', () => { |
| 3 | +describe('index', () => { |
| 4 | + test('abstract event subclasses', () => { |
| 5 | + class Event0 extends AbstractEvent {} |
| 6 | + class Event1<T> extends AbstractEvent<T> {} |
| 7 | + class Event2 extends AbstractEvent<string> {} |
| 8 | + class Event3 extends AbstractEvent<object> {} |
| 9 | + const e0 = new Event0(); |
| 10 | + expect(e0.detail).toBeNull(); |
| 11 | + const e1 = new Event1(); |
| 12 | + expect(e1.detail).toBeNull(); |
| 13 | + const e1_ = new Event1({ detail: 'string' }); |
| 14 | + expect(e1_.detail).toBe('string'); |
| 15 | + // This is not caught by the type system |
| 16 | + const e2 = new Event2(); |
| 17 | + expect(e2.detail).toBeNull(); |
| 18 | + const e2_ = new Event2({ detail: 'string' }); |
| 19 | + expect(e2_.detail).toBe('string'); |
| 20 | + const e11 = e1.clone(); |
| 21 | + expect(e11.type).toBe(e1.type); |
| 22 | + expect(e11.detail).toBe(e1.detail); |
| 23 | + const e22 = e2.clone(); |
| 24 | + expect(e22.type).toBe(e2.type); |
| 25 | + expect(e22.detail).toBe(e2.detail); |
| 26 | + const e3 = new Event3({ detail: { a: 1 } }); |
| 27 | + expect(e3.detail).toBe(e3.clone().detail); |
| 28 | + }); |
| 29 | + test('abstract event subclasses with different constructors', () => { |
| 30 | + class EventCustom1 extends AbstractEvent<string> { |
| 31 | + constructor(options: CustomEventInit<string>) { |
| 32 | + super(EventCustom1.name, options); |
| 33 | + } |
| 34 | + } |
| 35 | + class EventCustom2 extends AbstractEvent<string> { |
| 36 | + constructor(options: CustomEventInit<string>) { |
| 37 | + super(EventCustom2.name, options, arguments); |
| 38 | + } |
| 39 | + } |
| 40 | + const e1 = new EventCustom1({ detail: 'string' }); |
| 41 | + expect(() => e1.clone()).toThrow(TypeError); |
| 42 | + const e2 = new EventCustom2({ detail: 'string' }); |
| 43 | + expect(() => e2.clone()).not.toThrowError(); |
| 44 | + }); |
4 | 45 | test('can access event target via symbol', () => { |
5 | 46 | interface X extends Evented {} |
6 | 47 | @Evented() |
|
0 commit comments