|
| 1 | +/* |
| 2 | + * This file is part of ng2-json-editor. |
| 3 | + * Copyright (C) 2016 CERN. |
| 4 | + * |
| 5 | + * ng2-json-editor is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU General Public License as |
| 7 | + * published by the Free Software Foundation; either version 2 of the |
| 8 | + * License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * ng2-json-editor is distributed in the hope that it will be useful, but |
| 11 | + * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with ng2-json-editor; if not, write to the Free Software Foundation, Inc., |
| 17 | + * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 18 | + * In applying this license, CERN does not |
| 19 | + * waive the privileges and immunities granted to it by virtue of its status |
| 20 | + * as an Intergovernmental Organization or submit itself to any jurisdiction. |
| 21 | +*/ |
| 22 | + |
| 23 | +import { |
| 24 | + async, |
| 25 | + ComponentFixture, |
| 26 | + TestBed |
| 27 | +} from '@angular/core/testing'; |
| 28 | + |
| 29 | +import { Component } from '@angular/core'; |
| 30 | + |
| 31 | +import { fromJS, Map } from 'immutable'; |
| 32 | + |
| 33 | +import { TreeMenuItemComponent } from './tree-menu-item.component'; |
| 34 | +import { TreeMenuComponent } from './tree-menu.component'; |
| 35 | + |
| 36 | +import { |
| 37 | + DomUtilService, |
| 38 | + PathUtilService, |
| 39 | + WindowHrefService, |
| 40 | + AppGlobalsService, |
| 41 | + TabsUtilService |
| 42 | +} from '../shared/services'; |
| 43 | + |
| 44 | +import { |
| 45 | + AddAlwaysShowFieldsPipe, |
| 46 | + FilterAndSortBySchemaPipe |
| 47 | +} from '../shared/pipes'; |
| 48 | + |
| 49 | + |
| 50 | +/** |
| 51 | + * Wrap tree-menu in a host component to be able trigger `ngOnChanges` |
| 52 | + */ |
| 53 | +@Component({ |
| 54 | + template: '<tree-menu [record]="record" [schema]="schema"></tree-menu>' |
| 55 | +}) |
| 56 | +class TestHostComponent { |
| 57 | + schema = { |
| 58 | + type: 'object', |
| 59 | + properties: { |
| 60 | + anArray: { |
| 61 | + type: 'array', |
| 62 | + items: { |
| 63 | + type: 'object', |
| 64 | + properties: { |
| 65 | + aString: { |
| 66 | + type: 'string' |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + record: Map<string, any> = fromJS({ |
| 75 | + anArray: [ |
| 76 | + { |
| 77 | + aString: 'foo' |
| 78 | + }, |
| 79 | + { |
| 80 | + aString: 'bar' |
| 81 | + } |
| 82 | + ] |
| 83 | + }); |
| 84 | + firstLevelKeysCount = 1; |
| 85 | + allKeysCount = 5; |
| 86 | +} |
| 87 | + |
| 88 | +describe('TreeMenuComponent', () => { |
| 89 | + |
| 90 | + let fixture: ComponentFixture<TestHostComponent>; |
| 91 | + let component: TestHostComponent; |
| 92 | + let nativeEl: HTMLElement; |
| 93 | + let appGlobalsService = new AppGlobalsService(); |
| 94 | + |
| 95 | + beforeEach(async(() => { |
| 96 | + TestBed.configureTestingModule({ |
| 97 | + declarations: [ |
| 98 | + TestHostComponent, |
| 99 | + TreeMenuComponent, |
| 100 | + TreeMenuItemComponent, |
| 101 | + AddAlwaysShowFieldsPipe, |
| 102 | + FilterAndSortBySchemaPipe |
| 103 | + ], |
| 104 | + imports: [ |
| 105 | + |
| 106 | + ], |
| 107 | + providers: [ |
| 108 | + PathUtilService, |
| 109 | + WindowHrefService, |
| 110 | + DomUtilService, |
| 111 | + TabsUtilService, |
| 112 | + { provide: AppGlobalsService, useValue: appGlobalsService } |
| 113 | + ] |
| 114 | + }).compileComponents(); |
| 115 | + })); |
| 116 | + |
| 117 | + beforeEach(() => { |
| 118 | + fixture = TestBed.createComponent(TestHostComponent); |
| 119 | + component = fixture.componentInstance; |
| 120 | + appGlobalsService.config = {}; |
| 121 | + fixture.detectChanges(); |
| 122 | + nativeEl = fixture.nativeElement; |
| 123 | + }); |
| 124 | + |
| 125 | + it('should be nested if menuMaxDepth is not set', () => { |
| 126 | + let menuItemsCount = nativeEl.querySelectorAll('tree-menu-item').length; |
| 127 | + |
| 128 | + expect(menuItemsCount).toEqual(component.allKeysCount); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should not go deeper than menuMaxDepth', () => { |
| 132 | + appGlobalsService.config = { menuMaxDepth: 1 }; |
| 133 | + // Create the component again to render with new menuMaxDepth |
| 134 | + fixture = TestBed.createComponent(TestHostComponent); |
| 135 | + component = fixture.componentInstance; |
| 136 | + nativeEl = fixture.nativeElement; |
| 137 | + fixture.detectChanges(); |
| 138 | + |
| 139 | + let menuItemsCount = nativeEl.querySelectorAll('tree-menu-item').length; |
| 140 | + expect(menuItemsCount).toEqual(component.firstLevelKeysCount); |
| 141 | + }); |
| 142 | +}); |
0 commit comments