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

[TS migration] Migrate tests in react-native-onyx #543

Merged
merged 18 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions lib/DevTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,4 @@ class DevTools {
}

export default new DevTools();
export type {DevtoolsConnection};
25,051 changes: 13,007 additions & 12,044 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
},
"devDependencies": {
"@actions/core": "^1.10.1",
"@jest/globals": "^29.7.0",
"@lwc/eslint-plugin-lwc": "^1.7.2",
"@ngneat/falso": "^7.2.0",
"@react-native-community/eslint-config": "^3.2.0",
Expand Down
49 changes: 0 additions & 49 deletions tests/components/ViewWithCollections.js

This file was deleted.

36 changes: 36 additions & 0 deletions tests/components/ViewWithCollections.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, {forwardRef, useImperativeHandle} from 'react';
import {View, Text} from 'react-native';
import utils from '../../lib/utils';

type ViewWithCollectionsProps = {
collections: Record<string, {ID: number}>;
testObject: {isDefaultProp: boolean};
onRender: (props: ViewWithCollectionsProps) => void;
markReadyForHydration: () => void;
} & Record<string, unknown>;

function ViewWithCollections(
{collections = {}, testObject = {isDefaultProp: true}, onRender, markReadyForHydration, ...rest}: ViewWithCollectionsProps,
ref: React.Ref<{markReadyForHydration: () => void}>,
) {
useImperativeHandle(ref, () => ({
markReadyForHydration,
}));

onRender?.({collections, testObject, onRender, markReadyForHydration, ...rest});
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved
if (utils.isEmptyObject(collections)) {
return <Text>empty</Text>;
}

return (
<View>
{Object.values(collections).map((collection, i) => (
// eslint-disable-next-line react/no-array-index-key
<Text key={i}>{collection.ID}</Text>
))}
</View>
);
}

export default forwardRef(ViewWithCollections);
export type {ViewWithCollectionsProps};
26 changes: 0 additions & 26 deletions tests/components/ViewWithObject.js

This file was deleted.

19 changes: 19 additions & 0 deletions tests/components/ViewWithObject.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import {Text, View} from 'react-native';

type ViewWithObjectProps = {
onRender?: () => void;
} & Record<string, unknown>;

function ViewWithObject({onRender, ...rest}: ViewWithObjectProps) {
onRender?.();

return (
<View>
<Text testID="text-element">{JSON.stringify(rest)}</Text>
</View>
);
}

export default ViewWithObject;
export type {ViewWithObjectProps};
28 changes: 0 additions & 28 deletions tests/components/ViewWithText.js

This file was deleted.

22 changes: 22 additions & 0 deletions tests/components/ViewWithText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React from 'react';
import {View, Text} from 'react-native';

type ViewWithTextOnyxProps = {text: unknown};
type ViewWithTextProps = ViewWithTextOnyxProps & {
// eslint-disable-next-line react/no-unused-prop-types -- it's used in withOnyx in the tests
collectionID?: string;
onRender?: () => void;
};

function ViewWithText({onRender, text}: ViewWithTextProps) {
onRender?.();

return (
<View>
<Text testID="text-element">{(text as string) || 'null'}</Text>
</View>
);
}

export default ViewWithText;
export type {ViewWithTextProps, ViewWithTextOnyxProps};
26 changes: 14 additions & 12 deletions tests/unit/DevToolsTest.js → tests/unit/DevToolsTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable dot-notation */
/* eslint-disable @lwc/lwc/no-async-await */
/* eslint-disable no-underscore-dangle */
import Onyx from '../../lib';
import type {DevtoolsConnection} from '../../lib/DevTools';
import DevTools from '../../lib/DevTools';
import utils from '../../lib/utils';
import type GenericCollection from '../utils/GenericCollection';

const ONYX_KEYS = {
NUM_KEY: 'numKey',
Expand All @@ -21,7 +24,7 @@ const initialKeyStates = {
[ONYX_KEYS.OBJECT_KEY]: {id: 42},
};

const exampleCollection = {
const exampleCollection: GenericCollection = {
[`${ONYX_KEYS.COLLECTION.NUM_KEY}1`]: 1,
[`${ONYX_KEYS.COLLECTION.NUM_KEY}2`]: 2,
};
Expand All @@ -33,17 +36,16 @@ const mergedCollection = {...initialKeyStates, ...exampleCollection};
const mergedObject = {...initialKeyStates, [ONYX_KEYS.OBJECT_KEY]: {...exampleObject, id: 42}};

describe('DevTools', () => {
let initMock;
let sendMock;
let initMock: jest.Mock<void>;
let sendMock: jest.Mock<void>;

beforeEach(() => {
// Mock DevTools
initMock = jest.fn();
sendMock = jest.fn();
DevTools.remoteDev = {init: initMock, send: sendMock};
DevTools['remoteDev'] = {init: initMock, send: sendMock} as unknown as DevtoolsConnection;
Onyx.init({
keys: ONYX_KEYS,
registerStorageEventListener: () => {},
initialKeyStates,
});
});
Expand All @@ -56,10 +58,10 @@ describe('DevTools', () => {
expect(initMock).toHaveBeenCalledWith(initialKeyStates);
});
it('Sets the default state correctly', () => {
expect(DevTools.defaultState).toEqual(initialKeyStates);
expect(DevTools['defaultState']).toEqual(initialKeyStates);
});
it('Sets the internal state correctly', () => {
expect(DevTools.state).toEqual(initialKeyStates);
expect(DevTools['state']).toEqual(initialKeyStates);
});
});

Expand All @@ -70,7 +72,7 @@ describe('DevTools', () => {
});
it('Sets the internal state correctly', async () => {
await Onyx.set(ONYX_KEYS.SOME_KEY, 3);
expect(DevTools.state).toEqual({...initialKeyStates, [ONYX_KEYS.SOME_KEY]: 3});
expect(DevTools['state']).toEqual({...initialKeyStates, [ONYX_KEYS.SOME_KEY]: 3});
});
});

Expand All @@ -81,7 +83,7 @@ describe('DevTools', () => {
});
it('Sets the internal state correctly', async () => {
await Onyx.merge(ONYX_KEYS.OBJECT_KEY, exampleObject);
expect(DevTools.state).toEqual(mergedObject);
expect(DevTools['state']).toEqual(mergedObject);
});
});

Expand All @@ -92,7 +94,7 @@ describe('DevTools', () => {
});
it('Sets the internal state correctly', async () => {
await Onyx.mergeCollection(ONYX_KEYS.COLLECTION.NUM_KEY, exampleCollection);
expect(DevTools.state).toEqual(mergedCollection);
expect(DevTools['state']).toEqual(mergedCollection);
});
});

Expand All @@ -103,7 +105,7 @@ describe('DevTools', () => {
});
it('Sets the internal state correctly', async () => {
await Onyx.multiSet(exampleCollection);
expect(DevTools.state).toEqual(mergedCollection);
expect(DevTools['state']).toEqual(mergedCollection);
});
});

Expand All @@ -120,7 +122,7 @@ describe('DevTools', () => {
it('Clears internal state correctly', async () => {
await Onyx.merge(ONYX_KEYS.NUM_KEY, 2);
await Onyx.clear([ONYX_KEYS.NUM_KEY]);
expect(DevTools.state).toEqual({...initialKeyStates, [ONYX_KEYS.NUM_KEY]: 2});
expect(DevTools['state']).toEqual({...initialKeyStates, [ONYX_KEYS.NUM_KEY]: 2});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ const ONYX_KEYS = {
test('Cache eviction', () => {
const RECORD_TO_EVICT = 'evict';
const RECORD_TO_ADD = 'add';
const collection = {};
const collection: Record<string, unknown> = {};

// Given an evictable key previously set in storage
return StorageMock.setItem(`${ONYX_KEYS.COLLECTION.TEST_KEY}${RECORD_TO_EVICT}`, {test: 'evict'})
.then(() => {
// When we initialize Onyx and mark the set collection key as a safeEvictionKey
Onyx.init({
keys: ONYX_KEYS,
registerStorageEventListener: () => {},
safeEvictionKeys: [ONYX_KEYS.COLLECTION.TEST_KEY],
});

Expand Down
15 changes: 10 additions & 5 deletions tests/unit/fastMergeTest.js → tests/unit/fastMergeTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type {DeepRecord} from '../../lib/types';
import utils from '../../lib/utils';

const testObject = {
type DeepObject = DeepRecord<string, unknown> | unknown[];

const testObject: DeepObject = {
a: 'a',
b: {
c: 'c',
Expand All @@ -12,7 +15,7 @@ const testObject = {
},
};

const testObjectWithNullishValues = {
const testObjectWithNullishValues: DeepObject = {
a: undefined,
b: {
c: {
Expand All @@ -24,7 +27,7 @@ const testObjectWithNullishValues = {
},
};

const testObjectWithNullValuesRemoved = {
const testObjectWithNullValuesRemoved: DeepObject = {
b: {
c: {
h: 'h',
Expand Down Expand Up @@ -82,13 +85,15 @@ describe('fastMerge', () => {
});

it('should replace an object with an array', () => {
const result = utils.fastMerge(testObject, [1, 2, 3]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = utils.fastMerge(testObject, [1, 2, 3] as any);
blazejkustra marked this conversation as resolved.
Show resolved Hide resolved

expect(result).toEqual([1, 2, 3]);
});

it('should replace an array with an object', () => {
const result = utils.fastMerge([1, 2, 3], testObject);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const result = utils.fastMerge([1, 2, 3] as any, testObject);

expect(result).toEqual(testObject);
});
Expand Down
Loading
Loading