-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathreact-container.component.spec.ts
More file actions
100 lines (84 loc) · 3.02 KB
/
react-container.component.spec.ts
File metadata and controls
100 lines (84 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { ComponentFixture, TestBed } from "@angular/core/testing";
import { By } from "@angular/platform-browser";
import { Theme, ThemeDefinition, ThemeService } from "app/services";
import * as React from "react";
import { BehaviorSubject } from "rxjs";
import { waitFor } from "@testing-library/react";
import { ReactContainerComponent } from "./react-container.component";
interface SimpleMessageProps {
message?: string
}
const SimpleMessage: React.FC<SimpleMessageProps> = props => {
return React.createElement("span", null, `${props.message}`);
};
const testTheme = new Theme("testTheme", {
"chart-colors": [
"#003f5c",
"#aa3939",
"#4caf50",
"#ffa600",
],
} as Partial<ThemeDefinition> as any);
describe("ReactContainerComponent", () => {
let fixture: ComponentFixture<ReactContainerComponent<unknown>>;
let testComponent: ReactContainerComponent<unknown>;
const themeSubject: BehaviorSubject<Theme | undefined> = new BehaviorSubject(undefined);
beforeEach(() => {
const themeServiceSpy = {
currentTheme: themeSubject,
};
TestBed.configureTestingModule({
imports: [],
declarations: [ReactContainerComponent],
providers: [{ provide: ThemeService, useValue: themeServiceSpy },],
});
fixture = TestBed.createComponent(ReactContainerComponent);
testComponent = fixture.componentInstance;
});
afterEach(() => {
themeSubject.next(undefined);
testComponent.ngOnDestroy();
fixture.destroy();
});
function getRootEl() {
const el = fixture.debugElement.query(By.css(".react-root")).nativeElement;
if (!el) {
throw new Error("Root element not found");
}
return el;
}
function getChildText() {
const rootEl = getRootEl();
if (rootEl.children.length === 0) {
throw new Error("No children found");
}
return rootEl.children[0].textContent;
}
it("can render a simple react component", async () => {
themeSubject.next(testTheme);
testComponent.component = SimpleMessage;
testComponent.props = {
message: "Hello world!"
};
fixture.detectChanges();
await fixture.whenStable();
const rootEl = await waitFor(getRootEl);
expect(rootEl.tagName).toEqual("DIV");
const childText = await waitFor(getChildText);
expect(childText).toEqual("Hello world!");
});
it("waits for theme to load before rendering", async () => {
testComponent.component = SimpleMessage;
testComponent.props = {
message: "Hello world!!"
};
fixture.detectChanges();
await fixture.whenStable();
expect(() => getRootEl()).toThrowError();
themeSubject.next(testTheme);
fixture.detectChanges();
await fixture.whenStable();
const childText = await waitFor(getChildText);
expect(childText).toEqual("Hello world!!");
});
});