Skip to content

Commit 85ddbfc

Browse files
committed
tests(app): Integration test UpdatePermissions component based on input type
1 parent 397753e commit 85ddbfc

File tree

2 files changed

+107
-10
lines changed

2 files changed

+107
-10
lines changed

packages/openneuro-app/src/scripts/dataset/mutations/__tests__/__snapshots__/update-permissions.spec.jsx.snap

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`UpdateDatasetPermissions mutation > renders with default props 1`] = `
3+
exports[`UpdateDatasetPermissions mutation > calls UPDATE_ORCID_PERMISSIONS when clicked with an ORCID 1`] = `
4+
<DocumentFragment>
5+
<button
6+
aria-label="Share"
7+
class="on-button on-button--small on-button--primary btn-modal-action"
8+
role="button"
9+
type="button"
10+
>
11+
Share
12+
</button>
13+
</DocumentFragment>
14+
`;
15+
16+
exports[`UpdateDatasetPermissions mutation > calls UPDATE_PERMISSIONS when clicked with an email address 1`] = `
417
<DocumentFragment>
518
<button
619
aria-label="Share"
@@ -13,7 +26,7 @@ exports[`UpdateDatasetPermissions mutation > renders with default props 1`] = `
1326
</DocumentFragment>
1427
`;
1528

16-
exports[`UpdateDatasetPermissions mutation > renders with typical props 1`] = `
29+
exports[`UpdateDatasetPermissions mutation > renders with default props 1`] = `
1730
<DocumentFragment>
1831
<button
1932
aria-label="Share"

packages/openneuro-app/src/scripts/dataset/mutations/__tests__/update-permissions.spec.jsx

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,44 @@
11
import React from "react"
2-
import { render } from "@testing-library/react"
2+
import { fireEvent, render, screen, waitFor } from "@testing-library/react"
33
import { MockedProvider } from "@apollo/client/testing"
4-
import { isValidOrcid, UpdateDatasetPermissions } from "../update-permissions"
4+
import {
5+
isValidOrcid,
6+
UPDATE_ORCID_PERMISSIONS,
7+
UPDATE_PERMISSIONS,
8+
UpdateDatasetPermissions,
9+
} from "../update-permissions"
10+
11+
function permissionMocksFactory(
12+
updatePermissionsCalled,
13+
updateOrcidPermissionsCalled,
14+
) {
15+
return [
16+
{
17+
request: {
18+
query: UPDATE_PERMISSIONS,
19+
20+
variables: {
21+
datasetId: "ds000005",
22+
userEmail: "[email protected]",
23+
level: "ro",
24+
},
25+
},
26+
newData: updatePermissionsCalled,
27+
},
28+
{
29+
request: {
30+
query: UPDATE_ORCID_PERMISSIONS,
31+
32+
variables: {
33+
datasetId: "ds000005",
34+
userOrcid: "0000-0002-1694-233X",
35+
level: "ro",
36+
},
37+
},
38+
newData: updateOrcidPermissionsCalled,
39+
},
40+
]
41+
}
542

643
describe("UpdateDatasetPermissions mutation", () => {
744
it("renders with default props", () => {
@@ -12,18 +49,65 @@ describe("UpdateDatasetPermissions mutation", () => {
1249
)
1350
expect(asFragment()).toMatchSnapshot()
1451
})
15-
it("renders with typical props", () => {
52+
it("calls UPDATE_PERMISSIONS when clicked with an email address", async () => {
53+
const updatePermissionsCalled = vi.fn()
54+
const updateOrcidPermissionsCalled = vi.fn()
55+
const mocks = permissionMocksFactory(
56+
updatePermissionsCalled,
57+
updateOrcidPermissionsCalled,
58+
)
59+
const done = vi.fn()
1660
const { asFragment } = render(
17-
<MockedProvider>
61+
<MockedProvider mocks={mocks} addTypename={false}>
1862
<UpdateDatasetPermissions
1963
datasetId="ds000005"
20-
userEmail="[email protected]"
21-
access="ro"
22-
done={vi.fn()}
64+
userIdentifier="[email protected]"
65+
metadata="ro"
66+
done={done}
2367
/>
2468
</MockedProvider>,
2569
)
26-
expect(asFragment()).toMatchSnapshot()
70+
const fragment = asFragment()
71+
expect(fragment).toMatchSnapshot()
72+
// Try clicking the button and make sure the right mutation runs
73+
const button = screen.getByRole("button")
74+
await fireEvent.click(button)
75+
// Make sure it ran at all
76+
await waitFor(() => expect(done).toHaveBeenCalled())
77+
// Verify the expected query ran
78+
await waitFor(() => expect(updatePermissionsCalled).toHaveBeenCalled())
79+
await waitFor(() =>
80+
expect(updateOrcidPermissionsCalled).not.toHaveBeenCalled()
81+
)
82+
})
83+
it("calls UPDATE_ORCID_PERMISSIONS when clicked with an ORCID", async () => {
84+
const updatePermissionsCalled = vi.fn()
85+
const updateOrcidPermissionsCalled = vi.fn()
86+
const mocks = permissionMocksFactory(
87+
updatePermissionsCalled,
88+
updateOrcidPermissionsCalled,
89+
)
90+
const done = vi.fn()
91+
const { asFragment } = render(
92+
<MockedProvider mocks={mocks} addTypename={false}>
93+
<UpdateDatasetPermissions
94+
datasetId="ds000005"
95+
userIdentifier="0000-0002-1694-233X"
96+
metadata="ro"
97+
done={done}
98+
/>
99+
</MockedProvider>,
100+
)
101+
const fragment = asFragment()
102+
expect(fragment).toMatchSnapshot()
103+
// Try clicking the button and make sure the right mutation runs
104+
const button = screen.getByRole("button")
105+
await fireEvent.click(button)
106+
// Make sure it ran at all
107+
await waitFor(() => expect(done).toHaveBeenCalled())
108+
// Verify the expected query ran
109+
await waitFor(() => expect(updatePermissionsCalled).not.toHaveBeenCalled())
110+
await waitFor(() => expect(updateOrcidPermissionsCalled).toHaveBeenCalled())
27111
})
28112
describe("isValidOrcid", () => {
29113
it("matches typical ORCID strings", () => {

0 commit comments

Comments
 (0)