Skip to content

Commit 2c70f9d

Browse files
committed
refactor: constants
1 parent e0d6c21 commit 2c70f9d

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-6
lines changed

src/__tests__/render.test.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* eslint-disable no-console */
22
import * as React from 'react';
33
import { Pressable, Text, TextInput, View } from 'react-native';
4-
import { getConfig, resetToDefaults } from '../config';
5-
import { fireEvent, render, RenderAPI, screen } from '..';
4+
import { getConfig } from '../config';
5+
import { CONTAINER_TYPE } from '../renderer/contants';
6+
import { fireEvent, render, type RenderAPI, screen, resetToDefaults } from '..';
67

78
const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
89
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
@@ -172,7 +173,7 @@ test('returns container', () => {
172173
render(<View testID="inner" />);
173174

174175
expect(screen.container).toBeDefined();
175-
expect(screen.container.type).toBe('CONTAINER');
176+
expect(screen.container.type).toBe(CONTAINER_TYPE);
176177
expect(screen.container.props).toEqual({});
177178
});
178179

src/helpers/component-tree.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { CONTAINER_TYPE } from '../renderer/contants';
12
import { HostElement } from '../renderer/host-element';
23

34
/**
45
* Checks if the given element is a host element.
56
* @param element The element to check.
67
*/
78
export function isValidElement(element?: HostElement | null): element is HostElement {
8-
return typeof element?.type === 'string' && element.type !== 'CONTAINER';
9+
return typeof element?.type === 'string' && element.type !== CONTAINER_TYPE;
910
}
1011

1112
/**

src/renderer/contants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const CONTAINER_TYPE = 'CONTAINER';

src/renderer/host-element.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { CONTAINER_TYPE } from './contants';
12
import { Container, Instance, TextInstance } from './reconciler';
3+
import { JsonNode, renderToJson } from './render-to-json';
24

35
export type HostNode = HostElement | string;
46
export type HostElementProps = Record<string, any>;
@@ -13,7 +15,7 @@ export class HostElement {
1315
}
1416

1517
get type(): string {
16-
return this.instance.tag === 'INSTANCE' ? this.instance.type : 'CONTAINER';
18+
return this.instance.tag === 'INSTANCE' ? this.instance.type : CONTAINER_TYPE;
1719
}
1820

1921
get props(): HostElementProps {
@@ -53,6 +55,10 @@ export class HostElement {
5355
return Symbol.for('react.test.json');
5456
}
5557

58+
toJSON(): JsonNode | null {
59+
return renderToJson(this.instance);
60+
}
61+
5662
static fromContainer(container: Container): HostElement {
5763
const hostElement = instanceToHostElementMap.get(container);
5864
if (hostElement) {

src/renderer/render-to-json.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CONTAINER_TYPE } from './contants';
12
import { Container, Instance, TextInstance } from './reconciler';
23

34
export type JsonNode = JsonInstance | string;
@@ -70,7 +71,7 @@ export function renderToJson(instance: Container | Instance | TextInstance): Jso
7071
}
7172

7273
const result = {
73-
type: 'CONTAINER',
74+
type: CONTAINER_TYPE,
7475
props: {},
7576
children: renderedChildren,
7677
$$typeof: Symbol.for('react.test.json'),

src/renderer/renderer.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ function Passthrough({ children }: { children: React.ReactNode }) {
66
return children;
77
}
88

9+
function RendersNull() {
10+
return null;
11+
}
12+
913
test('renders View', () => {
1014
const renderer = createRenderer();
1115
renderer.render(<View />);
@@ -82,6 +86,15 @@ test('returns container view', () => {
8286
`);
8387
});
8488

89+
test('returns null when rendering indirectly null', () => {
90+
const renderer = createRenderer();
91+
renderer.render(<RendersNull />);
92+
93+
expect(renderer.container).toMatchInlineSnapshot(`<CONTAINER />`);
94+
expect(renderer.root).toBeNull();
95+
expect(renderer.toJSON()).toBeNull();
96+
});
97+
8598
test('throws when rendering string outside of Text', () => {
8699
jest.spyOn(console, 'error').mockImplementation(() => {});
87100

0 commit comments

Comments
 (0)