-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathuseUpdateCellDispatch.test.tsx
More file actions
157 lines (132 loc) · 4.85 KB
/
useUpdateCellDispatch.test.tsx
File metadata and controls
157 lines (132 loc) · 4.85 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
import { renderHook, waitFor } from '@testing-library/react';
import type React from 'react';
import * as Y from 'yjs';
import { DatabaseContext, DatabaseContextState, FieldType } from '@/application/database-yjs';
import { useUpdateCellDispatch, useUpdateStartEndTimeCell } from '@/application/database-yjs/dispatch';
import {
RowId,
YDatabase,
YDatabaseField,
YDatabaseFields,
YDatabaseView,
YDatabaseViews,
YDoc,
YjsDatabaseKey,
YjsEditorKey,
} from '@/application/types';
import { createRowDoc } from './test-helpers';
jest.mock('@/utils/runtime-config', () => ({
getConfigValue: (_key: string, fallback: string) => fallback,
}));
const databaseId = 'database-id';
const viewId = 'view-id';
const rowId = 'row-id';
const fieldId = 'name-field-id';
function createTextField(): YDatabaseField {
const field = new Y.Map() as YDatabaseField;
field.set(YjsDatabaseKey.id, fieldId);
field.set(YjsDatabaseKey.name, 'Name');
field.set(YjsDatabaseKey.type, FieldType.RichText);
return field;
}
function createDatabaseDoc(): YDoc {
const doc = new Y.Doc({ guid: databaseId }) as YDoc;
const sharedRoot = doc.getMap(YjsEditorKey.data_section);
const database = new Y.Map() as YDatabase;
const fields = new Y.Map<YDatabaseField>() as YDatabaseFields;
const views = new Y.Map<YDatabaseView>() as YDatabaseViews;
const view = new Y.Map() as YDatabaseView;
fields.set(fieldId, createTextField());
views.set(viewId, view);
database.set(YjsDatabaseKey.id, databaseId);
database.set(YjsDatabaseKey.fields, fields);
database.set(YjsDatabaseKey.views, views);
sharedRoot.set(YjsEditorKey.database, database);
return doc;
}
function getCellData(rowDoc: YDoc) {
const row = rowDoc.getMap(YjsEditorKey.data_section).get(YjsEditorKey.database_row);
const cells = row?.get(YjsDatabaseKey.cells);
return cells?.get(fieldId)?.get(YjsDatabaseKey.data);
}
function getCell(rowDoc: YDoc) {
const row = rowDoc.getMap(YjsEditorKey.data_section).get(YjsEditorKey.database_row);
const cells = row?.get(YjsDatabaseKey.cells);
return cells?.get(fieldId);
}
function createWrapper(contextValue: DatabaseContextState) {
return ({ children }: { children: React.ReactNode }) => (
<DatabaseContext.Provider value={contextValue}>{children}</DatabaseContext.Provider>
);
}
describe('useUpdateCellDispatch', () => {
it('ensures a missing row doc before committing the cell update', async () => {
const databaseDoc = createDatabaseDoc();
const rowDoc = createRowDoc(rowId, databaseId, {});
const ensureRow = jest.fn<Promise<YDoc>, [RowId]>().mockResolvedValue(rowDoc);
const contextValue: DatabaseContextState = {
readOnly: false,
databaseDoc,
databasePageId: viewId,
activeViewId: viewId,
rowMap: {},
ensureRow,
workspaceId: 'workspace-id',
};
const { result } = renderHook(() => useUpdateCellDispatch(rowId, fieldId), {
wrapper: createWrapper(contextValue),
});
result.current('Recovered value');
await waitFor(() => {
expect(getCellData(rowDoc)).toBe('Recovered value');
});
expect(ensureRow).toHaveBeenCalledWith(rowId);
});
});
describe('useUpdateStartEndTimeCell', () => {
it('ensures a missing row doc before committing the calendar time update', async () => {
const databaseDoc = createDatabaseDoc();
const rowDoc = createRowDoc(rowId, databaseId, {});
const ensureRow = jest.fn<Promise<YDoc>, [RowId]>().mockResolvedValue(rowDoc);
const contextValue: DatabaseContextState = {
readOnly: false,
databaseDoc,
databasePageId: viewId,
activeViewId: viewId,
rowMap: {},
ensureRow,
workspaceId: 'workspace-id',
};
const { result } = renderHook(() => useUpdateStartEndTimeCell(), {
wrapper: createWrapper(contextValue),
});
result.current(rowId, fieldId, '100', '200', false);
await waitFor(() => {
const cell = getCell(rowDoc);
expect(cell?.get(YjsDatabaseKey.data)).toBe('100');
expect(cell?.get(YjsDatabaseKey.end_timestamp)).toBe('200');
expect(cell?.get(YjsDatabaseKey.include_time)).toBe(true);
});
expect(ensureRow).toHaveBeenCalledWith(rowId);
});
it('does not commit calendar time updates when the row doc cannot be loaded', async () => {
const databaseDoc = createDatabaseDoc();
const ensureRow = jest.fn<Promise<YDoc | undefined>, [RowId]>().mockResolvedValue(undefined);
const contextValue: DatabaseContextState = {
readOnly: false,
databaseDoc,
databasePageId: viewId,
activeViewId: viewId,
rowMap: {},
ensureRow,
workspaceId: 'workspace-id',
};
const { result } = renderHook(() => useUpdateStartEndTimeCell(), {
wrapper: createWrapper(contextValue),
});
result.current(rowId, fieldId, '100');
await waitFor(() => {
expect(ensureRow).toHaveBeenCalledWith(rowId);
});
});
});