@scm-manager/ui-api 2.31.1-20220118-144252 → 2.31.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/ApiProvider.test.tsx +4 -4
- package/src/ApiProvider.tsx +3 -3
- package/src/LegacyContext.tsx +4 -34
- package/src/base.test.ts +33 -34
- package/src/index.ts +0 -2
- package/src/login.test.ts +13 -14
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scm-manager/ui-api",
|
|
3
|
-
"version": "2.31.1
|
|
3
|
+
"version": "2.31.1",
|
|
4
4
|
"description": "React hook api for the SCM-Manager backend",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"files": [
|
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
"react-test-renderer": "^17.0.1"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@scm-manager/ui-types": "^2.31.1
|
|
28
|
+
"@scm-manager/ui-types": "^2.31.1",
|
|
29
29
|
"fetch-mock-jest": "^1.5.1",
|
|
30
30
|
"gitdiff-parser": "^0.1.2",
|
|
31
31
|
"query-string": "6.14.1",
|
package/src/ApiProvider.test.tsx
CHANGED
|
@@ -23,20 +23,20 @@
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
import { LegacyContext, useLegacyContext } from "./LegacyContext";
|
|
26
|
-
import * as React from "react";
|
|
27
26
|
import { FC } from "react";
|
|
28
27
|
import { renderHook } from "@testing-library/react-hooks";
|
|
28
|
+
import * as React from "react";
|
|
29
29
|
import ApiProvider from "./ApiProvider";
|
|
30
30
|
import { useQueryClient } from "react-query";
|
|
31
31
|
|
|
32
32
|
describe("ApiProvider tests", () => {
|
|
33
|
-
const createWrapper = (context
|
|
33
|
+
const createWrapper = (context?: LegacyContext): FC => {
|
|
34
34
|
return ({ children }) => <ApiProvider {...context}>{children}</ApiProvider>;
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
it("should register QueryClient", () => {
|
|
38
38
|
const { result } = renderHook(() => useQueryClient(), {
|
|
39
|
-
wrapper: createWrapper(
|
|
39
|
+
wrapper: createWrapper(),
|
|
40
40
|
});
|
|
41
41
|
expect(result.current).toBeDefined();
|
|
42
42
|
});
|
|
@@ -48,7 +48,7 @@ describe("ApiProvider tests", () => {
|
|
|
48
48
|
};
|
|
49
49
|
|
|
50
50
|
const { result } = renderHook(() => useLegacyContext(), {
|
|
51
|
-
wrapper: createWrapper({ onIndexFetched
|
|
51
|
+
wrapper: createWrapper({ onIndexFetched }),
|
|
52
52
|
});
|
|
53
53
|
|
|
54
54
|
if (result.current?.onIndexFetched) {
|
package/src/ApiProvider.tsx
CHANGED
package/src/LegacyContext.tsx
CHANGED
|
@@ -24,17 +24,12 @@
|
|
|
24
24
|
|
|
25
25
|
import { IndexResources, Me } from "@scm-manager/ui-types";
|
|
26
26
|
import React, { createContext, FC, useContext } from "react";
|
|
27
|
-
import { QueryClient, useQueryClient } from "react-query";
|
|
28
27
|
|
|
29
|
-
export type
|
|
28
|
+
export type LegacyContext = {
|
|
30
29
|
onIndexFetched?: (index: IndexResources) => void;
|
|
31
30
|
onMeFetched?: (me: Me) => void;
|
|
32
31
|
};
|
|
33
32
|
|
|
34
|
-
export type LegacyContext = BaseContext & {
|
|
35
|
-
initialize: () => void;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
33
|
const Context = createContext<LegacyContext | undefined>(undefined);
|
|
39
34
|
|
|
40
35
|
export const useLegacyContext = () => {
|
|
@@ -45,31 +40,6 @@ export const useLegacyContext = () => {
|
|
|
45
40
|
return context;
|
|
46
41
|
};
|
|
47
42
|
|
|
48
|
-
const
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
initialize: () => {
|
|
52
|
-
if (ctx.onIndexFetched) {
|
|
53
|
-
const index: IndexResources | undefined = queryClient.getQueryData("index");
|
|
54
|
-
if (index) {
|
|
55
|
-
ctx.onIndexFetched(index);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (ctx.onMeFetched) {
|
|
59
|
-
const me: Me | undefined = queryClient.getQueryData("me");
|
|
60
|
-
if (me) {
|
|
61
|
-
ctx.onMeFetched(me);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
return ctx;
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
export const LegacyContextProvider: FC<BaseContext> = ({ onIndexFetched, onMeFetched, children }) => {
|
|
71
|
-
const queryClient = useQueryClient();
|
|
72
|
-
const ctx = createInitialContext(queryClient, { onIndexFetched, onMeFetched });
|
|
73
|
-
|
|
74
|
-
return <Context.Provider value={ctx}>{children}</Context.Provider>;
|
|
75
|
-
};
|
|
43
|
+
export const LegacyContextProvider: FC<LegacyContext> = ({ onIndexFetched, onMeFetched, children }) => (
|
|
44
|
+
<Context.Provider value={{ onIndexFetched, onMeFetched }}>{children}</Context.Provider>
|
|
45
|
+
);
|
package/src/base.test.ts
CHANGED
|
@@ -34,7 +34,7 @@ describe("Test base api hooks", () => {
|
|
|
34
34
|
describe("useIndex tests", () => {
|
|
35
35
|
fetchMock.get("/api/v2/", {
|
|
36
36
|
version: "x.y.z",
|
|
37
|
-
_links: {}
|
|
37
|
+
_links: {},
|
|
38
38
|
});
|
|
39
39
|
|
|
40
40
|
it("should return index", async () => {
|
|
@@ -48,10 +48,9 @@ describe("Test base api hooks", () => {
|
|
|
48
48
|
it("should call onIndexFetched of LegacyContext", async () => {
|
|
49
49
|
let index: IndexResources;
|
|
50
50
|
const context: LegacyContext = {
|
|
51
|
-
onIndexFetched: fetchedIndex => {
|
|
51
|
+
onIndexFetched: (fetchedIndex) => {
|
|
52
52
|
index = fetchedIndex;
|
|
53
53
|
},
|
|
54
|
-
initialize: () => null
|
|
55
54
|
};
|
|
56
55
|
const { result, waitFor } = renderHook(() => useIndex(), { wrapper: createWrapper(context) });
|
|
57
56
|
await waitFor(() => {
|
|
@@ -71,10 +70,10 @@ describe("Test base api hooks", () => {
|
|
|
71
70
|
const queryClient = new QueryClient();
|
|
72
71
|
queryClient.setQueryData("index", {
|
|
73
72
|
version: "x.y.z",
|
|
74
|
-
_links: {}
|
|
73
|
+
_links: {},
|
|
75
74
|
});
|
|
76
75
|
const { result } = renderHook(() => useIndexLink("spaceships"), {
|
|
77
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
76
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
78
77
|
});
|
|
79
78
|
expect(result.current).toBeUndefined();
|
|
80
79
|
});
|
|
@@ -87,17 +86,17 @@ describe("Test base api hooks", () => {
|
|
|
87
86
|
spaceships: [
|
|
88
87
|
{
|
|
89
88
|
name: "heartOfGold",
|
|
90
|
-
href: "/spaceships/heartOfGold"
|
|
89
|
+
href: "/spaceships/heartOfGold",
|
|
91
90
|
},
|
|
92
91
|
{
|
|
93
92
|
name: "razorCrest",
|
|
94
|
-
href: "/spaceships/razorCrest"
|
|
95
|
-
}
|
|
96
|
-
]
|
|
97
|
-
}
|
|
93
|
+
href: "/spaceships/razorCrest",
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
98
97
|
});
|
|
99
98
|
const { result } = renderHook(() => useIndexLink("spaceships"), {
|
|
100
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
99
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
101
100
|
});
|
|
102
101
|
expect(result.current).toBeUndefined();
|
|
103
102
|
});
|
|
@@ -108,12 +107,12 @@ describe("Test base api hooks", () => {
|
|
|
108
107
|
version: "x.y.z",
|
|
109
108
|
_links: {
|
|
110
109
|
spaceships: {
|
|
111
|
-
href: "/api/spaceships"
|
|
112
|
-
}
|
|
113
|
-
}
|
|
110
|
+
href: "/api/spaceships",
|
|
111
|
+
},
|
|
112
|
+
},
|
|
114
113
|
});
|
|
115
114
|
const { result } = renderHook(() => useIndexLink("spaceships"), {
|
|
116
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
115
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
117
116
|
});
|
|
118
117
|
expect(result.current).toBe("/api/spaceships");
|
|
119
118
|
});
|
|
@@ -131,12 +130,12 @@ describe("Test base api hooks", () => {
|
|
|
131
130
|
version: "x.y.z",
|
|
132
131
|
_links: {
|
|
133
132
|
spaceships: {
|
|
134
|
-
href: "/api/spaceships"
|
|
135
|
-
}
|
|
136
|
-
}
|
|
133
|
+
href: "/api/spaceships",
|
|
134
|
+
},
|
|
135
|
+
},
|
|
137
136
|
});
|
|
138
137
|
const { result } = renderHook(() => useIndexLinks(), {
|
|
139
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
138
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
140
139
|
});
|
|
141
140
|
expect((result.current!.spaceships as Link).href).toBe("/api/spaceships");
|
|
142
141
|
});
|
|
@@ -151,10 +150,10 @@ describe("Test base api hooks", () => {
|
|
|
151
150
|
it("should return version", () => {
|
|
152
151
|
const queryClient = new QueryClient();
|
|
153
152
|
queryClient.setQueryData("index", {
|
|
154
|
-
version: "x.y.z"
|
|
153
|
+
version: "x.y.z",
|
|
155
154
|
});
|
|
156
155
|
const { result } = renderHook(() => useVersion(), {
|
|
157
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
156
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
158
157
|
});
|
|
159
158
|
expect(result.current).toBe("x.y.z");
|
|
160
159
|
});
|
|
@@ -165,10 +164,10 @@ describe("Test base api hooks", () => {
|
|
|
165
164
|
const queryClient = new QueryClient();
|
|
166
165
|
queryClient.setQueryData("index", {
|
|
167
166
|
version: "x.y.z",
|
|
168
|
-
_links: {}
|
|
167
|
+
_links: {},
|
|
169
168
|
});
|
|
170
169
|
const { result } = renderHook(() => useRequiredIndexLink("spaceships"), {
|
|
171
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
170
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
172
171
|
});
|
|
173
172
|
expect(result.error).toBeDefined();
|
|
174
173
|
});
|
|
@@ -179,12 +178,12 @@ describe("Test base api hooks", () => {
|
|
|
179
178
|
version: "x.y.z",
|
|
180
179
|
_links: {
|
|
181
180
|
spaceships: {
|
|
182
|
-
href: "/api/spaceships"
|
|
183
|
-
}
|
|
184
|
-
}
|
|
181
|
+
href: "/api/spaceships",
|
|
182
|
+
},
|
|
183
|
+
},
|
|
185
184
|
});
|
|
186
185
|
const { result } = renderHook(() => useRequiredIndexLink("spaceships"), {
|
|
187
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
186
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
188
187
|
});
|
|
189
188
|
expect(result.current).toBe("/api/spaceships");
|
|
190
189
|
});
|
|
@@ -197,19 +196,19 @@ describe("Test base api hooks", () => {
|
|
|
197
196
|
version: "x.y.z",
|
|
198
197
|
_links: {
|
|
199
198
|
spaceships: {
|
|
200
|
-
href: "/spaceships"
|
|
201
|
-
}
|
|
202
|
-
}
|
|
199
|
+
href: "/spaceships",
|
|
200
|
+
},
|
|
201
|
+
},
|
|
203
202
|
});
|
|
204
203
|
|
|
205
204
|
const spaceship = {
|
|
206
|
-
name: "heartOfGold"
|
|
205
|
+
name: "heartOfGold",
|
|
207
206
|
};
|
|
208
207
|
|
|
209
208
|
fetchMock.get("/api/v2/spaceships", spaceship);
|
|
210
209
|
|
|
211
210
|
const { result, waitFor } = renderHook(() => useIndexJsonResource<typeof spaceship>("spaceships"), {
|
|
212
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
211
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
213
212
|
});
|
|
214
213
|
|
|
215
214
|
await waitFor(() => {
|
|
@@ -224,11 +223,11 @@ describe("Test base api hooks", () => {
|
|
|
224
223
|
const queryClient = new QueryClient();
|
|
225
224
|
queryClient.setQueryData("index", {
|
|
226
225
|
version: "x.y.z",
|
|
227
|
-
_links: {}
|
|
226
|
+
_links: {},
|
|
228
227
|
});
|
|
229
228
|
|
|
230
229
|
const { result } = renderHook(() => useIndexJsonResource<{}>("spaceships"), {
|
|
231
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
230
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
232
231
|
});
|
|
233
232
|
|
|
234
233
|
expect(result.current.isLoading).toBe(false);
|
package/src/index.ts
CHANGED
package/src/login.test.ts
CHANGED
|
@@ -37,7 +37,7 @@ describe("Test login hooks", () => {
|
|
|
37
37
|
name: "tricia",
|
|
38
38
|
displayName: "Tricia",
|
|
39
39
|
groups: [],
|
|
40
|
-
_links: {}
|
|
40
|
+
_links: {},
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
describe("useMe tests", () => {
|
|
@@ -45,7 +45,7 @@ describe("Test login hooks", () => {
|
|
|
45
45
|
name: "tricia",
|
|
46
46
|
displayName: "Tricia",
|
|
47
47
|
groups: [],
|
|
48
|
-
_links: {}
|
|
48
|
+
_links: {},
|
|
49
49
|
});
|
|
50
50
|
|
|
51
51
|
it("should return me", async () => {
|
|
@@ -65,10 +65,9 @@ describe("Test login hooks", () => {
|
|
|
65
65
|
|
|
66
66
|
let me: Me;
|
|
67
67
|
const context: LegacyContext = {
|
|
68
|
-
onMeFetched: fetchedMe => {
|
|
68
|
+
onMeFetched: (fetchedMe) => {
|
|
69
69
|
me = fetchedMe;
|
|
70
70
|
},
|
|
71
|
-
initialize: () => null
|
|
72
71
|
};
|
|
73
72
|
|
|
74
73
|
const { result, waitFor } = renderHook(() => useMe(), { wrapper: createWrapper(context, queryClient) });
|
|
@@ -131,7 +130,7 @@ describe("Test login hooks", () => {
|
|
|
131
130
|
name: "_anonymous",
|
|
132
131
|
displayName: "Anonymous",
|
|
133
132
|
groups: [],
|
|
134
|
-
_links: {}
|
|
133
|
+
_links: {},
|
|
135
134
|
});
|
|
136
135
|
const { result } = renderHook(() => useSubject(), { wrapper: createWrapper(undefined, queryClient) });
|
|
137
136
|
|
|
@@ -159,8 +158,8 @@ describe("Test login hooks", () => {
|
|
|
159
158
|
cookie: true,
|
|
160
159
|
grant_type: "password",
|
|
161
160
|
username: "tricia",
|
|
162
|
-
password: "hitchhikersSecret!"
|
|
163
|
-
}
|
|
161
|
+
password: "hitchhikersSecret!",
|
|
162
|
+
},
|
|
164
163
|
});
|
|
165
164
|
|
|
166
165
|
// required because we invalidate the whole cache and react-query refetches the index
|
|
@@ -168,13 +167,13 @@ describe("Test login hooks", () => {
|
|
|
168
167
|
version: "x.y.z",
|
|
169
168
|
_links: {
|
|
170
169
|
login: {
|
|
171
|
-
href: "/second/login"
|
|
172
|
-
}
|
|
173
|
-
}
|
|
170
|
+
href: "/second/login",
|
|
171
|
+
},
|
|
172
|
+
},
|
|
174
173
|
});
|
|
175
174
|
|
|
176
175
|
const { result, waitForNextUpdate } = renderHook(() => useLogin(), {
|
|
177
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
176
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
178
177
|
});
|
|
179
178
|
const { login } = result.current;
|
|
180
179
|
expect(login).toBeDefined();
|
|
@@ -195,7 +194,7 @@ describe("Test login hooks", () => {
|
|
|
195
194
|
queryClient.setQueryData("me", tricia);
|
|
196
195
|
|
|
197
196
|
const { result } = renderHook(() => useLogin(), {
|
|
198
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
197
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
199
198
|
});
|
|
200
199
|
|
|
201
200
|
expect(result.current.login).toBeUndefined();
|
|
@@ -210,7 +209,7 @@ describe("Test login hooks", () => {
|
|
|
210
209
|
fetchMock.deleteOnce("/api/v2/logout", {});
|
|
211
210
|
|
|
212
211
|
const { result, waitForNextUpdate } = renderHook(() => useLogout(), {
|
|
213
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
212
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
214
213
|
});
|
|
215
214
|
const { logout } = result.current;
|
|
216
215
|
expect(logout).toBeDefined();
|
|
@@ -230,7 +229,7 @@ describe("Test login hooks", () => {
|
|
|
230
229
|
setEmptyIndex(queryClient);
|
|
231
230
|
|
|
232
231
|
const { result } = renderHook(() => useLogout(), {
|
|
233
|
-
wrapper: createWrapper(undefined, queryClient)
|
|
232
|
+
wrapper: createWrapper(undefined, queryClient),
|
|
234
233
|
});
|
|
235
234
|
|
|
236
235
|
const { logout } = result.current;
|