|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +import { assert, describe, expect, it } from 'vitest'; |
| 5 | +import { EventThenable } from './eventThenable.js'; |
| 6 | +import { PromiseState } from '../thenable.js'; |
| 7 | + |
| 8 | +describe('EventThenable', () => { |
| 9 | + class Event { |
| 10 | + num: number = 0; |
| 11 | + } |
| 12 | + |
| 13 | + describe('Non-Filtering Signals', () => { |
| 14 | + class Signal { |
| 15 | + public closure_: ((e: Event) => void) | undefined = undefined; |
| 16 | + |
| 17 | + public sendEvent(e: Event) { |
| 18 | + if (this.closure_ !== undefined) { |
| 19 | + this.closure_(e); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public subscribe(closure: (e: Event) => void) { |
| 24 | + this.closure_ = closure; |
| 25 | + return closure; |
| 26 | + } |
| 27 | + |
| 28 | + public unsubscribe(_: (e: Event) => void) { |
| 29 | + this.closure_ = undefined; |
| 30 | + } |
| 31 | + } |
| 32 | + const signal = new Signal(); |
| 33 | + |
| 34 | + it('successfully resolve event', () => { |
| 35 | + const e = new EventThenable(signal); |
| 36 | + assert(signal.closure_ !== undefined); |
| 37 | + signal.sendEvent({ num: 4 }); |
| 38 | + expect(e.state()).toBe(PromiseState.FULFILLED); |
| 39 | + }); |
| 40 | + |
| 41 | + it('successfully use then on an EventThenable', () => { |
| 42 | + new EventThenable(signal).then((event?: Event) => { |
| 43 | + assert(event !== undefined); |
| 44 | + expect(event.num).toBe(5); |
| 45 | + }); |
| 46 | + signal.sendEvent({ num: 5 }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('successfully cancel an EventThenable', () => { |
| 50 | + const e = new EventThenable(signal); |
| 51 | + e.then((event?: Event) => { |
| 52 | + assert(event === undefined); |
| 53 | + }); |
| 54 | + e.cancel(); |
| 55 | + expect(e.state()).toBe(PromiseState.FULFILLED); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('Filterable Signals', () => { |
| 60 | + class EventFilters { |
| 61 | + public someFilterValue: number = 0; |
| 62 | + } |
| 63 | + |
| 64 | + class Signal { |
| 65 | + public closure_: ((e: Event) => void) | undefined = undefined; |
| 66 | + public filters_: EventFilters | undefined = undefined; |
| 67 | + |
| 68 | + public sendEvent(e: Event) { |
| 69 | + if (this.closure_ !== undefined) { |
| 70 | + this.closure_(e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public subscribe(closure: (e: Event) => void, options?: EventFilters) { |
| 75 | + this.closure_ = closure; |
| 76 | + this.filters_ = options; |
| 77 | + return closure; |
| 78 | + } |
| 79 | + |
| 80 | + public unsubscribe(_: (e: Event) => void) { |
| 81 | + this.closure_ = undefined; |
| 82 | + } |
| 83 | + } |
| 84 | + const signal = new Signal(); |
| 85 | + |
| 86 | + // checking that the filters are being passed through to the signal properly |
| 87 | + it('successfully create EventThenable with filtered signal with filter', () => { |
| 88 | + const e = new EventThenable(signal, { someFilterValue: 18 }); |
| 89 | + assert(signal.filters_ !== undefined); |
| 90 | + expect(signal.filters_.someFilterValue).toBe(18); |
| 91 | + signal.sendEvent({ num: 4 }); |
| 92 | + expect(e.state()).toBe(PromiseState.FULFILLED); |
| 93 | + }); |
| 94 | + |
| 95 | + it('successfully create EventThenable with filtered signal with no filter', () => { |
| 96 | + const e = new EventThenable(signal); |
| 97 | + assert(signal.filters_ === undefined); |
| 98 | + signal.sendEvent({ num: 4 }); |
| 99 | + expect(e.state()).toBe(PromiseState.FULFILLED); |
| 100 | + }); |
| 101 | + }); |
| 102 | +}); |
0 commit comments