-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module:avatar): add avatar component (#1028)
* wip(module:avatar): add avatar component * fix tslint & Use NzUpdateHostClassService Instead of class operation * (BREAKCHANGES:nzIcon): Use ngClass Instead of string
- Loading branch information
Showing
11 changed files
with
219 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { Component, DebugElement, ViewChild } from '@angular/core'; | ||
import { fakeAsync, tick, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
import { By } from '@angular/platform-browser'; | ||
|
||
import { NzAvatarComponent } from './nz-avatar.component'; | ||
import { NzAvatarModule } from './nz-avatar.module'; | ||
|
||
function getType(dl: DebugElement): string { | ||
const el = dl.nativeElement as HTMLElement; | ||
if (el.querySelector('img') != null) return 'image'; | ||
if (el.querySelector('.anticon') != null) return 'icon'; | ||
return el.innerText.trim().length === 0 ? '' : 'text'; | ||
} | ||
|
||
describe('avatar', () => { | ||
let fixture: ComponentFixture<TestAvatarComponent>; | ||
let context: TestAvatarComponent; | ||
let dl: DebugElement; | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ NzAvatarModule ], | ||
declarations: [ TestAvatarComponent ] | ||
}).compileComponents(); | ||
fixture = TestBed.createComponent(TestAvatarComponent); | ||
context = fixture.componentInstance; | ||
dl = fixture.debugElement; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('#nzSrc', () => { | ||
expect(context).not.toBeNull(); | ||
}); | ||
|
||
it('#nzIcon', () => { | ||
context.nzSrc = null; | ||
context.nzText = null; | ||
fixture.detectChanges(); | ||
expect(getType(dl)).toBe('icon'); | ||
}); | ||
|
||
describe('#nzText', () => { | ||
beforeEach(() => { | ||
context.nzSrc = null; | ||
context.nzIcon = null; | ||
fixture.detectChanges(); | ||
}); | ||
it('property', () => { | ||
expect(getType(dl)).toBe('text'); | ||
}); | ||
it('should be normal font-size', fakeAsync(() => { | ||
context.nzText = 'a'; | ||
fixture.detectChanges(); | ||
tick(); | ||
const scale = +dl.nativeElement.querySelector('.ant-avatar-string').style.transform.replace(/[^\.0-9]/ig, ''); | ||
expect(scale).toBe(0); | ||
})); | ||
it('should be autoset font-size', fakeAsync(() => { | ||
context.nzText = 'LongUsername'; | ||
fixture.detectChanges(); | ||
tick(); | ||
const scale = +dl.nativeElement.querySelector('.ant-avatar-string').style.transform.replace(/[^\.0-9]/ig, ''); | ||
expect(scale).toBeLessThan(1); | ||
})); | ||
}); | ||
|
||
describe('#nzShape', () => { | ||
for (const type of [ 'square', 'circle' ]) { | ||
it(type, () => { | ||
context.nzShape = type; | ||
fixture.detectChanges(); | ||
expect(dl.query(By.css(`.ant-avatar-${type}`)) !== null).toBe(true); | ||
}); | ||
} | ||
}); | ||
|
||
describe('#nzSize', () => { | ||
for (const item of [ { size: 'large', cls: 'lg'}, { size: 'small', cls: 'sm'} ]) { | ||
it(item.size, () => { | ||
context.nzSize = item.size; | ||
fixture.detectChanges(); | ||
expect(dl.query(By.css(`.ant-avatar-${item.cls}`)) !== null).toBe(true); | ||
}); | ||
} | ||
}); | ||
|
||
describe('order: image > icon > text', () => { | ||
it('image priority', () => { | ||
expect(getType(dl)).toBe('image'); | ||
}); | ||
it('should be show icon when image loaded error and icon exists', fakeAsync(() => { | ||
expect(getType(dl)).toBe('image'); | ||
context.comp.imgError(); | ||
tick(); | ||
fixture.detectChanges(); | ||
expect(getType(dl)).toBe('icon'); | ||
})); | ||
it('should be show text when image loaded error and icon not exists', fakeAsync(() => { | ||
expect(getType(dl)).toBe('image'); | ||
context.nzIcon = null; | ||
fixture.detectChanges(); | ||
context.comp.imgError(); | ||
tick(); | ||
fixture.detectChanges(); | ||
expect(getType(dl)).toBe('text'); | ||
})); | ||
it('should be show empty when image loaded error and icon & text not exists', fakeAsync(() => { | ||
expect(getType(dl)).toBe('image'); | ||
context.nzIcon = null; | ||
context.nzText = null; | ||
fixture.detectChanges(); | ||
context.comp.imgError(); | ||
tick(); | ||
fixture.detectChanges(); | ||
expect(getType(dl)).toBe(''); | ||
})); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
template: ` | ||
<nz-avatar #comp | ||
[nzShape]="nzShape" | ||
[nzSize]="nzSize" | ||
[nzIcon]="nzIcon" | ||
[nzText]="nzText" | ||
[nzSrc]="nzSrc"></nz-avatar> | ||
`, | ||
styleUrls: [ './style/index.less' ] | ||
}) | ||
class TestAvatarComponent { | ||
@ViewChild('comp') comp: NzAvatarComponent; | ||
nzShape = 'square'; | ||
nzSize = 'large'; | ||
nzIcon = 'anticon anticon-user'; | ||
nzText = 'A'; | ||
nzSrc = `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+P+/HgAFhAJ/wlseKgAAAABJRU5ErkJggg==`; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './public-api'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './nz-avatar.component'; | ||
export * from './nz-avatar.module'; |
Oops, something went wrong.