Skip to content

Commit d903734

Browse files
committed
unit tests working
1 parent 7639792 commit d903734

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

src/cdk-experimental/testing/testbed.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,25 @@ export class TestbedHarnessEnvironment extends AbstractHarnessEnvironment<Elemen
3333
super(rawRootElement);
3434
}
3535

36-
static create(fixture: ComponentFixture<unknown>): TestbedHarnessEnvironment {
36+
static create(fixture: ComponentFixture<unknown>): HarnessEnvironment {
3737
const stabilize = async () => {
3838
fixture.detectChanges();
3939
await fixture.whenStable();
4040
};
4141
return new TestbedHarnessEnvironment(fixture.nativeElement, stabilize);
4242
}
4343

44+
static async harnessForFixtureRoot<T extends ComponentHarness>(
45+
fixture: ComponentFixture<unknown>, harnessType: ComponentHarnessConstructor<T>): Promise<T> {
46+
const stabilize = async () => {
47+
fixture.detectChanges();
48+
await fixture.whenStable();
49+
};
50+
const environment = new TestbedHarnessEnvironment(fixture.nativeElement, stabilize);
51+
await environment._stabilize();
52+
return environment.createHarness(harnessType, fixture.nativeElement);
53+
}
54+
4455
documentRootLocatorFactory(): LocatorFactory {
4556
let element = this.rawRootElement;
4657
while (element.parentElement) {

src/cdk-experimental/testing/tests/testbed.spec.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
2-
import {getNativeElement, load} from '../testbed';
1+
import {ComponentFixture, TestBed} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '../testbed';
33
import {MainComponentHarness} from './harnesses/main-component-harness';
44

55
import {TestComponentsModule} from './test-components-module';
@@ -8,17 +8,11 @@ import {TestMainComponent} from './test-main-component';
88
describe('Testbed Helper Test', () => {
99
let harness: MainComponentHarness;
1010
let fixture: ComponentFixture<{}>;
11-
beforeEach(async(() => {
12-
TestBed
13-
.configureTestingModule({
14-
imports: [TestComponentsModule],
15-
})
16-
.compileComponents()
17-
.then(() => {
18-
fixture = TestBed.createComponent(TestMainComponent);
19-
harness = load(MainComponentHarness, fixture);
20-
});
21-
}));
11+
beforeEach(async () => {
12+
await TestBed.configureTestingModule({imports: [TestComponentsModule]}).compileComponents();
13+
fixture = TestBed.createComponent(TestMainComponent);
14+
harness = await TestbedHarnessEnvironment.harnessForFixtureRoot(fixture, MainComponentHarness);
15+
});
2216

2317
describe('Locator', () => {
2418
it('should be able to locate a element based on CSS selector', async () => {
@@ -143,14 +137,8 @@ describe('Testbed Helper Test', () => {
143137
} catch (err) {
144138
expect(err.message)
145139
.toBe(
146-
'Cannot find element based on the CSS selector: wrong locator');
140+
'Expected to find element matching selector: "wrong locator", but none was found');
147141
}
148142
});
149143
});
150-
151-
describe('getNativeElement', () => {
152-
it('should return the native element', async () => {
153-
expect(getNativeElement(harness.host())).toBe(fixture.nativeElement);
154-
});
155-
});
156144
});

0 commit comments

Comments
 (0)