Skip to content

Commit e0d6c21

Browse files
committed
refactor: fix test
1 parent db18123 commit e0d6c21

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

src/__tests__/render.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,8 @@ test('returns host root', () => {
164164
render(<View testID="inner" />);
165165

166166
expect(screen.root).toBeDefined();
167-
expect(screen.root.type).toBe('View');
168-
expect(screen.root.props.testID).toBe('inner');
167+
expect(screen.root?.type).toBe('View');
168+
expect(screen.root?.props.testID).toBe('inner');
169169
});
170170

171171
test('returns container', () => {

src/helpers/host-component-names.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ function detectHostComponentNames(): HostComponentNames {
4545
);
4646
});
4747
return {
48-
text: getByTestId(renderer.root, 'text').type as string,
49-
textInput: getByTestId(renderer.root, 'textInput').type as string,
50-
image: getByTestId(renderer.root, 'image').type as string,
51-
switch: getByTestId(renderer.root, 'switch').type as string,
52-
scrollView: getByTestId(renderer.root, 'scrollView').type as string,
53-
modal: getByTestId(renderer.root, 'modal').type as string,
48+
text: getByTestId(renderer.container, 'text').type as string,
49+
textInput: getByTestId(renderer.container, 'textInput').type as string,
50+
image: getByTestId(renderer.container, 'image').type as string,
51+
switch: getByTestId(renderer.container, 'switch').type as string,
52+
scrollView: getByTestId(renderer.container, 'scrollView').type as string,
53+
modal: getByTestId(renderer.container, 'modal').type as string,
5454
};
5555
} catch (error) {
5656
const errorMessage =

src/render.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function buildRenderResult(renderer: Renderer, wrap: (element: React.ReactElemen
6969
toJSON: renderer.toJSON,
7070
debug: debug(renderer),
7171
container: renderer.container,
72-
get root(): HostElement {
72+
get root(): HostElement | null {
7373
return renderer.root;
7474
},
7575
};

src/renderer/renderer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ export type RendererOptions = {
1111
export type Renderer = {
1212
render: (element: ReactElement) => void;
1313
unmount: () => void;
14-
root: HostElement;
1514
container: HostElement;
15+
root: HostElement | null;
1616
toJSON: () => JsonNode | JsonNode[] | null;
1717
};
1818

@@ -79,11 +79,15 @@ export function createRenderer(options?: RendererOptions): Renderer {
7979
render,
8080
unmount,
8181
toJSON,
82-
get root(): HostElement {
83-
if (containerFiber == null || container == null || container.children.length === 0) {
82+
get root(): HostElement | null {
83+
if (containerFiber == null || container == null) {
8484
throw new Error("Can't access .root on unmounted test renderer");
8585
}
8686

87+
if (container.children.length === 0) {
88+
return null;
89+
}
90+
8791
const root = HostElement.fromInstance(container.children[0]);
8892
if (typeof root === 'string') {
8993
throw new Error('Cannot render string as root element');

src/screen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ interface Screen extends RenderResult {
1818

1919
const defaultScreen: Screen = {
2020
isDetached: true,
21-
get root(): HostElement {
21+
get root(): HostElement | null {
2222
throw new Error(SCREEN_ERROR);
2323
},
2424
get container(): HostElement {

0 commit comments

Comments
 (0)