-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Expand file tree
/
Copy pathReferenceManyCountBase.stories.tsx
More file actions
143 lines (135 loc) · 4.32 KB
/
ReferenceManyCountBase.stories.tsx
File metadata and controls
143 lines (135 loc) · 4.32 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
import * as React from 'react';
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
import { RecordContextProvider } from '../record';
import { DataProviderContext } from '../../dataProvider';
import { ResourceContextProvider } from '../../core';
import { TestMemoryRouter } from '../../routing';
import { ReferenceManyCountBase } from './ReferenceManyCountBase';
export default {
title: 'ra-core/controller/field/ReferenceManyCountBase',
excludeStories: ['Wrapper'],
};
const post = {
id: 1,
title: 'Lorem Ipsum',
};
const comments = [
{ id: 1, post_id: 1, is_published: true },
{ id: 2, post_id: 1, is_published: true },
{ id: 3, post_id: 1, is_published: false },
{ id: 4, post_id: 2, is_published: true },
{ id: 5, post_id: 2, is_published: false },
];
export const Wrapper = ({ dataProvider, children }) => (
<TestMemoryRouter>
<DataProviderContext.Provider value={dataProvider}>
<QueryClientProvider
client={
new QueryClient({
defaultOptions: {
queries: {
retry: false,
},
},
})
}
>
<ResourceContextProvider value="posts">
<RecordContextProvider value={post}>
{children}
</RecordContextProvider>
</ResourceContextProvider>
</QueryClientProvider>
</DataProviderContext.Provider>
</TestMemoryRouter>
);
export const Basic = () => (
<Wrapper
dataProvider={{
getManyReference: () =>
Promise.resolve({
data: [comments.filter(c => c.post_id === 1)[0]],
total: comments.filter(c => c.post_id === 1).length,
}),
}}
>
<ReferenceManyCountBase reference="comments" target="post_id" />
</Wrapper>
);
export const LoadingState = () => (
<Wrapper dataProvider={{ getManyReference: () => new Promise(() => {}) }}>
<ReferenceManyCountBase
reference="comments"
target="post_id"
loading="loading..."
/>
</Wrapper>
);
export const ErrorState = () => (
<Wrapper
dataProvider={{
getManyReference: () => Promise.reject(new Error('problem')),
}}
>
<ReferenceManyCountBase
reference="comments"
target="post_id"
error="Error!"
/>
</Wrapper>
);
export const Filter = () => (
<Wrapper
dataProvider={{
getManyReference: (resource, params) =>
Promise.resolve({
data: comments
.filter(c => c.post_id === 1)
.filter(post =>
Object.keys(params.filter).every(
key => post[key] === params.filter[key]
)
),
total: comments
.filter(c => c.post_id === 1)
.filter(post =>
Object.keys(params.filter).every(
key => post[key] === params.filter[key]
)
).length,
}),
}}
>
<ReferenceManyCountBase
reference="comments"
target="post_id"
filter={{ is_published: true }}
/>
</Wrapper>
);
export const Slow = () => (
<Wrapper
dataProvider={{
getManyReference: () =>
new Promise(resolve =>
setTimeout(
() =>
resolve({
data: [
comments.filter(c => c.post_id === 1)[0],
],
total: comments.filter(c => c.post_id === 1)
.length,
}),
2000
)
),
}}
>
<ReferenceManyCountBase
reference="comments"
target="post_id"
loading="Loading..."
/>
</Wrapper>
);