@@ -3,8 +3,14 @@ const TEST_IMAGE_SOURCE_URL =
33
44import { expect } from 'chai'
55import { after , before , describe , test } from 'mocha'
6- import { initClient , getDefaultSpace , createTestSpace } from '../helpers'
7- import type { Environment , Space } from '../../lib/export-types'
6+ import {
7+ initClient ,
8+ getDefaultSpace ,
9+ createTestSpace ,
10+ initPlainClient ,
11+ getTestOrganizationId ,
12+ } from '../helpers'
13+ import type { ConceptProps , Environment , PlainClientAPI , Space } from '../../lib/export-types'
814
915describe ( 'Asset api' , function ( ) {
1016 describe ( 'Read' , ( ) => {
@@ -48,10 +54,14 @@ describe('Asset api', function () {
4854
4955 // Write test seems currently broken
5056 describe ( 'Write' , function ( ) {
51- let space
52- let environment
57+ let space : Space
58+ let environment : Environment
59+ let client : PlainClientAPI
5360
5461 before ( async ( ) => {
62+ client = initPlainClient ( {
63+ organizationId : getTestOrganizationId ( ) ,
64+ } )
5565 space = await createTestSpace ( initClient ( { retryOnError : false } ) , 'Assets' )
5666 environment = await space . getEnvironment ( 'master' )
5767 await environment . createLocale ( {
@@ -146,6 +156,7 @@ describe('Asset api', function () {
146156 file : '<svg xmlns="http://www.w3.org/2000/svg"><path fill="red" d="M50 50h150v50H50z"/></svg>' ,
147157 } ,
148158 } ,
159+ description : { } ,
149160 } ,
150161 } )
151162
@@ -171,6 +182,7 @@ describe('Asset api', function () {
171182 file : '<svg xmlns="http://www.w3.org/2000/svg"><path fill="red" d="M50 50h150v50H50z"/></svg>' ,
172183 } ,
173184 } ,
185+ description : { } ,
174186 } ,
175187 } )
176188 const processedAsset = await asset . processForAllLocales ( { processingCheckWait : 5000 } )
@@ -192,6 +204,7 @@ describe('Asset api', function () {
192204 file : '<svg xmlns="http://www.w3.org/2000/svg"><path fill="blue" d="M50 50h150v50H50z"/></svg>' ,
193205 } ,
194206 } ,
207+ description : { } ,
195208 } ,
196209 } ,
197210 {
@@ -203,5 +216,145 @@ describe('Asset api', function () {
203216 expect ( e ) . to . be . instanceOf ( Error )
204217 }
205218 } )
219+
220+ describe ( 'Taxonomy' , ( ) => {
221+ const conceptsToCleanUp : ConceptProps [ ] = [ ]
222+
223+ after ( async ( ) => {
224+ for ( const conceptToBeDeleted of conceptsToCleanUp ) {
225+ await client . concept . delete ( {
226+ conceptId : conceptToBeDeleted . sys . id ,
227+ version : conceptToBeDeleted . sys . version ,
228+ } )
229+ }
230+ } )
231+
232+ test ( 'should create asset with concepts assigned when concepts provided' , async ( ) => {
233+ const newConcept = await client . concept . create (
234+ { } ,
235+ {
236+ prefLabel : {
237+ 'en-US' : 'Concept to be assigned' ,
238+ } ,
239+ }
240+ )
241+ conceptsToCleanUp . push ( newConcept )
242+
243+ const createdAsset = await environment . createAsset ( {
244+ fields : {
245+ title : {
246+ 'en-US' : 'this is the title of a newly created asset with a concept assigned' ,
247+ } ,
248+ file : {
249+ 'en-US' : {
250+ contentType : 'image/jpeg' ,
251+ fileName : 'shiba-stuck.jpg' ,
252+ upload : TEST_IMAGE_SOURCE_URL ,
253+ } ,
254+ } ,
255+ } ,
256+ metadata : {
257+ concepts : [
258+ {
259+ sys : {
260+ id : newConcept . sys . id ,
261+ linkType : 'TaxonomyConcept' ,
262+ type : 'Link' ,
263+ } ,
264+ } ,
265+ ] ,
266+ tags : [ ] ,
267+ } ,
268+ } )
269+
270+ expect ( createdAsset . metadata . concepts ) . lengthOf ( 1 )
271+ expect ( createdAsset . metadata . concepts [ 0 ] . sys . id ) . to . eq ( newConcept . sys . id )
272+ } )
273+
274+ test ( 'should update asset with concepts assigned when concepts are provided' , async ( ) => {
275+ const newConcept = await client . concept . create (
276+ { } ,
277+ {
278+ prefLabel : {
279+ 'en-US' : 'Concept to be assigned' ,
280+ } ,
281+ }
282+ )
283+ conceptsToCleanUp . push ( newConcept )
284+
285+ const assetToUpdate = await environment . createAsset ( {
286+ fields : {
287+ title : { 'en-US' : 'this asset should be updated with a concept assigned' } ,
288+ file : {
289+ 'en-US' : {
290+ contentType : 'image/jpeg' ,
291+ fileName : 'shiba-stuck.jpg' ,
292+ upload : TEST_IMAGE_SOURCE_URL ,
293+ } ,
294+ } ,
295+ } ,
296+ // `metadata` intentionally omitted
297+ } )
298+ expect ( assetToUpdate . metadata . concepts ) . to . be . an ( 'array' ) . that . is . empty
299+
300+ assetToUpdate . metadata = {
301+ concepts : [
302+ {
303+ sys : {
304+ id : newConcept . sys . id ,
305+ linkType : 'TaxonomyConcept' ,
306+ type : 'Link' ,
307+ } ,
308+ } ,
309+ ] ,
310+ tags : [ ] ,
311+ }
312+ const updatedAsset = await assetToUpdate . update ( )
313+ expect ( updatedAsset . metadata . concepts ) . lengthOf ( 1 )
314+ expect ( updatedAsset . metadata . concepts [ 0 ] . sys . id ) . to . eq ( newConcept . sys . id )
315+ } )
316+
317+ test ( 'should update asset with concepts removed when concepts already exist' , async ( ) => {
318+ const newConcept = await client . concept . create (
319+ { } ,
320+ {
321+ prefLabel : {
322+ 'en-US' : 'Concept to be assigned' ,
323+ } ,
324+ }
325+ )
326+ conceptsToCleanUp . push ( newConcept )
327+ const assetToDeleteConceptFrom = await environment . createAsset ( {
328+ fields : {
329+ title : { 'en-US' : 'this is the title of an asset with a concept already assigned' } ,
330+ file : {
331+ 'en-US' : {
332+ contentType : 'image/jpeg' ,
333+ fileName : 'shiba-stuck.jpg' ,
334+ upload : TEST_IMAGE_SOURCE_URL ,
335+ } ,
336+ } ,
337+ } ,
338+ metadata : {
339+ concepts : [
340+ {
341+ sys : {
342+ id : newConcept . sys . id ,
343+ linkType : 'TaxonomyConcept' ,
344+ type : 'Link' ,
345+ } ,
346+ } ,
347+ ] ,
348+ tags : [ ] ,
349+ } ,
350+ } )
351+ expect ( assetToDeleteConceptFrom . metadata . concepts ) . lengthOf ( 1 )
352+
353+ assetToDeleteConceptFrom . metadata . concepts = [ ]
354+ const updatedAsset = await assetToDeleteConceptFrom . update ( )
355+
356+ expect ( updatedAsset . metadata . concepts ) . to . be . an ( 'array' ) . that . is . empty
357+ } )
358+ } )
206359 } )
207360} )
0 commit comments