@@ -5,11 +5,13 @@ import {
55 DataverseApiAuthMechanism
66} from '../../../src/core/infra/repositories/ApiConfig'
77import {
8+ createCollectionDTO ,
89 createCollectionModel ,
9- createCollectionPayload
10+ createCollectionPayload ,
11+ createNewCollectionRequestPayload
1012} from '../../testHelpers/collections/collectionHelper'
1113import { TestConstants } from '../../testHelpers/TestConstants'
12- import { ReadError } from '../../../src'
14+ import { ReadError , WriteError } from '../../../src'
1315import { ROOT_COLLECTION_ALIAS } from '../../../src/collections/domain/models/Collection'
1416
1517describe ( 'CollectionsRepository' , ( ) => {
@@ -28,7 +30,10 @@ describe('CollectionsRepository', () => {
2830 DataverseApiAuthMechanism . API_KEY ,
2931 TestConstants . TEST_DUMMY_API_KEY
3032 )
33+
34+ jest . clearAllMocks ( )
3135 } )
36+
3237 describe ( 'getCollection' , ( ) => {
3338 const expectedRequestConfigApiKey = {
3439 params : {
@@ -108,4 +113,63 @@ describe('CollectionsRepository', () => {
108113 } )
109114 } )
110115 } )
116+
117+ describe ( 'createCollection' , ( ) => {
118+ const testNewCollection = createCollectionDTO ( )
119+
120+ const testCreatedCollectionId = 1
121+ const testCreateCollectionResponse = {
122+ data : {
123+ status : 'OK' ,
124+ data : {
125+ id : testCreatedCollectionId ,
126+ }
127+ }
128+ }
129+
130+ const expectedNewCollectionRequestPayloadJson = JSON . stringify ( createNewCollectionRequestPayload ( ) )
131+ const expectedApiEndpoint = `${ TestConstants . TEST_API_URL } /dataverses/root`
132+
133+ test ( 'should call the API with a correct request payload' , async ( ) => {
134+ jest . spyOn ( axios , 'post' ) . mockResolvedValue ( testCreateCollectionResponse )
135+
136+ // API Key auth
137+ let actual = await sut . createCollection ( testNewCollection )
138+
139+ expect ( axios . post ) . toHaveBeenCalledWith (
140+ expectedApiEndpoint ,
141+ expectedNewCollectionRequestPayloadJson ,
142+ TestConstants . TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
143+ )
144+ expect ( actual ) . toStrictEqual ( testCreatedCollectionId )
145+
146+ // Session cookie auth
147+ ApiConfig . init ( TestConstants . TEST_API_URL , DataverseApiAuthMechanism . SESSION_COOKIE )
148+
149+ actual = await sut . createCollection ( testNewCollection )
150+
151+ expect ( axios . post ) . toHaveBeenCalledWith (
152+ expectedApiEndpoint ,
153+ expectedNewCollectionRequestPayloadJson ,
154+ TestConstants . TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_SESSION_COOKIE
155+ )
156+ expect ( actual ) . toStrictEqual ( testCreatedCollectionId )
157+ } )
158+
159+ test ( 'should return error result on error response' , async ( ) => {
160+ jest . spyOn ( axios , 'post' ) . mockRejectedValue ( TestConstants . TEST_ERROR_RESPONSE )
161+
162+ let error = undefined as unknown as WriteError
163+ await sut
164+ . createCollection ( testNewCollection )
165+ . catch ( ( e ) => ( error = e ) )
166+
167+ expect ( axios . post ) . toHaveBeenCalledWith (
168+ expectedApiEndpoint ,
169+ expectedNewCollectionRequestPayloadJson ,
170+ TestConstants . TEST_EXPECTED_AUTHENTICATED_REQUEST_CONFIG_API_KEY
171+ )
172+ expect ( error ) . toBeInstanceOf ( Error )
173+ } )
174+ } )
111175} )
0 commit comments