-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add collapse animation to json-formatter * use mdTooltip instead of `title` * feature(json-formatter): Improved efficiency by changing its change detection to OnPush. * bugfix(json-formatter): recreate children array so it doesnt append the new children data. * limit preview tooltip to 80 characters * update docs and README.md * json-formatter unit test scaffolding * remove lint issue * code-health(json-formatter): added initial unit tests for json-formatter
- Loading branch information
1 parent
8015a67
commit da4db7f
Showing
6 changed files
with
244 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
194 changes: 194 additions & 0 deletions
194
src/platform/core/json-formatter/json-formatter.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import { | ||
TestBed, | ||
inject, | ||
async, | ||
ComponentFixture, | ||
} from '@angular/core/testing'; | ||
import { Component } from '@angular/core'; | ||
import { CovalentJsonFormatterModule, TdJsonFormatterComponent } from './json-formatter.module'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
describe('Component: JsonFormatter', () => { | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [ | ||
TdJsonFormatterBasicTestComponent, | ||
], | ||
imports: [ | ||
CovalentJsonFormatterModule.forRoot(), | ||
], | ||
}); | ||
TestBed.compileComponents(); | ||
})); | ||
|
||
it('should render component with undefined in it', | ||
async(inject([], () => { | ||
let fixture: ComponentFixture<any> = TestBed.createComponent(TdJsonFormatterBasicTestComponent); | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.query(By.css('.undefined'))).toBeTruthy(); | ||
}); | ||
}))); | ||
|
||
it('should render component with key truncated and elipsis', | ||
async(inject([], () => { | ||
let fixture: ComponentFixture<any> = TestBed.createComponent(TdJsonFormatterBasicTestComponent); | ||
let component: TdJsonFormatterBasicTestComponent = fixture.debugElement.componentInstance; | ||
component.data = { | ||
loooooooooooooooooooooooooooooong: 'string', | ||
}; | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.queryAll(By.css('.key'))[0].nativeElement.textContent.trim()) | ||
.toBe('looooooooooooooooooooooooooooo…:'); | ||
}); | ||
}))); | ||
|
||
it('should throw error if [levelsOpen] is not an integer', | ||
async(inject([], () => { | ||
let fixture: ComponentFixture<any> = TestBed.createComponent(TdJsonFormatterBasicTestComponent); | ||
let component: TdJsonFormatterBasicTestComponent = fixture.debugElement.componentInstance; | ||
component.levelsOpen = undefined; | ||
expect(function(): void { | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
}); | ||
}).toThrowError(); | ||
}))); | ||
|
||
it('should render component with key and values depending on type', | ||
async(inject([], () => { | ||
let fixture: ComponentFixture<any> = TestBed.createComponent(TdJsonFormatterBasicTestComponent); | ||
let component: TdJsonFormatterBasicTestComponent = fixture.debugElement.componentInstance; | ||
component.data = { | ||
stringProperty: 'string', | ||
dateProperty: new Date(), | ||
numberProperty: 1, | ||
functionProperty: function(): void {/* */}, | ||
/* tslint:disable-next-line */ | ||
nullProperty: null, | ||
undefinedProperty: undefined, | ||
arrayProperty: [], | ||
}; | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.queryAll(By.css('.key'))[0].nativeElement.textContent.trim()) | ||
.toBe('stringProperty:'); | ||
expect(fixture.debugElement.query(By.css('.string'))).toBeTruthy(); | ||
expect(fixture.debugElement.queryAll(By.css('.key'))[1].nativeElement.textContent.trim()) | ||
.toBe('dateProperty:'); | ||
expect(fixture.debugElement.query(By.css('.date'))).toBeTruthy(); | ||
expect(fixture.debugElement.queryAll(By.css('.key'))[2].nativeElement.textContent.trim()) | ||
.toBe('numberProperty:'); | ||
expect(fixture.debugElement.query(By.css('.number'))).toBeTruthy(); | ||
expect(fixture.debugElement.queryAll(By.css('.key'))[3].nativeElement.textContent.trim()) | ||
.toBe('functionProperty:'); | ||
expect(fixture.debugElement.query(By.css('.function'))).toBeTruthy(); | ||
expect(fixture.debugElement.queryAll(By.css('.key'))[4].nativeElement.textContent.trim()) | ||
.toBe('nullProperty:'); | ||
expect(fixture.debugElement.query(By.css('.null'))).toBeTruthy(); | ||
expect(fixture.debugElement.queryAll(By.css('.key'))[5].nativeElement.textContent.trim()) | ||
.toBe('undefinedProperty:'); | ||
expect(fixture.debugElement.query(By.css('.undefined'))).toBeTruthy(); | ||
expect(fixture.debugElement.queryAll(By.css('.key'))[6].nativeElement.textContent.trim()) | ||
.toBe('arrayProperty:'); | ||
expect(fixture.debugElement.query(By.css('.td-empty'))).toBeTruthy(); | ||
}); | ||
}))); | ||
|
||
it('should render component with an array hidden', | ||
async(inject([], () => { | ||
let fixture: ComponentFixture<any> = TestBed.createComponent(TdJsonFormatterBasicTestComponent); | ||
let component: TdJsonFormatterBasicTestComponent = fixture.debugElement.componentInstance; | ||
component.data = [1, 2, 3, 4, 5, 6]; | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.query(By.css('.td-key-node'))).toBeTruthy(); | ||
expect(fixture.debugElement.queryAll(By.css('.td-key-leaf')).length).toBe(6); | ||
/* tslint:disable-next-line */ | ||
expect(fixture.debugElement.query(By.css('.td-object-children')).styles['display']).toBe('none'); | ||
}); | ||
}))); | ||
|
||
it('should render component with an array hidden, click on node and then display array content', | ||
async(inject([], () => { | ||
let fixture: ComponentFixture<any> = TestBed.createComponent(TdJsonFormatterBasicTestComponent); | ||
let component: TdJsonFormatterBasicTestComponent = fixture.debugElement.componentInstance; | ||
component.data = [1, 2, 3, 4, 5, 6]; | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.query(By.css('.td-key-node'))).toBeTruthy(); | ||
/* tslint:disable-next-line */ | ||
expect(fixture.debugElement.query(By.css('.td-object-children')).styles['display']).toBe('none'); | ||
fixture.debugElement.query(By.css('.td-key-node')).triggerEventHandler('click', new Event('click')); | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
/* tslint:disable-next-line */ | ||
expect(fixture.debugElement.query(By.css('.td-object-children')).styles['display']).toBeNull(); | ||
}); | ||
}); | ||
}))); | ||
|
||
it('should render component with an array display by 1 level', | ||
async(inject([], () => { | ||
let fixture: ComponentFixture<any> = TestBed.createComponent(TdJsonFormatterBasicTestComponent); | ||
let component: TdJsonFormatterBasicTestComponent = fixture.debugElement.componentInstance; | ||
component.data = [1, 2, 3, 4, 5, 6]; | ||
component.levelsOpen = 1; | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
/* tslint:disable-next-line */ | ||
expect(fixture.debugElement.query(By.css('.td-object-children')).styles['display']).toBeNull(); | ||
}); | ||
}))); | ||
|
||
it('should render component and rerender data only when refresh method explicitly called', | ||
async(inject([], () => { | ||
let fixture: ComponentFixture<any> = TestBed.createComponent(TdJsonFormatterBasicTestComponent); | ||
let component: TdJsonFormatterBasicTestComponent = fixture.debugElement.componentInstance; | ||
component.data = {property: 'test'}; | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
/* tslint:disable-next-line */ | ||
expect(fixture.debugElement.query(By.css('.string')).nativeElement.textContent.trim()).toBe('"test"'); | ||
component.data.property = 'test2'; | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.query(By.css('.string')).nativeElement.textContent.trim()).toBe('"test"'); | ||
fixture.debugElement.query(By.directive(TdJsonFormatterComponent)).componentInstance.refresh(); | ||
fixture.detectChanges(); | ||
fixture.whenStable().then(() => { | ||
fixture.detectChanges(); | ||
expect(fixture.debugElement.query(By.css('.string')).nativeElement.textContent.trim()).toBe('"test2"'); | ||
}); | ||
}); | ||
}); | ||
}))); | ||
|
||
}); | ||
|
||
@Component({ | ||
selector: 'td-json-formatter-basic-test', | ||
template: ` | ||
<td-json-formatter [data]="data" [levelsOpen]="levelsOpen"> | ||
</td-json-formatter> | ||
`, | ||
}) | ||
class TdJsonFormatterBasicTestComponent { | ||
|
||
data: any; | ||
levelsOpen: number = 0; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters