11import React from 'react' ;
2- import PropTypes from 'prop-types' ;
3- import { IntlProvider } from '@edx/frontend-platform/i18n' ;
4- import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth' ;
5- import { initializeMockApp } from '@edx/frontend-platform' ;
6- import { AppProvider } from '@edx/frontend-platform/react' ;
72import {
83 render , waitFor , waitForElementToBeRemoved , screen , within ,
9- fireEvent , act , cleanup ,
10- } from '@testing-library/react' ;
11- import { QueryClient , QueryClientProvider } from '@tanstack/react-query' ;
12- import MockAdapter from 'axios-mock-adapter' ;
13-
14- import initializeStore from '../../store' ;
4+ fireEvent , act , cleanup , initializeMocks ,
5+ } from '@src/testUtils' ;
156import * as apiHooksModule from '../data/apiHooks' ;
167import * as hooksModule from './hooks' ;
178import * as treeTableModule from '../tree-table' ;
189import TagListTable from './TagListTable' ;
1910
20- let store ;
2111let axiosMock ;
22- const queryClient = new QueryClient ( ) ;
2312const adminUser = {
2413 userId : 3 ,
2514 username : 'abc123' ,
@@ -31,20 +20,6 @@ const nonAdminUser = {
3120 administrator : false ,
3221} ;
3322
34- const RootWrapper = ( { maxDepth = 3 } ) => (
35- < AppProvider store = { store } >
36- < IntlProvider locale = "en" messages = { { } } >
37- < QueryClientProvider client = { queryClient } >
38- < TagListTable taxonomyId = { 1 } maxDepth = { maxDepth } />
39- </ QueryClientProvider >
40- </ IntlProvider >
41- </ AppProvider >
42- ) ;
43-
44- RootWrapper . propTypes = {
45- maxDepth : PropTypes . number ,
46- } ;
47-
4823const tagDefaults = { depth : 0 , external_id : '' , parent_value : null } ;
4924const mockTagsResponse = {
5025 next : null ,
@@ -138,7 +113,7 @@ const subTagsResponse = {
138113const subTagsUrl = 'http://localhost:18010/api/content_tagging/v1/taxonomies/1/tags/?full_depth_threshold=10000&parent_tag=root+tag+1' ;
139114const createTagUrl = 'http://localhost:18010/api/content_tagging/v1/taxonomies/1/tags/' ;
140115
141- const renderTagListTable = ( maxDepth = 3 ) => render ( < RootWrapper maxDepth = { maxDepth } /> ) ;
116+ const renderTagListTable = ( maxDepth = 3 ) => render ( < TagListTable taxonomyId = { 1 } maxDepth = { maxDepth } /> ) ;
142117
143118const flushReactUpdates = async ( ) => {
144119 await act ( async ( ) => {
@@ -212,20 +187,8 @@ const openRenameDraftRow = async (tagName = 'root tag 1') => {
212187} ;
213188
214189describe ( '<TagListTable />' , ( ) => {
215- beforeAll ( async ( ) => {
216- initializeMockApp ( {
217- authenticatedUser : adminUser ,
218- } ) ;
219- axiosMock = new MockAdapter ( getAuthenticatedHttpClient ( ) ) ;
220- } ) ;
221190 beforeEach ( async ( ) => {
222- initializeMockApp ( {
223- authenticatedUser : adminUser ,
224- } ) ;
225- store = initializeStore ( ) ;
226- queryClient . clear ( ) ;
227- axiosMock . reset ( ) ;
228- axiosMock = new MockAdapter ( getAuthenticatedHttpClient ( ) ) ;
191+ ( { axiosMock } = initializeMocks ( { user : adminUser } ) ) ;
229192 axiosMock . onGet ( rootTagsListUrl ) . reply ( 200 , mockTagsResponse ) ;
230193 axiosMock . onGet ( subTagsUrl ) . reply ( 200 , subTagsResponse ) ;
231194 renderTagListTable ( ) ;
@@ -294,7 +257,12 @@ describe('<TagListTable />', () => {
294257 can_add_tag : false ,
295258 } ) ;
296259 cleanup ( ) ;
297- queryClient . clear ( ) ;
260+ ( { axiosMock } = initializeMocks ( { user : adminUser } ) ) ;
261+ axiosMock . onGet ( rootTagsListUrl ) . reply ( 200 , {
262+ ...mockTagsResponse ,
263+ can_add_tag : false ,
264+ } ) ;
265+ axiosMock . onGet ( subTagsUrl ) . reply ( 200 , subTagsResponse ) ;
298266 renderTagListTable ( ) ;
299267 await waitForRootTag ( ) ;
300268
@@ -640,7 +608,7 @@ describe('<TagListTable />', () => {
640608 } ) ;
641609
642610 it ( 'should hide Add Tag for users without taxonomy edit permissions' , async ( ) => {
643- initializeMockApp ( { authenticatedUser : nonAdminUser } ) ;
611+ initializeMocks ( { user : nonAdminUser } ) ;
644612 axiosMock . onGet ( rootTagsListUrl ) . reply ( 200 , mockTagsResponse ) ;
645613
646614 expect ( screen . queryByText ( 'Add Tag' ) ) . not . toBeInTheDocument ( ) ;
@@ -749,7 +717,7 @@ describe('<TagListTable />', () => {
749717 } ) ;
750718
751719 it ( 'should hide or disable Add sub-tag actions when user lacks edit permissions' , async ( ) => {
752- initializeMockApp ( { authenticatedUser : nonAdminUser } ) ;
720+ initializeMocks ( { user : nonAdminUser } ) ;
753721 const addSubtagActions = screen . queryAllByText ( 'Add Subtag' ) ;
754722 if ( addSubtagActions . length === 0 ) {
755723 expect ( addSubtagActions . length ) . toBe ( 0 ) ;
@@ -813,7 +781,12 @@ describe('<TagListTable />', () => {
813781 can_add_tag : false ,
814782 } ) ;
815783 cleanup ( ) ;
816- queryClient . clear ( ) ;
784+ ( { axiosMock } = initializeMocks ( { user : adminUser } ) ) ;
785+ axiosMock . onGet ( rootTagsListUrl ) . reply ( 200 , {
786+ ...mockTagsResponse ,
787+ can_add_tag : false ,
788+ } ) ;
789+ axiosMock . onGet ( subTagsUrl ) . reply ( 200 , subTagsResponse ) ;
817790 renderTagListTable ( ) ;
818791 await waitForRootTag ( ) ;
819792
@@ -825,7 +798,9 @@ describe('<TagListTable />', () => {
825798 axiosMock . reset ( ) ;
826799 axiosMock . onGet ( rootTagsListUrl ) . reply ( 200 , mockTagResponseDisallowingEdits ) ;
827800 cleanup ( ) ;
828- queryClient . clear ( ) ;
801+ ( { axiosMock } = initializeMocks ( { user : adminUser } ) ) ;
802+ axiosMock . onGet ( rootTagsListUrl ) . reply ( 200 , mockTagResponseDisallowingEdits ) ;
803+ axiosMock . onGet ( subTagsUrl ) . reply ( 200 , subTagsResponse ) ;
829804 renderTagListTable ( ) ;
830805 await waitForRootTag ( ) ;
831806
@@ -984,20 +959,8 @@ describe('<TagListTable />', () => {
984959// These async creation flows are intentionally isolated because they pass individually
985960// but can be flaky when interleaved with the larger suite's async/query timing.
986961describe ( '<TagListTable /> isolated async subtag tests' , ( ) => {
987- beforeAll ( async ( ) => {
988- initializeMockApp ( {
989- authenticatedUser : adminUser ,
990- } ) ;
991- axiosMock = new MockAdapter ( getAuthenticatedHttpClient ( ) ) ;
992- } ) ;
993-
994962 beforeEach ( async ( ) => {
995- initializeMockApp ( {
996- authenticatedUser : adminUser ,
997- } ) ;
998- store = initializeStore ( ) ;
999- axiosMock = new MockAdapter ( getAuthenticatedHttpClient ( ) ) ;
1000- queryClient . clear ( ) ;
963+ ( { axiosMock } = initializeMocks ( { user : adminUser } ) ) ;
1001964 } ) ;
1002965
1003966 it ( 'shows the spinner before the query is complete' , async ( ) => {
@@ -1196,8 +1159,7 @@ describe('<TagListTable /> pagination transition behavior', () => {
11961159 beforeEach ( ( ) => {
11971160 tableViewProps = null ;
11981161 mockEnterViewMode . mockReset ( ) ;
1199- store = initializeStore ( ) ;
1200- queryClient . clear ( ) ;
1162+ initializeMocks ( { user : adminUser } ) ;
12011163
12021164 jest . spyOn ( apiHooksModule , 'useTagListData' ) . mockReturnValue ( {
12031165 isLoading : false ,
0 commit comments