Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow sharing permissions by ORCID #3196

Merged
merged 8 commits into from
Nov 11, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ export const PERMISSION_FRAGMENT = gql`
user {
id
email
orcid
name
}
level
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`UpdateDatasetPermissions mutation > renders with default props 1`] = `
exports[`UpdateDatasetPermissions mutation > calls UPDATE_ORCID_PERMISSIONS when clicked with an ORCID 1`] = `
<DocumentFragment>
<button
aria-label="Share"
class="on-button on-button--small on-button--primary btn-modal-action"
role="button"
type="button"
>
Share
</button>
</DocumentFragment>
`;

exports[`UpdateDatasetPermissions mutation > calls UPDATE_PERMISSIONS when clicked with an email address 1`] = `
<DocumentFragment>
<button
aria-label="Share"
Expand All @@ -13,7 +26,7 @@ exports[`UpdateDatasetPermissions mutation > renders with default props 1`] = `
</DocumentFragment>
`;

exports[`UpdateDatasetPermissions mutation > renders with typical props 1`] = `
exports[`UpdateDatasetPermissions mutation > renders with default props 1`] = `
<DocumentFragment>
<button
aria-label="Share"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,44 @@
import React from "react"
import { render } from "@testing-library/react"
import { fireEvent, render, screen, waitFor } from "@testing-library/react"
import { MockedProvider } from "@apollo/client/testing"
import { UpdateDatasetPermissions } from "../update-permissions"
import {
isValidOrcid,
UPDATE_ORCID_PERMISSIONS,
UPDATE_PERMISSIONS,
UpdateDatasetPermissions,
} from "../update-permissions"

function permissionMocksFactory(
updatePermissionsCalled,
updateOrcidPermissionsCalled,
) {
return [
{
request: {
query: UPDATE_PERMISSIONS,

variables: {
datasetId: "ds000005",
userEmail: "[email protected]",
level: "ro",
},
},
newData: updatePermissionsCalled,
},
{
request: {
query: UPDATE_ORCID_PERMISSIONS,

variables: {
datasetId: "ds000005",
userOrcid: "0000-0002-1694-233X",
level: "ro",
},
},
newData: updateOrcidPermissionsCalled,
},
]
}

describe("UpdateDatasetPermissions mutation", () => {
it("renders with default props", () => {
Expand All @@ -12,17 +49,76 @@ describe("UpdateDatasetPermissions mutation", () => {
)
expect(asFragment()).toMatchSnapshot()
})
it("renders with typical props", () => {
it("calls UPDATE_PERMISSIONS when clicked with an email address", async () => {
const updatePermissionsCalled = vi.fn()
const updateOrcidPermissionsCalled = vi.fn()
const mocks = permissionMocksFactory(
updatePermissionsCalled,
updateOrcidPermissionsCalled,
)
const done = vi.fn()
const { asFragment } = render(
<MockedProvider>
<MockedProvider mocks={mocks} addTypename={false}>
<UpdateDatasetPermissions
datasetId="ds000005"
userEmail="[email protected]"
access="ro"
done={vi.fn()}
userIdentifier="[email protected]"
metadata="ro"
done={done}
/>
</MockedProvider>,
)
expect(asFragment()).toMatchSnapshot()
const fragment = asFragment()
expect(fragment).toMatchSnapshot()
// Try clicking the button and make sure the right mutation runs
const button = screen.getByRole("button")
await fireEvent.click(button)
// Make sure it ran at all
await waitFor(() => expect(done).toHaveBeenCalled())
// Verify the expected query ran
await waitFor(() => expect(updatePermissionsCalled).toHaveBeenCalled())
await waitFor(() =>
expect(updateOrcidPermissionsCalled).not.toHaveBeenCalled()
)
})
it("calls UPDATE_ORCID_PERMISSIONS when clicked with an ORCID", async () => {
const updatePermissionsCalled = vi.fn()
const updateOrcidPermissionsCalled = vi.fn()
const mocks = permissionMocksFactory(
updatePermissionsCalled,
updateOrcidPermissionsCalled,
)
const done = vi.fn()
const { asFragment } = render(
<MockedProvider mocks={mocks} addTypename={false}>
<UpdateDatasetPermissions
datasetId="ds000005"
userIdentifier="0000-0002-1694-233X"
metadata="ro"
done={done}
/>
</MockedProvider>,
)
const fragment = asFragment()
expect(fragment).toMatchSnapshot()
// Try clicking the button and make sure the right mutation runs
const button = screen.getByRole("button")
await fireEvent.click(button)
// Make sure it ran at all
await waitFor(() => expect(done).toHaveBeenCalled())
// Verify the expected query ran
await waitFor(() => expect(updatePermissionsCalled).not.toHaveBeenCalled())
await waitFor(() => expect(updateOrcidPermissionsCalled).toHaveBeenCalled())
})
describe("isValidOrcid", () => {
it("matches typical ORCID strings", () => {
expect(isValidOrcid("0000-0001-2345-678")).toBe(false)
expect(isValidOrcid("0000-0001-2345-678f")).toBe(false)
expect(isValidOrcid("19818c4d-1e60-4480-a317-6fcc1c1a88c6")).toBe(false)
expect(isValidOrcid("0000000123456789")).toBe(false)
// Check for a correct value
expect(isValidOrcid("0000-0001-2345-6789")).toBe(true)
// Test with the X checksum value
expect(isValidOrcid("0000-0002-1694-233X")).toBe(true)
})
})
})
Loading
Loading