11import React from "react"
2- import { render } from "@testing-library/react"
2+ import { fireEvent , render , screen , waitFor } from "@testing-library/react"
33import { MockedProvider } from "@apollo/client/testing"
4- import { 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+ 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
643describe ( "UpdateDatasetPermissions mutation" , ( ) => {
744 it ( "renders with default props" , ( ) => {
@@ -12,17 +49,76 @@ 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- 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 ( ) )
111+ } )
112+ describe ( "isValidOrcid" , ( ) => {
113+ it ( "matches typical ORCID strings" , ( ) => {
114+ expect ( isValidOrcid ( "0000-0001-2345-678" ) ) . toBe ( false )
115+ expect ( isValidOrcid ( "0000-0001-2345-678f" ) ) . toBe ( false )
116+ expect ( isValidOrcid ( "19818c4d-1e60-4480-a317-6fcc1c1a88c6" ) ) . toBe ( false )
117+ expect ( isValidOrcid ( "0000000123456789" ) ) . toBe ( false )
118+ // Check for a correct value
119+ expect ( isValidOrcid ( "0000-0001-2345-6789" ) ) . toBe ( true )
120+ // Test with the X checksum value
121+ expect ( isValidOrcid ( "0000-0002-1694-233X" ) ) . toBe ( true )
122+ } )
27123 } )
28124} )
0 commit comments