@truedat/core 6.3.1 → 6.3.2

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.
@@ -0,0 +1,143 @@
1
+ import { compile } from "path-to-regexp";
2
+ import { waitFor } from "@testing-library/react";
3
+ import { renderHook } from "@testing-library/react-hooks";
4
+ import { API_ACL_RESOURCE_ENTRIES, API_ACL_ENTRY } from "../../api";
5
+ import { apiJsonPost, apiJsonPatch, apiJsonDelete } from "../../services/api";
6
+ import {
7
+ useAclEntries,
8
+ useAclEntryCreate,
9
+ useAclEntryUpdate,
10
+ useAclEntryDelete,
11
+ } from "../useAclEntries";
12
+
13
+ jest.mock("@truedat/core/services/api", () => {
14
+ const originalModule = jest.requireActual("@truedat/core/services/api");
15
+
16
+ return {
17
+ __esModule: true,
18
+ ...originalModule,
19
+ apiJson: jest.fn(() => ({
20
+ data: {
21
+ _embedded: {
22
+ acl_entries: [
23
+ {
24
+ _links: {
25
+ self: {
26
+ href: "/api/acl_entries/1",
27
+ methods: ["GET", "DELETE", "POST"],
28
+ },
29
+ },
30
+ acl_entry_id: 1,
31
+ description: null,
32
+ principal: {
33
+ full_name: "Foo",
34
+ id: 11,
35
+ user_name: "foo@bar.com",
36
+ },
37
+ principal_type: "user",
38
+ role_id: 111,
39
+ role_name: "baz",
40
+ },
41
+ ],
42
+ },
43
+ },
44
+ })),
45
+ apiJsonPost: jest.fn(),
46
+ apiJsonPatch: jest.fn(),
47
+ apiJsonDelete: jest.fn(),
48
+ };
49
+ });
50
+
51
+ const id = 1;
52
+ const resource = {
53
+ type: "domain",
54
+ id,
55
+ };
56
+
57
+ const payload = {
58
+ acl_entry: {
59
+ role_name: "role2",
60
+ resource_type: resource.type,
61
+ principal_type: "user",
62
+ principal_id: 1,
63
+ description: "foo",
64
+ },
65
+ };
66
+
67
+ const acl_entry = {
68
+ acl_entry_id: id,
69
+ description: null,
70
+ principal: {
71
+ full_name: "Foo",
72
+ id: 11,
73
+ user_name: "foo@bar.com",
74
+ },
75
+ principal_type: "user",
76
+ role_id: 111,
77
+ role_name: "baz",
78
+ };
79
+
80
+ describe("hooks: useAclEntries", () => {
81
+ it("use acl entries return acl entries correctly", () => {
82
+ const {
83
+ result: {
84
+ current: { data },
85
+ },
86
+ } = renderHook(() => useAclEntries(resource));
87
+
88
+ waitFor(() =>
89
+ expect(data).toBe(
90
+ expect.objectContaining({
91
+ aclEntries: [acl_entry],
92
+ actions: { canCreate: true },
93
+ })
94
+ )
95
+ );
96
+ });
97
+
98
+ it("create acl entry trigger calls api json post with correct url and payload", () => {
99
+ const {
100
+ result: {
101
+ current: { trigger },
102
+ },
103
+ } = renderHook(() => useAclEntryCreate(resource));
104
+
105
+ trigger(payload);
106
+
107
+ const url = compile(API_ACL_RESOURCE_ENTRIES)(resource);
108
+ expect(apiJsonPost).toHaveBeenCalledWith(url, {
109
+ ...payload,
110
+ });
111
+ });
112
+
113
+ it("update acl entry trigger calls api json patch with correct url and payload", () => {
114
+ const {
115
+ result: {
116
+ current: { trigger },
117
+ },
118
+ } = renderHook(() => useAclEntryUpdate(resource.id));
119
+
120
+ trigger(payload);
121
+
122
+ const url = compile(API_ACL_ENTRY)({ id: resource.id });
123
+ expect(apiJsonPatch).toHaveBeenCalledWith(url, {
124
+ ...payload,
125
+ });
126
+ });
127
+
128
+ it("update acl entry trigger calls api json delete with correct url", () => {
129
+ const {
130
+ result: {
131
+ current: { trigger },
132
+ },
133
+ } = renderHook(() => useAclEntryDelete(resource.id));
134
+
135
+ trigger();
136
+
137
+ const url = compile(API_ACL_ENTRY)({ id: resource.id });
138
+ expect(apiJsonDelete).toHaveBeenCalledWith(
139
+ url,
140
+ expect.objectContaining({})
141
+ );
142
+ });
143
+ });
@@ -0,0 +1,44 @@
1
+ import { compile } from "path-to-regexp";
2
+ import _ from "lodash/fp";
3
+ import useSWR from "swr";
4
+ import useSWRMutation from "swr/mutation";
5
+ import { accentInsensitivePathOrder } from "../services/sort";
6
+ import { API_ACL_ENTRY, API_ACL_RESOURCE_ENTRIES } from "../api";
7
+ import {
8
+ apiJson,
9
+ apiJsonPatch,
10
+ apiJsonPost,
11
+ apiJsonDelete,
12
+ JSON_OPTS,
13
+ } from "../services/api";
14
+
15
+ export const useAclEntries = (resource) => {
16
+ const url = compile(API_ACL_RESOURCE_ENTRIES)(resource);
17
+ const { data, error, loading, mutate } = useSWR(url, apiJson);
18
+ const aclEntries = _.flow(
19
+ _.pathOr([], "_embedded.acl_entries"),
20
+ _.sortBy(accentInsensitivePathOrder("user_name"))
21
+ )(data?.data);
22
+ const actions = {
23
+ canCreate: _.flow(
24
+ _.pathOr([], "_links.self.methods"),
25
+ _.includes("POST")
26
+ )(data?.data),
27
+ };
28
+ return { data: { aclEntries, actions }, error, loading, mutate };
29
+ };
30
+
31
+ export const useAclEntryCreate = (resource) => {
32
+ const url = compile(API_ACL_RESOURCE_ENTRIES)(resource);
33
+ return useSWRMutation(url, (url, { arg }) => apiJsonPost(url, arg));
34
+ };
35
+
36
+ export const useAclEntryUpdate = (id) => {
37
+ const url = compile(API_ACL_ENTRY)({ id });
38
+ return useSWRMutation(url, (url, { arg }) => apiJsonPatch(url, arg));
39
+ };
40
+
41
+ export const useAclEntryDelete = (id) => {
42
+ const url = compile(API_ACL_ENTRY)({ id });
43
+ return useSWRMutation(url, (url) => apiJsonDelete(url, JSON_OPTS));
44
+ };
package/src/routes.js CHANGED
@@ -215,15 +215,17 @@ export const STRUCTURE_STRUCTURE_LINKS_NEW =
215
215
  "/structures/:id/structureLinks/new";
216
216
  export const STRUCTURE_LINKS = "/structures/:id/links";
217
217
  export const STRUCTURE_LINKS_NEW = "/structures/:id/links/new";
218
+ export const STRUCTURE_MEMBERS = "/structures/:id/members";
219
+ export const STRUCTURE_MEMBERS_NEW = "/structures/:id/members/new";
218
220
  export const STRUCTURE_METADATA = "/structures/:id/metadata";
219
221
  export const STRUCTURE_NOTES = "/structures/:id/notes";
220
222
  export const STRUCTURE_NOTES_EDIT = "/structures/:id/notes/edit";
221
223
  export const STRUCTURE_PARENTS = "/structures/:id/parents";
222
224
  export const STRUCTURE_PROFILE = "/structures/:id/profile";
223
225
  export const STRUCTURE_RULES = "/structures/:id/rules";
226
+ export const STRUCTURE_TAG_EDIT = "/structureTags/:id/edit";
224
227
  export const STRUCTURE_TAGS = "/structureTags";
225
228
  export const STRUCTURE_TAGS_NEW = "/structureTags/new";
226
- export const STRUCTURE_TAG_EDIT = "/structureTags/:id/edit";
227
229
  export const STRUCTURE_TYPES = "/structureTypes";
228
230
  export const STRUCTURE_TYPES_EDIT = "/structureTypes/:id/edit";
229
231
  export const STRUCTURE_VERSION = "/structures/:id/versions/:version";
@@ -429,9 +431,11 @@ const routes = {
429
431
  STRUCTURE_STRUCTURE_LINKS_NEW,
430
432
  STRUCTURE_LINKS,
431
433
  STRUCTURE_LINKS_NEW,
434
+ STRUCTURE_MEMBERS_NEW,
435
+ STRUCTURE_MEMBERS,
432
436
  STRUCTURE_METADATA,
433
- STRUCTURE_NOTES,
434
437
  STRUCTURE_NOTES_EDIT,
438
+ STRUCTURE_NOTES,
435
439
  STRUCTURE_PARENTS,
436
440
  STRUCTURE_PROFILE,
437
441
  STRUCTURE_RULES,