@@ -2,19 +2,20 @@ import fetchMock from 'fetch-mock-jest';
22
33import userEvent from '@testing-library/user-event' ;
44import MockAdapter from 'axios-mock-adapter/types' ;
5+ import { mockContentSearchConfig } from '../../../search-manager/data/api.mock' ;
56import {
67 initializeMocks ,
78 render as baseRender ,
89 screen ,
910 waitFor ,
10- } from '../../testUtils' ;
11- import mockCollectionsResults from '../__mocks__/collection-search.json' ;
12- import { mockContentSearchConfig } from '../../search-manager/data/api.mock' ;
13- import { mockContentLibrary , mockLibraryBlockMetadata } from '../data/api.mocks' ;
11+ } from '../../../testUtils' ;
12+ import mockCollectionsResults from '../../__mocks__/collection-search.json' ;
13+ import { LibraryProvider } from '../../common/context/LibraryContext' ;
14+ import { SidebarProvider } from '../../common/context/SidebarContext' ;
15+ import { getLibraryBlockCollectionsUrl } from '../../data/api' ;
16+ import { useUpdateComponentCollections } from '../../data/apiHooks' ;
17+ import { mockContentLibrary , mockLibraryBlockMetadata } from '../../data/api.mocks' ;
1418import ManageCollections from './ManageCollections' ;
15- import { LibraryProvider } from '../common/context/LibraryContext' ;
16- import { SidebarProvider } from '../common/context/SidebarContext' ;
17- import { getLibraryBlockCollectionsUrl } from '../data/api' ;
1819
1920let axiosMock : MockAdapter ;
2021let mockShowToast ;
@@ -60,8 +61,9 @@ describe('<ManageCollections />', () => {
6061 const url = getLibraryBlockCollectionsUrl ( mockLibraryBlockMetadata . usageKeyWithCollections ) ;
6162 axiosMock . onPatch ( url ) . reply ( 200 ) ;
6263 render ( < ManageCollections
63- usageKey = { mockLibraryBlockMetadata . usageKeyWithCollections }
64+ opaqueKey = { mockLibraryBlockMetadata . usageKeyWithCollections }
6465 collections = { [ { title : 'My first collection' , key : 'my-first-collection' } ] }
66+ useUpdateCollectionsHook = { useUpdateComponentCollections }
6567 /> ) ;
6668 const manageBtn = await screen . findByRole ( 'button' , { name : 'Manage Collections' } ) ;
6769 userEvent . click ( manageBtn ) ;
@@ -73,10 +75,10 @@ describe('<ManageCollections />', () => {
7375 userEvent . click ( confirmBtn ) ;
7476 await waitFor ( ( ) => {
7577 expect ( axiosMock . history . patch . length ) . toEqual ( 1 ) ;
76- expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Component collections updated' ) ;
77- expect ( JSON . parse ( axiosMock . history . patch [ 0 ] . data ) ) . toEqual ( {
78- collection_keys : [ 'my-first-collection' , 'my-second-collection' ] ,
79- } ) ;
78+ } ) ;
79+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Item collections updated' ) ;
80+ expect ( JSON . parse ( axiosMock . history . patch [ 0 ] . data ) ) . toEqual ( {
81+ collection_keys : [ 'my-first-collection' , 'my-second-collection' ] ,
8082 } ) ;
8183 expect ( screen . queryByRole ( 'search' ) ) . not . toBeInTheDocument ( ) ;
8284 } ) ;
@@ -85,8 +87,9 @@ describe('<ManageCollections />', () => {
8587 const url = getLibraryBlockCollectionsUrl ( mockLibraryBlockMetadata . usageKeyWithCollections ) ;
8688 axiosMock . onPatch ( url ) . reply ( 400 ) ;
8789 render ( < ManageCollections
88- usageKey = { mockLibraryBlockMetadata . usageKeyWithCollections }
90+ opaqueKey = { mockLibraryBlockMetadata . usageKeyWithCollections }
8991 collections = { [ ] }
92+ useUpdateCollectionsHook = { useUpdateComponentCollections }
9093 /> ) ;
9194 screen . logTestingPlaygroundURL ( ) ;
9295 const manageBtn = await screen . findByRole ( 'button' , { name : 'Add to Collection' } ) ;
@@ -99,20 +102,21 @@ describe('<ManageCollections />', () => {
99102 userEvent . click ( confirmBtn ) ;
100103 await waitFor ( ( ) => {
101104 expect ( axiosMock . history . patch . length ) . toEqual ( 1 ) ;
102- expect ( JSON . parse ( axiosMock . history . patch [ 0 ] . data ) ) . toEqual ( {
103- collection_keys : [ 'my-second-collection' ] ,
104- } ) ;
105- expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Failed to update Component collections' ) ;
106105 } ) ;
106+ expect ( JSON . parse ( axiosMock . history . patch [ 0 ] . data ) ) . toEqual ( {
107+ collection_keys : [ 'my-second-collection' ] ,
108+ } ) ;
109+ expect ( mockShowToast ) . toHaveBeenCalledWith ( 'Failed to update item collections' ) ;
107110 expect ( screen . queryByRole ( 'search' ) ) . not . toBeInTheDocument ( ) ;
108111 } ) ;
109112
110113 it ( 'should close manage collections selection on cancel' , async ( ) => {
111114 const url = getLibraryBlockCollectionsUrl ( mockLibraryBlockMetadata . usageKeyWithCollections ) ;
112115 axiosMock . onPatch ( url ) . reply ( 400 ) ;
113116 render ( < ManageCollections
114- usageKey = { mockLibraryBlockMetadata . usageKeyWithCollections }
117+ opaqueKey = { mockLibraryBlockMetadata . usageKeyWithCollections }
115118 collections = { [ ] }
119+ useUpdateCollectionsHook = { useUpdateComponentCollections }
116120 /> ) ;
117121 const manageBtn = await screen . findByRole ( 'button' , { name : 'Add to Collection' } ) ;
118122 userEvent . click ( manageBtn ) ;
@@ -124,8 +128,8 @@ describe('<ManageCollections />', () => {
124128 userEvent . click ( cancelBtn ) ;
125129 await waitFor ( ( ) => {
126130 expect ( axiosMock . history . patch . length ) . toEqual ( 0 ) ;
127- expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
128131 } ) ;
132+ expect ( mockShowToast ) . not . toHaveBeenCalled ( ) ;
129133 expect ( screen . queryByRole ( 'search' ) ) . not . toBeInTheDocument ( ) ;
130134 } ) ;
131135} ) ;
0 commit comments