|
| 1 | +import * as assert from 'assert' |
| 2 | +import { Observable, of, throwError } from 'rxjs' |
| 3 | +import { TestScheduler } from 'rxjs/testing' |
| 4 | +import { ContributableViewContainer } from '../../protocol' |
| 5 | +import * as plain from '../../protocol/plainTypes' |
| 6 | +import { Entry } from './registry' |
| 7 | +import { getView, getViews, ViewProviderRegistrationOptions } from './view' |
| 8 | + |
| 9 | +const FIXTURE_CONTAINER = ContributableViewContainer.Panel |
| 10 | + |
| 11 | +const FIXTURE_ENTRY_1: Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>> = { |
| 12 | + registrationOptions: { container: FIXTURE_CONTAINER, id: '1' }, |
| 13 | + provider: of<plain.PanelView>({ title: 't1', content: 'c1' }), |
| 14 | +} |
| 15 | +const FIXTURE_RESULT_1 = { container: FIXTURE_CONTAINER, id: '1', title: 't1', content: 'c1' } |
| 16 | + |
| 17 | +const FIXTURE_ENTRY_2: Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>> = { |
| 18 | + registrationOptions: { container: FIXTURE_CONTAINER, id: '2' }, |
| 19 | + provider: of<plain.PanelView>({ title: 't2', content: 'c2' }), |
| 20 | +} |
| 21 | +const FIXTURE_RESULT_2 = { container: FIXTURE_CONTAINER, id: '2', title: 't2', content: 'c2' } |
| 22 | + |
| 23 | +const scheduler = () => new TestScheduler((a, b) => assert.deepStrictEqual(a, b)) |
| 24 | + |
| 25 | +describe('getView', () => { |
| 26 | + describe('0 providers', () => { |
| 27 | + it('returns null', () => |
| 28 | + scheduler().run(({ cold, expectObservable }) => |
| 29 | + expectObservable( |
| 30 | + getView( |
| 31 | + cold<Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>>[]>('-a-|', { a: [] }), |
| 32 | + '1' |
| 33 | + ) |
| 34 | + ).toBe('-a-|', { |
| 35 | + a: null, |
| 36 | + }) |
| 37 | + )) |
| 38 | + }) |
| 39 | + |
| 40 | + it('returns result from provider', () => |
| 41 | + scheduler().run(({ cold, expectObservable }) => |
| 42 | + expectObservable( |
| 43 | + getView( |
| 44 | + cold<Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>>[]>('-a-|', { |
| 45 | + a: [FIXTURE_ENTRY_1], |
| 46 | + }), |
| 47 | + '1' |
| 48 | + ) |
| 49 | + ).toBe('-a-|', { |
| 50 | + a: FIXTURE_RESULT_1, |
| 51 | + }) |
| 52 | + )) |
| 53 | + |
| 54 | + describe('multiple emissions', () => { |
| 55 | + it('returns stream of results', () => |
| 56 | + scheduler().run(({ cold, expectObservable }) => |
| 57 | + expectObservable( |
| 58 | + getView( |
| 59 | + cold<Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>>[]>('-a-b-|', { |
| 60 | + a: [FIXTURE_ENTRY_1], |
| 61 | + b: [FIXTURE_ENTRY_1, FIXTURE_ENTRY_2], |
| 62 | + }), |
| 63 | + '2' |
| 64 | + ) |
| 65 | + ).toBe('-a-b-|', { |
| 66 | + a: null, |
| 67 | + b: FIXTURE_RESULT_2, |
| 68 | + }) |
| 69 | + )) |
| 70 | + }) |
| 71 | +}) |
| 72 | + |
| 73 | +describe('getViews', () => { |
| 74 | + describe('0 providers', () => { |
| 75 | + it('returns null', () => |
| 76 | + scheduler().run(({ cold, expectObservable }) => |
| 77 | + expectObservable( |
| 78 | + getViews( |
| 79 | + cold<Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>>[]>('-a-|', { a: [] }), |
| 80 | + FIXTURE_CONTAINER |
| 81 | + ) |
| 82 | + ).toBe('-a-|', { |
| 83 | + a: null, |
| 84 | + }) |
| 85 | + )) |
| 86 | + }) |
| 87 | + |
| 88 | + it('returns result from provider', () => |
| 89 | + scheduler().run(({ cold, expectObservable }) => |
| 90 | + expectObservable( |
| 91 | + getViews( |
| 92 | + cold<Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>>[]>('-a-|', { |
| 93 | + a: [FIXTURE_ENTRY_1], |
| 94 | + }), |
| 95 | + FIXTURE_CONTAINER |
| 96 | + ) |
| 97 | + ).toBe('-a-|', { |
| 98 | + a: [FIXTURE_RESULT_1], |
| 99 | + }) |
| 100 | + )) |
| 101 | + |
| 102 | + it('continues if provider has error', () => |
| 103 | + scheduler().run(({ cold, expectObservable }) => |
| 104 | + expectObservable( |
| 105 | + getViews( |
| 106 | + cold<Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>>[]>('-a-|', { |
| 107 | + a: [ |
| 108 | + { |
| 109 | + registrationOptions: { container: FIXTURE_CONTAINER, id: 'err' }, |
| 110 | + provider: throwError('err'), |
| 111 | + }, |
| 112 | + FIXTURE_ENTRY_1, |
| 113 | + ], |
| 114 | + }), |
| 115 | + FIXTURE_CONTAINER |
| 116 | + ) |
| 117 | + ).toBe('-a-|', { |
| 118 | + a: [FIXTURE_RESULT_1], |
| 119 | + }) |
| 120 | + )) |
| 121 | + |
| 122 | + describe('multiple emissions', () => { |
| 123 | + it('returns stream of results', () => |
| 124 | + scheduler().run(({ cold, expectObservable }) => |
| 125 | + expectObservable( |
| 126 | + getViews( |
| 127 | + cold<Entry<ViewProviderRegistrationOptions, Observable<plain.PanelView>>[]>('-a-b-|', { |
| 128 | + a: [FIXTURE_ENTRY_1], |
| 129 | + b: [FIXTURE_ENTRY_1, FIXTURE_ENTRY_2], |
| 130 | + }), |
| 131 | + FIXTURE_CONTAINER |
| 132 | + ) |
| 133 | + ).toBe('-a-b-|', { |
| 134 | + a: [FIXTURE_RESULT_1], |
| 135 | + b: [FIXTURE_RESULT_1, FIXTURE_RESULT_2], |
| 136 | + }) |
| 137 | + )) |
| 138 | + }) |
| 139 | +}) |
0 commit comments