@@ -14,9 +14,11 @@ import { getContentLibraryV2CreateApiUrl } from '@src/library-authoring/create-l
1414import { getStudioHomeApiUrl } from '@src/studio-home/data/api' ;
1515
1616import { LegacyLibMigrationPage } from './LegacyLibMigrationPage' ;
17+ import { bulkMigrateLegacyLibrariesUrl } from './data/api' ;
1718
1819const path = '/libraries-v1/migrate/*' ;
1920let axiosMock : MockAdapter ;
21+ let mockShowToast ;
2022
2123mockGetStudioHomeLibraries . applyMock ( ) ;
2224mockGetContentLibraryV2List . applyMock ( ) ;
@@ -41,7 +43,9 @@ const renderPage = () => (
4143
4244describe ( '<LegacyLibMigrationPage />' , ( ) => {
4345 beforeEach ( ( ) => {
44- axiosMock = initializeMocks ( ) . axiosMock ;
46+ const mocks = initializeMocks ( ) ;
47+ axiosMock = mocks . axiosMock ;
48+ mockShowToast = mocks . mockShowToast ;
4549 } ) ;
4650
4751 it ( 'should render legacy library migration page' , async ( ) => {
@@ -292,6 +296,7 @@ describe('<LegacyLibMigrationPage />', () => {
292296
293297 it ( 'should confirm migration' , async ( ) => {
294298 const user = userEvent . setup ( ) ;
299+ axiosMock . onPost ( bulkMigrateLegacyLibrariesUrl ( ) ) . reply ( 200 ) ;
295300 renderPage ( ) ;
296301 expect ( await screen . findByText ( 'Migrate Legacy Libraries' ) ) . toBeInTheDocument ( ) ;
297302 expect ( await screen . findByText ( 'MBA' ) ) . toBeInTheDocument ( ) ;
@@ -334,6 +339,66 @@ describe('<LegacyLibMigrationPage />', () => {
334339 const confirmButton = screen . getByRole ( 'button' , { name : / c o n f i r m / i } ) ;
335340 confirmButton . click ( ) ;
336341
337- // TODO: expect call migrate API
342+ await waitFor ( ( ) => {
343+ expect ( axiosMock . history . post . length ) . toBe ( 1 ) ;
344+ } ) ;
345+ expect ( axiosMock . history . post [ 0 ] . data ) . toBe (
346+ '{"sources":["library-v1:MBA+123","library-v1:UNIX+LG1","library-v1:MBA+1234"],"target":"lib:SampleTaxonomyOrg1:TL1","create_collections":true,"repeat_handling_strategy":"fork"}' ,
347+ ) ;
348+ expect ( mockShowToast ) . toHaveBeenCalledWith ( '3 legacy libraries are being migrated.' ) ;
349+ } ) ;
350+
351+ it ( 'should show error when confirm migration' , async ( ) => {
352+ const user = userEvent . setup ( ) ;
353+ axiosMock . onPost ( bulkMigrateLegacyLibrariesUrl ( ) ) . reply ( 400 ) ;
354+ renderPage ( ) ;
355+ expect ( await screen . findByText ( 'Migrate Legacy Libraries' ) ) . toBeInTheDocument ( ) ;
356+ expect ( await screen . findByText ( 'MBA' ) ) . toBeInTheDocument ( ) ;
357+
358+ // The filter is 'unmigrated' by default.
359+ // Clear the filter to select all libraries
360+ const filterButton = screen . getByRole ( 'button' , { name : / u n m i g r a t e d / i } ) ;
361+ await user . click ( filterButton ) ;
362+ const clearButton = await screen . findByRole ( 'button' , { name : / c l e a r f i l t e r / i } ) ;
363+ await user . click ( clearButton ) ;
364+
365+ const legacyLibrary1 = screen . getByRole ( 'checkbox' , { name : 'MBA' } ) ;
366+ const legacyLibrary2 = screen . getByRole ( 'checkbox' , { name : / l e g a c y l i b r a r y 1 i m p o r t e d l i b r a r y / i } ) ;
367+ const legacyLibrary3 = screen . getByRole ( 'checkbox' , { name : 'MBA 1' } ) ;
368+
369+ legacyLibrary1 . click ( ) ;
370+ legacyLibrary2 . click ( ) ;
371+ legacyLibrary3 . click ( ) ;
372+
373+ const nextButton = screen . getByRole ( 'button' , { name : / n e x t / i } ) ;
374+ nextButton . click ( ) ;
375+
376+ // Should show alert of SelectDestinationView
377+ expect ( await screen . findByText ( / a n y l e g a c y l i b r a r i e s t h a t a r e u s e d / i) ) . toBeInTheDocument ( ) ;
378+ expect ( await screen . findByText ( 'Test Library 1' ) ) . toBeInTheDocument ( ) ;
379+ const radioButton = screen . getByRole ( 'radio' , { name : / t e s t l i b r a r y 1 / i } ) ;
380+ radioButton . click ( ) ;
381+
382+ nextButton . click ( ) ;
383+
384+ // Should show alert of ConfirmationView
385+ expect ( await screen . findByText ( / t h e s e 3 l e g a c y l i b r a r i e s w i l l b e m i g r a t e d t o / i) ) . toBeInTheDocument ( ) ;
386+ expect ( screen . getByText ( 'MBA' ) ) . toBeInTheDocument ( ) ;
387+ expect ( screen . getByText ( 'Legacy library 1' ) ) . toBeInTheDocument ( ) ;
388+ expect ( screen . getByText ( 'MBA 1' ) ) . toBeInTheDocument ( ) ;
389+ expect ( screen . getByText (
390+ / P r e v i o u s l y m i g r a t e d l i b r a r y . A n y p r o b l e m b a n k l i n k s w e r e a l r e a d y m o v e d w i l l b e m i g r a t e d t o / i,
391+ ) ) . toBeInTheDocument ( ) ;
392+
393+ const confirmButton = screen . getByRole ( 'button' , { name : / c o n f i r m / i } ) ;
394+ confirmButton . click ( ) ;
395+
396+ await waitFor ( ( ) => {
397+ expect ( axiosMock . history . post . length ) . toBe ( 1 ) ;
398+ } ) ;
399+ expect ( axiosMock . history . post [ 0 ] . data ) . toBe (
400+ '{"sources":["library-v1:MBA+123","library-v1:UNIX+LG1","library-v1:MBA+1234"],"target":"lib:SampleTaxonomyOrg1:TL1","create_collections":true,"repeat_handling_strategy":"fork"}' ,
401+ ) ;
402+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Legacy libraries migration failed.' ) ;
338403 } ) ;
339404} ) ;
0 commit comments