@scm-manager/ui-api 2.31.0 → 2.31.1-20220118-142503

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