Skip to content

(DOCSP-26983): @realm/react-ify "Sets - React Native SDK" #2477

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

Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 2 additions & 9 deletions examples/react-native/__tests__/js/CRUD/create-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ describe('Create Data Tests', () => {
return (
<>
<TextInput onChangeText={setDogName} value={dogName} />
<Button
onPress={() => handleAddDog()}
title='Add Dog'
testID='handleAddDogBtn'
/>
<Button onPress={() => handleAddDog()} title='Add Dog' testID='handleAddDogBtn' />
</>
);
};
Expand All @@ -71,10 +67,7 @@ describe('Create Data Tests', () => {
const {getByTestId} = render(<App />);

// press the "Add Dog" button
const handleAddDogBtn = await waitFor(
() => getByTestId('handleAddDogBtn'),
{timeout: 5000},
);
const handleAddDogBtn = await waitFor(() => getByTestId('handleAddDogBtn'), {timeout: 5000});
await act(async () => {
fireEvent.press(handleAddDogBtn);
});
Expand Down
29 changes: 5 additions & 24 deletions examples/react-native/__tests__/js/CRUD/delete-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ describe('Delete Data Tests', () => {
return (
<>
<Text>{dog.name}</Text>
<Button
onPress={() => deleteDog(dog)}
title='Delete Dog'
testID='deleteDog'
/>
<Button onPress={() => deleteDog(dog)} title='Delete Dog' testID='deleteDog' />
</>
);
})}
Expand Down Expand Up @@ -120,16 +116,8 @@ describe('Delete Data Tests', () => {
</>
);
})}
<Button
onPress={() => deleteAllYoungDogObjects()}
title='Delete Young Dog Objects'
testID='deleteYoungDogs'
/>
<Button
onPress={() => deleteAllDogObjects()}
title='Delete All Dog Objects'
testID='deleteAllDogs'
/>
<Button onPress={() => deleteAllYoungDogObjects()} title='Delete Young Dog Objects' testID='deleteYoungDogs' />
<Button onPress={() => deleteAllDogObjects()} title='Delete All Dog Objects' testID='deleteAllDogs' />
</>
);
};
Expand All @@ -145,10 +133,7 @@ describe('Delete Data Tests', () => {
await waitFor(() => getAllByTestId('dogItem'), {timeout: 5000}); // even though we don't use this as variable, react-native-testing-library requires us to waitFor() this to avoid the following error: "Unable to find an element with testID: dogItem"

// Test that the young Dog objects (Bronson, Bowie) have been deleted from the realm + from the UI when the "Delete All Dog Objects" is pressed, leaving 1 dog object (Blaise) remaining
const deleteYoungDogsBtn = await waitFor(
() => getByTestId('deleteYoungDogs'),
{timeout: 5000},
);
const deleteYoungDogsBtn = await waitFor(() => getByTestId('deleteYoungDogs'), {timeout: 5000});
await act(async () => {
fireEvent.press(deleteYoungDogsBtn);
});
Expand Down Expand Up @@ -183,11 +168,7 @@ describe('Delete Data Tests', () => {
return (
<>
<Text>Delete all data in your profile:</Text>
<Button
onPress={deleteAllData}
title='Delete all data'
testID='deleteAllData'
/>
<Button onPress={deleteAllData} title='Delete all data' testID='deleteAllData' />
</>
);
};
Expand Down
103 changes: 24 additions & 79 deletions examples/react-native/__tests__/js/CRUD/read-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,8 @@ describe('Read Data Tests', () => {
const {getByTestId} = render(<App />);

// test that the 'useObject()' method worked and the correct sentence is rendered as expected
const taskItemSentence = await waitFor(
() => getByTestId('task-item-sentence'),
{timeout: 5000},
);
expect(taskItemSentence.props.children.join('')).toBe(
'Wash the dishes is a task with the priority of: 3',
);
const taskItemSentence = await waitFor(() => getByTestId('task-item-sentence'), {timeout: 5000});
expect(taskItemSentence.props.children.join('')).toBe('Wash the dishes is a task with the priority of: 3');
});

it('should filter data', async () => {
Expand All @@ -116,9 +111,7 @@ describe('Read Data Tests', () => {
const highPriorityTasks = tasks.filtered('priority >= 4');

// filter for tasks that have just-started or short-running progress
const lowProgressTasks = tasks.filtered(
'1 <= progressMinutes && progressMinutes < 10',
);
const lowProgressTasks = tasks.filtered('1 <= progressMinutes && progressMinutes < 10');
return (
<>
<Text>Your high priority tasks:</Text>
Expand All @@ -143,26 +136,14 @@ describe('Read Data Tests', () => {
const {getAllByTestId} = render(<App />);

// test that the highPriorityTasks items Text renders
const highPriorityTasksUIList = await waitFor(
() => getAllByTestId('high-priority-element'),
{timeout: 5000},
);
expect(highPriorityTasksUIList[0].children[0].toString()).toBe(
'Do the laundry',
); // Since only the 'Do the laundry' task is high priority
const highPriorityTasksUIList = await waitFor(() => getAllByTestId('high-priority-element'), {timeout: 5000});
expect(highPriorityTasksUIList[0].children[0].toString()).toBe('Do the laundry'); // Since only the 'Do the laundry' task is high priority

// test that the highPriorityTasks items Text renders
const lowProgressTasksUIList = await waitFor(
() => getAllByTestId('low-progress-element'),
{timeout: 5000},
);
const lowProgressTasksUIList = await waitFor(() => getAllByTestId('low-progress-element'), {timeout: 5000});
// test that both 'Wash the dishes' and 'Gym Workout' rendered because they are both low progress tasks
expect(lowProgressTasksUIList[0].children[0].toString()).toBe(
'Wash the dishes',
);
expect(lowProgressTasksUIList[1].children[0].toString()).toBe(
'Gym Workout',
);
expect(lowProgressTasksUIList[0].children[0].toString()).toBe('Wash the dishes');
expect(lowProgressTasksUIList[1].children[0].toString()).toBe('Gym Workout');
});

it('should render sorted tasks', async () => {
Expand Down Expand Up @@ -208,13 +189,9 @@ describe('Read Data Tests', () => {
<Text testID='tasks-by-name-descending-item'>{task.name}</Text>
))}

<Text>
Tasks sorted by priority descending, and name alphabetically:
</Text>
<Text>Tasks sorted by priority descending, and name alphabetically:</Text>
{tasksByPriorityDescendingAndName.map(task => (
<Text testID='tasks-by-priority-descending-and-name-item'>
{task.name}
</Text>
<Text testID='tasks-by-priority-descending-and-name-item'>{task.name}</Text>
))}

<Text>Tasks sorted by assignee name:</Text>
Expand All @@ -235,65 +212,33 @@ describe('Read Data Tests', () => {
const {getAllByTestId} = render(<App />);

// test that tasks should be in the order that they were written
const allTasksUIList = await waitFor(
() => getAllByTestId('all-tasks-item'),
{timeout: 5000},
);
const allTasksUIList = await waitFor(() => getAllByTestId('all-tasks-item'), {timeout: 5000});
expect(allTasksUIList[0].children[0].toString()).toBe('Wash the dishes');
expect(allTasksUIList[1].children[0].toString()).toBe('Do the laundry');
expect(allTasksUIList[2].children[0].toString()).toBe('Gym Workout');

// test that tasksByName should be in alphabetical name order
const taskByNameUIList = await waitFor(
() => getAllByTestId('tasks-by-name-item'),
{timeout: 5000},
);
const taskByNameUIList = await waitFor(() => getAllByTestId('tasks-by-name-item'), {timeout: 5000});
expect(taskByNameUIList[0].children[0].toString()).toBe('Do the laundry');
expect(taskByNameUIList[1].children[0].toString()).toBe('Gym Workout');
expect(taskByNameUIList[2].children[0].toString()).toBe('Wash the dishes');

// test that tasksByNameDescending should be in reverse alphabetical name order
const taskByNameDescendingUIList = await waitFor(
() => getAllByTestId('tasks-by-name-descending-item'),
{timeout: 5000},
);
expect(taskByNameDescendingUIList[0].children[0].toString()).toBe(
'Wash the dishes',
);
expect(taskByNameDescendingUIList[1].children[0].toString()).toBe(
'Gym Workout',
);
expect(taskByNameDescendingUIList[2].children[0].toString()).toBe(
'Do the laundry',
);
const taskByNameDescendingUIList = await waitFor(() => getAllByTestId('tasks-by-name-descending-item'), {timeout: 5000});
expect(taskByNameDescendingUIList[0].children[0].toString()).toBe('Wash the dishes');
expect(taskByNameDescendingUIList[1].children[0].toString()).toBe('Gym Workout');
expect(taskByNameDescendingUIList[2].children[0].toString()).toBe('Do the laundry');

// test that tasksByNameDescending should be in reverse alphabetical name order
const tasksByPriorityDescendingAndNameUIList = await waitFor(() =>
getAllByTestId('tasks-by-priority-descending-and-name-item'),
);
expect(
tasksByPriorityDescendingAndNameUIList[0].children[0].toString(),
).toBe('Do the laundry');
expect(
tasksByPriorityDescendingAndNameUIList[1].children[0].toString(),
).toBe('Gym Workout');
expect(
tasksByPriorityDescendingAndNameUIList[2].children[0].toString(),
).toBe('Wash the dishes');
const tasksByPriorityDescendingAndNameUIList = await waitFor(() => getAllByTestId('tasks-by-priority-descending-and-name-item'));
expect(tasksByPriorityDescendingAndNameUIList[0].children[0].toString()).toBe('Do the laundry');
expect(tasksByPriorityDescendingAndNameUIList[1].children[0].toString()).toBe('Gym Workout');
expect(tasksByPriorityDescendingAndNameUIList[2].children[0].toString()).toBe('Wash the dishes');

// test that tasksByNameDescending should be in reverse alphabetical name order
const tasksByAssigneeNameUIList = await waitFor(
() => getAllByTestId('tasks-by-assignee-name-item'),
{timeout: 5000},
);
expect(tasksByAssigneeNameUIList[0].children[0].toString()).toBe(
'Wash the dishes',
);
expect(tasksByAssigneeNameUIList[1].children[0].toString()).toBe(
'Do the laundry',
);
expect(tasksByAssigneeNameUIList[2].children[0].toString()).toBe(
'Gym Workout',
);
const tasksByAssigneeNameUIList = await waitFor(() => getAllByTestId('tasks-by-assignee-name-item'), {timeout: 5000});
expect(tasksByAssigneeNameUIList[0].children[0].toString()).toBe('Wash the dishes');
expect(tasksByAssigneeNameUIList[1].children[0].toString()).toBe('Do the laundry');
expect(tasksByAssigneeNameUIList[2].children[0].toString()).toBe('Gym Workout');
});
});
47 changes: 9 additions & 38 deletions examples/react-native/__tests__/js/CRUD/update-test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const realmConfig = {
deleteRealmIfMigrationNeeded: true,
};

const {RealmProvider, useRealm, useObject, useQuery} =
createRealmContext(realmConfig);
const {RealmProvider, useRealm, useObject, useQuery} = createRealmContext(realmConfig);

let assertionRealm;

Expand Down Expand Up @@ -75,11 +74,7 @@ describe('Update Data Tests', () => {
<Text>Task: {myTask.name}</Text>
<Text>Progress made (in minutes):</Text>
<Text testID='progressMinutes'>{myTask.progressMinutes}</Text>
<Button
onPress={() => incrementTaskProgress()}
title='Increment Task Progress'
testID='handleIncrementBtn'
/>
<Button onPress={() => incrementTaskProgress()} title='Increment Task Progress' testID='handleIncrementBtn' />
</>
);
} else {
Expand All @@ -97,14 +92,8 @@ describe('Update Data Tests', () => {
);
const {getByTestId} = render(<App />);

const handleIncrementBtn = await waitFor(
() => getByTestId('handleIncrementBtn'),
{timeout: 5000},
);
const progressMinutesText = await waitFor(
() => getByTestId('progressMinutes'),
{timeout: 5000},
);
const handleIncrementBtn = await waitFor(() => getByTestId('handleIncrementBtn'), {timeout: 5000});
const progressMinutesText = await waitFor(() => getByTestId('progressMinutes'), {timeout: 5000});

const paintTask = assertionRealm.objectForPrimaryKey(Task, 92140);

Expand Down Expand Up @@ -135,20 +124,12 @@ describe('Update Data Tests', () => {
realm.write(() => {
// Add a new Task to the realm. Since no task with ID 1234
// has been added yet, this adds the instance to the realm.
myTask = realm.create(
'Task',
{_id: 1234, name: 'Wash the car', progressMinutes: 0},
'modified',
);
myTask = realm.create('Task', {_id: 1234, name: 'Wash the car', progressMinutes: 0}, 'modified');

// If an object exists, setting the third parameter (`updateMode`) to
// "modified" only updates properties that have changed, resulting in
// faster operations.
myTask = realm.create(
'Task',
{_id: 1234, name: 'Wash the car', progressMinutes: 5},
'modified',
);
myTask = realm.create('Task', {_id: 1234, name: 'Wash the car', progressMinutes: 5}, 'modified');
});
return (
<>
Expand All @@ -168,10 +149,7 @@ describe('Update Data Tests', () => {
);
const {getByTestId} = render(<App />);

const progressMinutesText = await waitFor(
() => getByTestId('progressMinutes'),
{timeout: 5000},
);
const progressMinutesText = await waitFor(() => getByTestId('progressMinutes'), {timeout: 5000});
const carWashTask = assertionRealm.objectForPrimaryKey(Task, 1234);

// Test that the the 'Wash the car' task was upserted, and progressMinutesText is now displaying 5 minutes progressed
Expand Down Expand Up @@ -204,11 +182,7 @@ describe('Update Data Tests', () => {
{task.name} has {task.progressMinutes} minutes progressed
</Text>;
})}
<Button
onPress={resetProgressOnAllTasks}
title='Reset Progress'
testID='resetProgressOnAllTasksBtn'
/>
<Button onPress={resetProgressOnAllTasks} title='Reset Progress' testID='resetProgressOnAllTasksBtn' />
</>
);
};
Expand All @@ -222,10 +196,7 @@ describe('Update Data Tests', () => {
);
const {getByTestId} = render(<App />);

const resetProgressOnAllTasksBtn = await waitFor(
() => getByTestId('resetProgressOnAllTasksBtn'),
{timeout: 5000},
);
const resetProgressOnAllTasksBtn = await waitFor(() => getByTestId('resetProgressOnAllTasksBtn'), {timeout: 5000});

await act(async () => {
fireEvent.press(resetProgressOnAllTasksBtn);
Expand Down
17 changes: 17 additions & 0 deletions examples/react-native/__tests__/js/Models/Character.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Realm from 'realm';

// :snippet-start: js-character-schema
class Character extends Realm.Object {
static schema = {
name: 'Character',
primaryKey: '_id',
properties: {
_id: 'objectId',
name: 'string',
levelsCompleted: 'int<>',
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated in other PRs, I think it's worthwhile to show both the shorthand and object notation for defining schema properties:

levelsCompleted: { 
	type: "set",
	objectType: "int"
}

inventory: 'string<>',
},
};
}
// :snippet-end:
export default Character;
2 changes: 1 addition & 1 deletion examples/react-native/__tests__/js/models/Book.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Book extends Realm.Object {
static schema = {
name: 'Book',
properties: {
name: { type: 'string', indexed: true },
name: {type: 'string', indexed: true},
price: 'int?',
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ describe('Mixed Tests', () => {
const {getByTestId} = render(<App />);
const catBirthDate = await waitFor(() => getByTestId('catBirthDate'));
// Expect catBirthDate in the UI to be the same value we set in the beforeEach (which is clover's birthday 'January 21, 2016')
expect(new Date(catBirthDate.props.children)).toStrictEqual(
new Date('January 21, 2016'),
);
expect(new Date(catBirthDate.props.children)).toStrictEqual(new Date('January 21, 2016'));
});
});
Loading