|
| 1 | +/* |
| 2 | + The MIT License |
| 3 | +
|
| 4 | + Copyright (c) 2018 EclipseSource Munich |
| 5 | + https://github.com/eclipsesource/jsonforms |
| 6 | +
|
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be included in |
| 15 | + all copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | + THE SOFTWARE. |
| 24 | +*/ |
| 25 | +import { By } from '@angular/platform-browser'; |
| 26 | +import { NgRedux } from '@angular-redux/store'; |
| 27 | +import { MockNgRedux } from '@angular-redux/store/testing'; |
| 28 | +import { IonicModule, Item, NavParams, Platform, TextInput } from 'ionic-angular'; |
| 29 | +import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing'; |
| 30 | +import { ComponentFixture, fakeAsync, TestBed, tick } from '@angular/core/testing'; |
| 31 | +import { JsonFormsOutlet } from '@jsonforms/angular'; |
| 32 | +import { |
| 33 | + StringControlRenderer, |
| 34 | + stringControlTester, |
| 35 | + VerticalLayoutRenderer, |
| 36 | + verticalLayoutTester |
| 37 | +} from '../src'; |
| 38 | +import { PlatformMock, } from '../test-config/mocks-ionic'; |
| 39 | +import { DetailPage } from '../src/other/master-detail/pages/detail/detail'; |
| 40 | + |
| 41 | +describe('Master detail', () => { |
| 42 | + |
| 43 | + let fixture: ComponentFixture<any>; |
| 44 | + let component: any; |
| 45 | + |
| 46 | + const data = { |
| 47 | + orders: [ |
| 48 | + { |
| 49 | + customer: { |
| 50 | + name: 'ACME' |
| 51 | + }, |
| 52 | + title: 'Carrots' |
| 53 | + } |
| 54 | + ] |
| 55 | + }; |
| 56 | + const schema = { |
| 57 | + definitions: { |
| 58 | + order: { |
| 59 | + type: 'object', |
| 60 | + properties: { |
| 61 | + customer: { |
| 62 | + type: 'object', |
| 63 | + properties: { |
| 64 | + name: { type: 'string' } |
| 65 | + } |
| 66 | + }, |
| 67 | + title: { |
| 68 | + type: 'string' |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + type: 'object', |
| 74 | + properties: { |
| 75 | + orders: { |
| 76 | + type: 'array', |
| 77 | + items: { |
| 78 | + $ref: '#/definitions/order' |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + const pageData = { |
| 85 | + addToNavStack: true, |
| 86 | + get() { |
| 87 | + return { |
| 88 | + label: 'ACME', |
| 89 | + data: { |
| 90 | + customer: { |
| 91 | + name: 'ACME' |
| 92 | + }, |
| 93 | + title: 'Carrots' |
| 94 | + }, |
| 95 | + path: 'orders.0', |
| 96 | + schema: schema.definitions.order, |
| 97 | + uischema: { |
| 98 | + type: 'Control', |
| 99 | + scope: '#/properties/customer/properties/name', |
| 100 | + rule: { |
| 101 | + effect: 'HIDE', |
| 102 | + condition: { |
| 103 | + scope: '#/properties/customer/properties/name', |
| 104 | + schema: { |
| 105 | + const: 'ACME' |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + }; |
| 111 | + } |
| 112 | + }; |
| 113 | + |
| 114 | + beforeEach(() => { |
| 115 | + TestBed.configureTestingModule({ |
| 116 | + declarations: [ |
| 117 | + JsonFormsOutlet, |
| 118 | + StringControlRenderer, |
| 119 | + DetailPage |
| 120 | + ], |
| 121 | + imports: [IonicModule.forRoot(DetailPage)], |
| 122 | + providers: [ |
| 123 | + {provide: Platform, useClass: PlatformMock}, |
| 124 | + {provide: NgRedux, useFactory: MockNgRedux.getInstance}, |
| 125 | + {provide: NavParams, useValue: pageData }, |
| 126 | + ], |
| 127 | + }).overrideModule(BrowserDynamicTestingModule, { |
| 128 | + set: { |
| 129 | + entryComponents: [StringControlRenderer] |
| 130 | + } |
| 131 | + }).compileComponents(); |
| 132 | + |
| 133 | + MockNgRedux.reset(); |
| 134 | + fixture = TestBed.createComponent(DetailPage); |
| 135 | + component = fixture.componentInstance; |
| 136 | + }); |
| 137 | + |
| 138 | + it('should render detail page with inactive rule', fakeAsync(() => { |
| 139 | + const mockSubStore = MockNgRedux.getSelectorStub(); |
| 140 | + |
| 141 | + mockSubStore.next({ |
| 142 | + jsonforms: { |
| 143 | + core: { |
| 144 | + data: { |
| 145 | + orders: [ |
| 146 | + { |
| 147 | + customer: { |
| 148 | + name: 'foo' |
| 149 | + }, |
| 150 | + title: 'Carrots' |
| 151 | + } |
| 152 | + ] |
| 153 | + }, |
| 154 | + schema: schema.definitions.order, |
| 155 | + }, |
| 156 | + renderers: [ |
| 157 | + { tester: stringControlTester, renderer: StringControlRenderer }, |
| 158 | + { tester: verticalLayoutTester, renderer: VerticalLayoutRenderer } |
| 159 | + ] |
| 160 | + } |
| 161 | + }); |
| 162 | + mockSubStore.complete(); |
| 163 | + fixture.detectChanges(); |
| 164 | + tick(); |
| 165 | + fixture.detectChanges(); |
| 166 | + const textInputs = fixture.debugElement.queryAll(By.directive(TextInput)); |
| 167 | + const items = fixture.debugElement.queryAll(By.directive(Item)); |
| 168 | + expect(textInputs.length).toBe(1); |
| 169 | + expect(items[0].nativeElement.hidden).toBe(false); |
| 170 | + })); |
| 171 | + |
| 172 | + it('should render detail page with active rule', fakeAsync(() => { |
| 173 | + const mockSubStore = MockNgRedux.getSelectorStub(); |
| 174 | + |
| 175 | + mockSubStore.next({ |
| 176 | + jsonforms: { |
| 177 | + core: { |
| 178 | + data, |
| 179 | + schema: schema.definitions.order, |
| 180 | + }, |
| 181 | + renderers: [ |
| 182 | + { tester: stringControlTester, renderer: StringControlRenderer }, |
| 183 | + { tester: verticalLayoutTester, renderer: VerticalLayoutRenderer } |
| 184 | + ] |
| 185 | + } |
| 186 | + }); |
| 187 | + mockSubStore.complete(); |
| 188 | + fixture.detectChanges(); |
| 189 | + tick(); |
| 190 | + fixture.detectChanges(); |
| 191 | + const textInputs = fixture.debugElement.queryAll(By.directive(TextInput)); |
| 192 | + const items = fixture.debugElement.queryAll(By.directive(Item)); |
| 193 | + expect(textInputs.length).toBe(1); |
| 194 | + expect(items[0].nativeElement.hidden).toBe(true); |
| 195 | + })); |
| 196 | +}); |
0 commit comments