Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(testing): avatar harness test #3019

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 50 additions & 42 deletions projects/kit/components/avatar/tests/avatar.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import {Component, ViewChild} from '@angular/core';
import {HarnessLoader} from '@angular/cdk/testing';
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {TuiSizeXL, TuiSizeXS} from '@taiga-ui/core';
import {TuiAvatarComponent, TuiAvatarModule} from '@taiga-ui/kit';
import {configureTestSuite, TuiPageObject} from '@taiga-ui/testing';
import {TuiAvatarModule} from '@taiga-ui/kit';
import {configureTestSuite, TuiAvatarHarness, TuiPageObject} from '@taiga-ui/testing';

describe(`Avatar`, () => {
@Component({
template: `
<tui-avatar
automation-id="tui-avatar__component"
[avatarUrl]="avatarUrl"
[text]="text"
[size]="size"
[autoColor]="autoColor"
id="default"
[avatarUrl]="'someURL'"
[text]="'James Cameron'"
></tui-avatar>
<tui-avatar
id="null-url"
[avatarUrl]="null"
[text]="'James Cameron'"
></tui-avatar>
<tui-avatar
id="null-url-with-text"
[avatarUrl]="null"
[text]="'James'"
></tui-avatar>
<tui-avatar
id="null-url-xs"
size="xs"
[avatarUrl]="null"
[text]="'James'"
></tui-avatar>
<tui-avatar
id="auto-color"
[autoColor]="true"
></tui-avatar>
`,
})
class TestComponent {
@ViewChild(TuiAvatarComponent, {static: true})
component!: TuiAvatarComponent;

avatarUrl: string | null = `someUrl`;
text: string | null = `James Cameron`;
autoColor = false;
size: TuiSizeXS | TuiSizeXL = `m`;
}
class TestComponent {}

let fixture: ComponentFixture<TestComponent>;
let testComponent: TestComponent;
let component: TuiAvatarComponent;
let loader: HarnessLoader;
let pageObject: TuiPageObject<TestComponent>;
const testContext = {
get prefix() {
Expand All @@ -50,38 +60,38 @@ describe(`Avatar`, () => {

beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
pageObject = new TuiPageObject(fixture);
testComponent = fixture.componentInstance;
fixture.detectChanges();
component = testComponent.component;
loader = TestbedHarnessEnvironment.loader(fixture);
});

describe(`computedText`, () => {
it(`if there is an avatar, the text value is empty`, () => {
expect(component.computedText).toBe(``);
it(`if there is an avatar, the text value is empty`, async () => {
const avatar = await loader.getHarness(TuiAvatarHarness);

expect(await avatar.text()).toBe(``);
});

it(`if there is no avatar, the text value is taken from the first letters of the words in text`, () => {
testComponent.avatarUrl = null;
fixture.detectChanges();
it(`if there is no avatar, the text value is taken from the first letters of the words in text`, async () => {
const avatar = await loader.getHarness(
TuiAvatarHarness.with({selector: `#null-url`}),
);

expect(component.computedText).toBe(`JC`);
expect(await avatar.text()).toBe(`JC`);
});

it(`if the avatar is absent, and there is one word in text, its first letter is taken`, () => {
testComponent.avatarUrl = null;
testComponent.text = `James`;
fixture.detectChanges();
it(`if the avatar is absent, and there is one word in text, its first letter is taken`, async () => {
const avatar = await loader.getHarness(
TuiAvatarHarness.with({selector: `#null-url-with-text`}),
);

expect(component.computedText).toBe(`J`);
expect(await avatar.text()).toBe(`J`);
});

it(`for xs sizes only one letter is taken`, () => {
testComponent.avatarUrl = null;
testComponent.size = `xs`;
fixture.detectChanges();
it(`for xs sizes only one letter is taken`, async () => {
const avatar = await loader.getHarness(
TuiAvatarHarness.with({selector: `#null-url-xs`}),
);

expect(component.computedText).toBe(`J`);
expect(await avatar.text()).toBe(`J`);
});
});

Expand All @@ -94,8 +104,6 @@ describe(`Avatar`, () => {
});

it(`when autoColor is on, the color will be - rgb(160, 170, 228)`, () => {
splincode marked this conversation as resolved.
Show resolved Hide resolved
testComponent.autoColor = true;
fixture.detectChanges();
expect(getComputedStyle(getAvatar()).backgroundColor).toBe(
`rgb(160, 170, 228)`,
);
Expand Down
9 changes: 9 additions & 0 deletions projects/testing/core/avatar.harness.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {TuiComponentHarness} from '@taiga-ui/testing/utils';

export class TuiAvatarHarness extends TuiComponentHarness {
static hostSelector = `tui-avatar`;

async text(): Promise<string> {
return (await this.host()).text();
}
}
1 change: 1 addition & 0 deletions projects/testing/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './action.harness';
export * from './avatar.harness';
export * from './badge.harness';
export * from './button.harness';
export * from './card.harness';
Expand Down