-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathHypergraphSpaceContext.test.tsx
More file actions
193 lines (155 loc) · 6.21 KB
/
Copy pathHypergraphSpaceContext.test.tsx
File metadata and controls
193 lines (155 loc) · 6.21 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { Repo } from '@automerge/automerge-repo';
import { RepoContext } from '@automerge/automerge-repo-react-hooks';
import { Entity, Type, store } from '@graphprotocol/hypergraph';
import '@testing-library/jest-dom/vitest';
import { act, cleanup, renderHook, waitFor } from '@testing-library/react';
// biome-ignore lint/style/useImportType: <explanation>
import React from 'react';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import {
HypergraphSpaceProvider,
useCreateEntity,
useDeleteEntity,
useQueryEntity,
useQueryLocal,
useUpdateEntity,
} from '../src/HypergraphSpaceContext.js';
afterEach(() => {
cleanup();
});
describe('HypergraphSpaceContext', () => {
class Person extends Entity.Class<Person>('Person')({
name: Type.Text,
age: Type.Number,
}) {}
class User extends Entity.Class<User>('User')({
name: Type.Text,
email: Type.Text,
}) {}
class Event extends Entity.Class<Event>('Event')({
name: Type.Text,
}) {}
const spaceId = '1e5e39da-a00d-4fd8-b53b-98095337112f';
let repo = new Repo({});
let wrapper = ({ children }: Readonly<{ children: React.ReactNode }>) => (
<RepoContext.Provider value={repo}>
<HypergraphSpaceProvider space={spaceId}>{children}</HypergraphSpaceProvider>
</RepoContext.Provider>
);
beforeEach(() => {
repo = new Repo({});
store.send({ type: 'setRepo', repo });
store.send({
type: 'setSpace',
spaceId,
spaceState: {
id: spaceId,
members: {},
invitations: {},
removedMembers: {},
inboxes: {},
lastEventHash: '',
},
name: 'Test Space',
updates: { updates: [], firstUpdateClock: 0, lastUpdateClock: 0 },
events: [],
inboxes: [],
keys: [],
});
wrapper = ({ children }: Readonly<{ children: React.ReactNode }>) => (
<RepoContext.Provider value={repo}>
<HypergraphSpaceProvider space={spaceId}>{children}</HypergraphSpaceProvider>
</RepoContext.Provider>
);
});
describe('useCreateEntity', () => {
it('should be able to create an entity through the useCreateEntity Hook', async () => {
const { result: queryEntitiesResult, rerender } = renderHook(() => useQueryLocal(Event), { wrapper });
const { result: createEntityResult } = renderHook(() => useCreateEntity(Event), { wrapper });
let createdEntity: Entity.Entity<typeof Event> | null = null;
act(() => {
createdEntity = createEntityResult.current({ name: 'Conference' });
});
await waitFor(() => {
expect(createdEntity).not.toBeNull();
});
if (createdEntity != null) {
const { result: queryEntityResult } = renderHook(() => useQueryEntity(Event, createdEntity?.id || ''), {
wrapper,
});
expect(queryEntityResult.current).toEqual(createdEntity);
}
rerender();
expect(queryEntitiesResult.current).toEqual({ deletedEntities: [], entities: [createdEntity] });
});
});
describe('useUpdateEntity', () => {
it('should be able to update a created entity through the useUpdateEntity hook', async () => {
const { result: createEntityResult } = renderHook(() => useCreateEntity(Person), { wrapper });
let createdEntity: Entity.Entity<typeof Person> | null = null;
act(() => {
createdEntity = createEntityResult.current({ name: 'Test', age: 1 });
});
await waitFor(() => {
expect(createdEntity).not.toBeNull();
expect(createdEntity).toEqual(
expect.objectContaining({ name: 'Test', age: 1, type: Person.name, __deleted: false }),
);
});
if (createdEntity == null) {
throw new Error('person not created successfully');
}
const id = (createdEntity as Entity.Entity<typeof Person>).id;
const {
result: { current: updateEntity },
} = renderHook(() => useUpdateEntity(Person), { wrapper });
act(() => {
createdEntity = updateEntity(id, { name: 'Test User', age: 2112 });
});
expect(createdEntity).toEqual({ id, name: 'Test User', age: 2112, type: Person.name });
const { result: queryEntityResult } = renderHook(() => useQueryEntity(Person, id), { wrapper });
// @ts-expect-error - TODO: fix the types error
expect(queryEntityResult.current).toEqual({ ...createdEntity, __version: '', __deleted: false });
const { result: queryEntitiesResult, rerender } = renderHook(() => useQueryLocal(Person), { wrapper });
rerender();
expect(queryEntitiesResult.current).toEqual({
deletedEntities: [],
// @ts-expect-error - TODO: fix the types error
entities: [{ ...createdEntity, __version: '', __deleted: false }],
});
});
});
describe('useDeleteEntity', () => {
it('should be able to delete the created entity', async () => {
const { result: createEntityResult } = renderHook(() => useCreateEntity(User), { wrapper });
let createdEntity: Entity.Entity<typeof User> | null = null;
act(() => {
createdEntity = createEntityResult.current({ name: 'Test', email: 'test.user@edgeandnode.com' });
});
await waitFor(() => {
expect(createdEntity).not.toBeNull();
expect(createdEntity).toEqual(
expect.objectContaining({ name: 'Test', email: 'test.user@edgeandnode.com', type: User.name }),
);
});
const { result: queryEntitiesResult, rerender: rerenderQueryEntities } = renderHook(() => useQueryLocal(User), {
wrapper,
});
rerenderQueryEntities();
expect(queryEntitiesResult.current).toEqual({ deletedEntities: [], entities: [createdEntity] });
const { result: deleteEntityResult } = renderHook(() => useDeleteEntity(), { wrapper });
let deleted = false;
act(() => {
// biome-ignore lint/style/noNonNullAssertion: <explanation>
deleted = deleteEntityResult.current(createdEntity!.id);
});
await waitFor(() => {
expect(deleted).toBe(true);
});
rerenderQueryEntities();
expect(queryEntitiesResult.current.entities).toHaveLength(0);
expect(queryEntitiesResult.current.entities).toEqual([]);
expect(queryEntitiesResult.current.deletedEntities).toHaveLength(1);
});
});
});